title sh/bash/ksh reference. author GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh vim:ft=sh: ============================================================ section Quick_sh_ksh_bash moshtag=shell,ksh,sh,case,for,select,until,esac case word in [ ( pattern [ | pattern ] ... ) list ;; ] ... esac select name [ in word ] ; do list ; done for name [ in word ] ; do list ; done if list; then list; [ elif list; then list; ] ... [ else list; ] while list; do list; done until list; do list; done [ function ] name () { list; } ============================================================ moshtag=FD,file_descriptors,dup moshtag=>,>>,<,<<,>&,<&,<&-,>&-,<> File Descriptors in Bourne shell. F is filename, &F is filehandle. fd 0 stdin, '-'. fd 1 stdout fd 2 stderr > F Redirect stdout to file F. E> F Redirect fd E to file F. Only param and command subst, Not filename or blank interpretations. Eg. $ >b cat a .. copies a to b. Eg. $ echo x > *.c .. creates '*.c' E<>F Redirect E to file F in read-write mode E>&D fd D is duped and result is fd E (default is stdout). stderr to stdout, dup fd1 into fd2. E<&D fd D is duped and result is fd E (default is stdin). <&- close stdin >&- close stdout E<&- close fd E (default is stdin /0). E>&- close fd E (default is stdout/1). ============================================================ Shell ReDirection Examples: moshtag=redirection Order of redirection is always left to right. Eg. $ gcc x.c 2>&1 | grep errors Eg. $ gcc x.c > log 2>&1 1. First send stdout 1> to ./log 2. Then send stderr 2> to &1 (ie. ./log) Eg. $ exec 5>./config.log ; echo "into config.log" 1>&5 Eg. $ cmd 3>&1 1>&2 2>&3 3>&- # Swap stdout and stderr for cmd 1. First save stdout as &3. 2. Next send stdout to stderr 3. Send stderr to &3 (stdout) 4. close &3 Eg. stderr_output=`cmd 3>&1 1>&2 2>&3 3>&-` ----------- -------------------------------------- Redirection Meaning ----------- -------------------------------------- 0/dev/null Discard STDOUT 2>/dev/null Discard STDERR 2>&1 Send STDERR to STDOUT instead 2>&- Close STDERR (not recommended) 3<>/dev/tty Open fd 3 to /dev/tty in read-write mode ----------- -------------------------------------- ============================================================ PatternMatch: One SquareParen and Two Equals. moshtag=[ eg. if [ "${FILE}" == "./*" && -r "${FILE}" ] ;then echo "$PWD/$FILE readable" fi ============================================================ Expr: (see man expr). moshtag=expr $(expr string : 're') .. string =~ m/^re/ .. returns strlen of match. .. string =~ m/re\(re\)re/ .. \1 is returned, else 0. ============================================================ $(substr string from len) $(index string substr) $(length string) ============================================================ moshtag=expr-regexp eg. A=`expr $A + 1` expr 'src-mosh.c' : '.*-\(.*\)\.c' .. returns 'mosh'. ============================================================ moshtag=toupper,lolower,uc,lc Uppercasing var value: word=$(echo $word | tr [a-z] [A-Z]) ============================================================ moshtag=base_n ksh base convertor (base 2..36 ok): typeset -i16 x=30 ;echo $x # prints 16#1e, x in hex/base 16. typeset -i2 x=12#20 ;echo $x # prints x (12#20=24) in binary 2#11000. ============================================================ moshtag=yesterday_tz Get date two days ago: TZ=GMT+48 date ============================================================ moshtag=subst Variable Subst: ${VAR/pat/str} ${VAR//pat/str} ${VAR/#pat/str} ${VAR/%pat/str} $((ArithExpr)) $[ArithExpr] DIR="${PWD//[\/:]/_}" # Convert :/ to _. P="${PWD//\//_}"; Q="${P//:/_}"; .. c:/mohsin/perl -> c__mohsin_perl Local file to url: F="file://$(hostname)/${F/:/}" Eg. echo "${X//\//\\}" .. $X =~ s,/,\\,g; .. slash to backslashes. ============================================================ moshtag=##,%% Variables: % %% Remove tail, eg. TTY=${TTY%/dev/ttyp} ie. /dev/ttyp0 => /dev/ttyp # ## Remove head, eg. TTY=${TTY#/dev/ttyp} ie. /dev/ttyp0 => 0 Examples: BASENAME=${FILE%%.*} EXTENSION=${FILE##*.} for i in *.c ; do if [[ ! -r "${i%%.*}.o" ]] ; then # file.c => file.o gcc $i fi done ${VAR:offset:len} ${VAR:offset} ============================================================ moshtag=lasterror,$? cmp -x file1 file2; echo "exit status=$?" # lasterror $? : "${1:?'Need Arg1'}" : "${X:?}" .. check X is set. : ${T:=DefaultT} : ${X:-Default3rd} TMP=${$WINDIR+c:/tmp} .. c:/tmp if $WINDIR. ============================================================ moshtag=interactive_shell Interactive shells: Run this as "x.ksh" and ". x.ksh" case $- in # (( *i*) INTERACTIVE=1;; *) INTERACTIVE=0;; esac if [ "$-" == "*i*" ] ; then echo interactive_shell ; fi ============================================================ moshtag=select_ask Select: .. Example .. IFS,Select menu, default. ask_diff(){ TAB=' ' DIFF_CHOICES="diff -wb${TAB}vim -od${TAB}vim -d${TAB}xdiff\ ${TAB}ediff${TAB}windiff${TAB}cp -vi${TAB}:" OFS=$IFS IFS=$TAB select diff in $DIFF_CHOICES ; do echo diff=${diff:='windiff'} break done IFS=$OFS } ============================================================ moshtag=ediff ediff(){ ${EMACS:=emacs} --eval "(ediff-files \"$1\" \"$2\")" } ============================================================ moshtag=ksh_args,CMDLINEARG,opt.txt # ksh tips CMD=${0##*/} CMD=${0##*\\} CMD=${CMD%.*} # Evaluate overriding cmdline args. # for i ; do ; echo $i ; done # === for i in $* ; do; echo $i; done for CMDLINEARG # remember $* do case $CMDLINEARG in [A-Z]*=*) echo echo "Overriding variable, eval $CMDLINEARG" eval "$CMDLINEARG" ;; -*) echo " USAGE: $CMD [OPTIONS] ... Does x y z. OPTIONS: " exit esac done ============================================================