Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# This method finds last name separated by /, helps in getting directory or file name without path.
print_name() {
echo $1 | awk -F "/" '{ print $NF }'
}
dir_name=/Users/tanzyy/_temp/logshomedir/*
for d in $dir_name; do
#!/bin/bash
# This script shows how to make multiline comment. Indentation is mandatory inside "comment".
echo "This should get printed: Before multiline comment"
: <<'Comment'
echo "This should NOT get printed: Inside multiline comment"
Comment
#!/bin/bash
#This script shows how to pass shell variable to awk using option -v
index_pos="4"
search_string="31/Mar/2017:03:58:38"
cat ~/Desktop/tempp.txt | awk -v pos=$index_pos -v ss=$search_string '{ if($pos==ss) print $0 }'
@tanzyy
tanzyy / add_to_array.sh
Last active April 11, 2017 23:35
Shell | Add value to array
#!/bin/bash
# This script shows how to declare array and and add vlaues to it.
result=()
result+=('foo')
result+=('bar')
for i in "${result[@]}"
do
@tanzyy
tanzyy / dateloop.sh
Last active March 28, 2017 22:04
Loop with date (MAC)
#!/bin/bash
# On MAC date -d option does not work rather -v.
# Below code logic is as follows:
# Create folders with date(in format YYYY-MM-DD) with consecutive dates until it reaches to input difference
# Here variable datediff is 28 meaning from current date, it will generate dates until there is difference of 28.
now=$(date -v -1d +"%Y-%m-%d")
datediff=28
for (( c=0; c<=$datediff; c++ ))