Thursday, 25 June 2020

shell script

1. sha-bang #!

#!/bin/bash
# Proper header for a Bash script.


2. define all variables (UpperCase) at top at refer them later using $


3. command line argument
  • $0: command itself
  • $1: 1st argument

4. `pwd`.    //everything between `` is evaluated first


5. $?           //the exit code of previous command is stored in it


6. $(command)      //run command and return value as variable


7. i/o redirection
  • 0, 1, 2                    //stdin, stdout, stderr
  • 1>filename            //redirect stdout to file
  • 2>&1                     //redirect stderr to stdout
  • >                            //redirect, overwrite
  • >>                          //redirect append

8. sed (stream editor)

sed -n           //don't print input, always use
sed -i            //modify file in-place

echo "hello sed" | sed 's/sed/world/'                                       //hello world
sed 's+http://+https://www.cyberciti.biz+g' input.txt             //sed allow delimiter other than /
sed -i -e '/FOO/s/love/sick/' input.txt.                                    //if the line contain FOO
sed -i 's/foo/bar/gI' hello.txt                                                   //'I' to match all cases of foo
sed -n -e 's/^.*ch\([a-zA-Z0-9-]\{1,\}\).*$/\1/p' input.txt.     //+ does not work in sed, use \{1,\}


9. special character
  • $                                              //access variable
  • ${HOME}                              //return value of HOME
  • $(command)                           //return output of command, maybe separated by space !!!  
  • "$(command)"                        //return output of command as a whole
  • [   ]                                          //condition, !!! need space after [ and before ], i.e. [ 1 -lt 2 ] 
  • [ -z "$var" ]                             //$var is empty

10. awk
  • program structure

    BEGIN {awk-commands}
    /pattern/ {awk-commands}      // body
    END {awk-commands}

  • variable
    $0: a special variable in awk that represents the entire line

  • command
    awk -f command.awk marks.txt
    awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt

reference
1. special char in shell scripting
2. tldp sed
3. tutorialspoint regex with sed
4. how-to-use-sed-to-find-and-replace-text-in-files