Bash scripting and text tools — Bird's Linux Private Kitchen notes

Contents

  1. 1. bash
    1. 1.1. type
    2. 1.2. env
    3. 1.3. export
    4. 1.4. declare
    5. 1.5. ulimit
    6. 1.6. read
    7. 1.7. delete (parameter expansion)
    8. 1.8. substitute
    9. 1.9. initialize
    10. 1.10. alias&unalias
    11. 1.11. history
    12. 1.12. PS1
    13. 1.13. login
    14. 1.14. source
    15. 1.15. grep
    16. 1.16. egrep
    17. 1.17. sed
    18. 1.18. awk
    19. 1.19. cut
    20. 1.20. diff
    21. 1.21. cmp
    22. 1.22. patch
  2. 2. shell script
    1. 2.1. test
    2. 2.2. seq
    3. 2.3. arithmetic
    4. 2.4. positional variables
    5. 2.5. shift
    6. 2.6. exit status
    7. 2.7. sh
    8. 2.8. control flow

Notes from Bird’s Linux Private Kitchen (3rd edition), kept for quick review.

bash

type

  • -t # command type
  • -a # every matching name on PATH

env

  • print environment

export

  • put a shell variable into the environment for child processes

declare

  • declare -i num=$RANDOM*10/32768 # random digit 0-9
  • -x variable # export to environment
  • -r variable # read-only
  • +x variable # stop exporting (does not clear -a or -r)

ulimit

  • -a show limits

read

  • -p “prompt” variable
  • -t 30 variable # timeout seconds

delete (parameter expansion)

  • echo ${PATH#/*:} # strip shortest match from left
  • echo ${PATH##/*:} # strip longest match from left
  • echo ${PATH%/*bin} # strip shortest match from right
  • echo ${PATH%%/*bin} # strip longest match from right

substitute

  • echo ${PATH/sbin/SBIN} # first match left to right
  • echo ${PATH//sbin/SBIN} # all matches

initialize

  • new_var=${old_var-default_var} # use default when old_var unset
  • new_var=${old_var:-default_var} # default when unset or empty
  • new_var=${old_var=default_var} # same, and assign default to old_var
  • new_var=${old_var:=default_var} # same with := semantics

alias&unalias

history

  • !num # rerun by number
  • !! # previous command
  • !cmd # latest command starting with cmd

PS1

  • PS1=’[\u@\h \t \W]$ ‘ # prompt layout

login

  • /etc/issue # pre-login banner

  • /etc/motd # post-login message

  • /etc/profile # system-wide login

  • ~/.bash_profile | ~/.bash_login | ~/.profile # user login

  • interactive shells read ~/.bashrc last — put personal tweaks there

source

  • source ~/.bashrc # apply config in current shell
  • . ~/.bashrc # same

grep

  • cat file | grep -n -A2 -B3 ‘str’ # match plus 3 lines before, 2 after

  • cat file | grep -vn ‘str’ # invert match

  • cat file | grep -in ‘str’ # case insensitive

  • cat file | grep -n ‘[^[:lower:]]ar’ # non-lowercase then ar; [:lower:] like [a-z]

  • grep -l ‘*‘ $(find /etc/ -type f) # files under /etc containing *

  • find /etc/ -type f|xargs -n 10 grep ‘*‘ # same, 10 files per grep batch

egrep

  • egrep -v ‘^$|^#’ file # drop comments and blank lines

sed

  • cat -n file|sed ‘2,5d’ # delete lines 2-5

  • cat -n file|sed ‘2a line after row 2’ # append after line

  • cat -n file|sed ‘2i line before row 2’ # insert before line

  • sed -n ‘2 w output.txt’ file # write matching lines to output.txt

  • cat -n file|sed ‘2,5c replacement text’ # replace lines

  • sed -n ‘2,5p’ file # print line range

  • cat -n t |sed -n ‘2~5p’ # print from line 2 every 5th line

  • cat -n t |sed -n ‘/search/,+5p’ # from match, print five more lines

  • cat -n t |sed ‘s/search/replace/g’ # global substitute

  • cat -n t |sed ‘/^$/d’ # drop blank lines

  • sed -i ‘s/.$/!/g’ file # in-place: trailing . to !

  • sed ‘s/^/ls -l /e’ files # substitute and execute

  • sed -i ‘$a new line at EOF’ file # append at end of file

  • sed -n -f sed-cmd-file file # run sed commands from file

  • cat file|sed -e ‘4d’ -e ‘6c replace line 6’ # multiple commands

  • sed ‘{
    s/Google/Github/
    s/Git/git/
    }’ file # same, script block

  • sed ‘s/^.*/<&>/‘ file # wrap line in <&>; & is matched text

  • sed ‘s/Paul|Addy/[&]/g’ file # | must escape in basic sed regex

  • echo “string”|sed ‘s/string/S\LTRING/‘ # \L lowercase rest; \l first char only

  • echo “string”|sed ‘s/string/\USTRING/‘ # \U uppercase rest; \u first char only

  • echo “string”|sed ‘s/string/\Us\Etring/‘ # \E ends \L or \U

  • sed –in-place=bak ‘s/Ian/IAN/‘ source.txt # edit with backup
    sed -ibak ‘s/Ian/IAN/‘ source.txt

  • sed ‘y/AB/ab/‘ file # transliterate characters

awk

  • last -5|awk '{print $1 "\t" $3}’ # user and IP columns

  • last -5|awk '{print $1 "\t" line:"NR" "\t" col:"NF"}' # add line and field counts

  • last -5|awk '{print $1 "\t line:"NR "\t col:"NF "\t delimiter:"FS}' # built-ins

  • cat /etc/passwd|awk 'BEGIN{FS=":"} $3<10 {print $1 "\t" $3}' # UID < 10

  • cat pay.txt|awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"};NR>=2{total=$2+$3+$4;printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,total}' # column totals; ; can be newline

cut

  • -d ‘sep’ -fN # default tab; N = field index

diff

  • -b # ignore whitespace-only changes

cmp

  • -s # report every differing byte

patch

  • diff -Naur t t2 >t.patch # create patch
  • patch -pN < file.patch # apply; N strips leading path components
  • patch -R -pN < file.patch # reverse patch

shell script

test

  • test -z str # string empty
  • [ -z "$HOME" ];echo $? # mind the spaces
  • test -e filename && echo 'exist' || echo 'not exist' # file exists

seq

  • seq start end # inclusive integer range

arithmetic

  • $((cal_expr))

positional variables

  • $0 # script name
  • $# # argument count
  • $@ # all arguments
  • $1 # first argument
  • $$ # shell PID

shift

  • shift N # drop first N arguments

exit status

  • ping -c 1 -w 1 192.168.1.1 &>/dev/null && result=0 || result=1

sh

  • sh -n file.sh # syntax check
  • sh -v file.sh # trace lines before execution
  • sh -x file.sh # print commands as they run

control flow

  • if

      if ;then
      fi
      ---
      if ;then
      elif ;then
      fi
      ---
      if ;then else
      fi
    
  • case

      case $var in
      "case1")
          ...
          ;;
      *)  #default
          ...
          ;;
      esac
    
  • while

      while [cond]
      do
          ...
      done
    
  • until

      until [cond]
      do
          ...
      done
    
  • for

      for v in $arrs
      do
          ...
      done
      ---
      for((i=1;i<$nu;i=i+1))
      do
          ...
      done
    
Questions? Leave a comment below or email: newbvirgil@gmail.com