Shell Tips
1. Quicklinks
2. Reference
- () execute command in subshell
- {} execute command in currecnt shell Usage is same with () expect the final command in the list ends with a semicolon.
3. Sample Section A
3.1. grep
grep -Elr --include=*.xsd --exclude-dir={branches,tags} VendorDesc.xsd $DIRECTORY
### count process and exclude grep itself
ps aux | grep vpnc | grep -v grep -c
3.2. find
find ${1} \( -name Consume*wsdl -o -name Produce*wsdl \) -path *trunk*
find $update_dir \( -name '*.java' \) \( -path '**/source/**' -o -path '**/test/**' -o -path '**/resource/**' \)
3.3. awk
### list all time of each ping
awk -F"=" '/time=/ {print substr($4,0,length($4)-3) }' < ping.log
### passing shell parameter to awk scripts
svn st ${dirs} | awk -F" " -v ac="${*}" ' /'${predicate}'/ && (index($2,"\\")==0 || gsub(/\\/,"/",$2)) {print ac, $2}'
### change Output Row Separator
awk ' BEGIN { ORS = " " } { print }'
3.4. sed
ll | sed '/Stores\|Test\|^-\|^t/d' | wc smbclient -L \\\\hangzhou2\\twitters -U foobar | sed '/Hangzhou20/,$d' export BEA_IP=`/sbin/ifconfig eth0 | sed '/inet addr/!d;s/.*addr:\([^ ]\+\).*/\1/g'`
3.5. cp
cp filename{,.bak}
4. Sample Section B
4.1. while
while getopts ":ab:c" opt; do
case $opt in
a ) echo "I am a" ;;
b ) echo $OPTARG ;;
c ) echo "i am c" ;;
? ) echo "usage : $0 [-a] [-b barg] [-c] args .."
exit 1 ;;
esac
shift $(($OPTIND - 1))
done
4.2. for
for s in $(echo $string | sed "s/;/ /g"); do
echo $s
done
for (( i=1; i<=$n; i++ )) do
ls -1 "${file_pattern}" | sed 's#\(.*\)\(.xml\).bak#cp & \1'"_$i"'\2#g'
done
4.3. case
case $action in
backup ) tar cfj "$backup_dir/$bakfile" * --exclude "jobs/*/workspace" ;;
rest | restore ) tar xfj $backup_dir/$bakfile ;;
* ) echo "all supported actions: backup | rest[ore]" ; exit 0 ;;
esac