User:Feixie/Bash

[Advanced Bash-Scripting Guide]

Grocery
export TERM="xterm-color" export PS1='\e[0;32m$NICK$E_USER\e[m@\e[0;33m$GoodAlias\e[m:\e[0;36m$PWD>\e[m\n$ '
 * Bashrc

set -o vi function cd2 { case "$#" in    0) cd $HOME;;    1) cd $1 ;; 2) cd `pwd | sed "s/$1/$2/g"` ;;   *) echo "cd2: invalid number of arguments"; exit 1;; esac }

alias ls='ls --color=always' alias ll="ls -l" alias la="ls -la" alias cltmp='rm .*.swp' alias vdiff='vimdiff' alias d1='du -h --max-depth=1' alias d2='du -h --max-depth=2'

bash cmd/script executing procedure
Before the shell executes a command, it performs the following operations (check the manual for details):

1. Syntax analysis (Parsing)           # split parameters here 2. Brace expansion 3. Tilde expansion 4. Parameter and variable expansion    # substitute variables here 5. Command substitution 6. Arithmetic expansion 7. Word splitting 8. Filename expansion 9. Quote removal                       # process quotes here
 * misconceptions

array && shift
declare -a arr1 declare -a arr2=(`ls`)
 * array declaration and definition
 * shift N, used to shift argument $1 ... $N

quote behind bash script
feixie@kd185c1n8:~/bin $ a="echo haha > \'a\'" feixie@kd185c1n8:~/bin $ echo $a echo haha > \'a\' feixie@kd185c1n8:~/bin $ $a haha > \'a\' feixie@kd185c1n8:~/bin $ ls \'a\' ls: 'a': No such file or directory feixie@kd185c1n8:~/bin $ eval $a feixie@kd185c1n8:~/bin $ ls \'a\' 'a' feixie@kd185c1n8:~/bin $ set -x feixie@kd185c1n8:~/bin $ $a + echo haha '>' '\'\a\'\' haha > \'a\' feixie@kd185c1n8:~/bin $ eval $a + eval echo haha '>' '\'\a\'\' ++ echo haha bash: 'a': cannot overwrite existing file

expression calculation
a=1.2 b=1.3 c=$(echo "$a+$b"|bc)   #.... d=$(echo "$a*$b"|bc)   #.... e=$(echo "scale=5;$a/$b"|bc)   #.....scale=5........5. echo "$c $d $e"
 * example
 * 1) ! /bin/bash

echo "s(1)"|bc -l   #..sin(1).-l........
 * man bc
 * servinfo -s hostname -g alias

eval and func
function add { let local sum=$1+$2 eval "$3=$sum" } add 1 2 result echo $result
 * use eval to pass reference to function
 * eval + \$$ to de-reference
 * eval vv=\$${todo[1]};echo ${todo[1]} = $vv;;
 * use eval to accept a=b like parameters
 * a.sh a=b c=e

Pass variables to shell script

 * var=value shellscript

string manipulation

 * egrep 'abc|def' *
 * echo $a+$b | bc
 * ((sum=$a+$b)) or let sum=$a+$b
 * ${var:-default}, ${var:?comment}, ${var:=default}, ${var:+returnme}
 * ${var#regex_begin_short}, ${var##regex_begin_long}, ${var%regex_end_short}, ${var%%regex_end_long}
 * a&&b string comparison, ((a||b)) arithmatic comparison, $((expr)) expression expansion `expr `
 * ${#var}, ${#arr[n]}, ${#arr[@/*]}, read -a arr, ${str/regex/replace}, ${str//#regex/replace}, ${str%%regex}
 * getopts, OPTARG, OPTIND
 * sed '/todelete/d' file
 * tr -s '[:space:]'

getopts
60 while getopts "mcarh" flag 61 do 62  case $flag in 63     m)FLAG_M=Y;; 64     c)FLAG_C=Y;; 65    h)FLAG_H=Y;; 66     a)$pte_host=$OPTARG;; 67    r)filter=$filter_qa;; 68   esac 69 done 70 71 shift $(($OPTIND -1)) 72 73 if [ -n "$FLAG_H" ];then 74   printHelp 75   exit 76 fi

expr regex
7 function restore_file 8 { 9   orgi_file=`expr "$1" : "\(.*\)\.bk"` 10  if [ -n $orgi_file ];then 11    echo mv $1 $orgi_file 12    mv $1 $orgi_file 13  fi 14 }

pipe && argument
49 if [ -n "$1" ];then 50  while [ -n "$1" ] 51  do 52     process_afile $1 53    shift 54  done 55 else 56  while read -a filename 57  do 58     for afile in $filename 59    do 60       process_afile $afile 61    done 62  done 63 fi