get first through third char : cut -c1-3 get third char to end of line : cut -c3- match matt and not matthew : grep -w matt give count of matched lines : grep -c matt print lines that do NOT match : grep -v matt print line with string match : grep "matt baker" print a line match : grep ^"my name is matt"$ delete first 3 lines : sed -e 1,3d get rid of letter 'k' : sed -e s/k//g change matt to stephen : sed -e s/matt/stephen/g print only a line : sed -n 1p print a range of lines : sed -n 5-10p print a line based on a var : sed -n ${CNT}p change lower to upper case : tr [a-z] [A-Z] delete char : tr -d 's' change tabs to newlines : tr '\t' '\012' NOTE: ksh (Korn) can run pure sh (and mixed sh and ksh), but sh (Bourne) cannot recognize any ksh unique syntax. (Korn is a superset of Bourne.) Shell Executeable Prompt Best Usage Author Bourne /bin/sh $ programming Stephen Bourne C-Shell /bin/csh % interactive Bill Joy Korn /bin/ksh $ both David Korn testing csh: () sh: [] ksh: [], [[]], (()) [[]] - ascii (()) - integer Input/output stdin cmd < in_file #0 stdout cmd > out_file #1 stderr cmd 2> err_file #2 stdout and stderr together DIFFERENT FILES cmd > out_file 2> err_file SAME FILES cmd 2>&1 out_and_err_file stderr only output to pipe, but not stdout cmd 2>&1 > /dev/null | grep "pattern-from-stderr" #**OR** command 2> >(grep 'pattern-from-stderr') > /dev/null #**OR** command 2> >(grep -v pattern-from-stderr 1>&2) Pipes cmd | cmd Subshells (cmd;cmd) test NOTE: The word "test" and '[' are the same. "test -d /tmp" **is-equivelant-to** "[[ -d /tmp ]]" unary [[ -d /usr/tmp ]] some unary tests file -f, file is non-zero (size) -s directory -d null VAR -z non-null VAR -n strings [[ matt == matt ]] integers (( 1 == 3 )) loops for NODE in adm1 adm2 nms1 nms2 do ssh $NODE df done for PART in $(df | grep ^dev | awk '{print $1}') rsh $NODE chpt -q $PART > /tmp/$NODE_$PART done while #infinite loop while (( 1 )) ls; sleep 60 done Running a script ksh program_name or chmod 755 program_name programe_name debug methods ksh -[vx] prgm run that section of code by itself, line-by-line ksh [-x] $ (enter commands, execute after ) $ ^D print (like echo) print "hello" print "$VAR at $LINENO" style/philosophy put in file: description author date: create and modify os ver h/w type comment large sections of code and important and complex lines also comment logic areas/lines cleanup of files at the end of your script or trap (^C) most problems incurred when: cutting and pasting (var and file names) syntax (done, fi, esac; using = instead of ==) forgot to init var logic errors non-unique filenames: use $$ for PID so each file is unique (so you can run multiple instances of program) awk print first field awk '{print $1}' print if match (number of fiels on line is 6, print whole line awk '{if (NF == 6) print $0}' print percentage awk '{s=$1 / $2 * 100; print s}' sum/avg/max/min #pick a field - number 3 in this example CNT=3 echo "Max Min Sum Avg" awk '{input = $$CNT {if(input >= max) max = input} \ {if(input <= min) min = input} \ {sum += input} END \ {printf %6d %6d %6d %8.2f\n , \ max, min, sum, sum/NR}' use cut instead of awk (nor recommended) fields 1-3 cut -f 1-3 #colon is field separator cut -d: -f1-3 # # test for alpha char # if [[ $proceed == *[[:alpha:]]* ]] then echo "VAlue ${proceed} is invalid. Exiting......." exit 1 fi # # compile shell script # http://alnazmin.blogspot.com/2011/07/how-to-compile-bash-script-using-shc.html