Bash Common Tasks

Submitted by code_admin on Fri, 07/20/2018 - 12:51

Check number of Params

  1. #Check the input paramaters are correct
  2. if [[ $# != 2 ]]
  3. then
  4.   echo "ERROR!!!"
  5.   exit 1
  6. fi

File Exists File Exist

  1. if  [[ -f ${LIVECUSTOMPLL} ]]
  2. then
  3.   echo "File Exists"
  4. else
  5.   echo "File Missing"
  6.   exit 1
  7. fi

Dir Exists

  1. if [ -d "${DIR_SVN_COMP}/${PIPNAME}" ]; then
  2.     echo "Sucess"
  3. else
  4.     echo "Fail"
  5.     echo "Please check the pip name is correct"
  6.     exit 1
  7. fi

Write Permissions

  1. if [ -w "${DIR_SVN_COMP}/${PIPNAME}" ]; then
  2.     echo "Sucess"
  3. else
  4.     echo "Fail"
  5.     echo "You can't write to this dir"
  6.     exit 1
  7. fi

Enviroment Var contains something

  1. if [[ $(echo ${TWO_TASK} | grep -i "SOMETHING" | wc -l ) = 1 ]]
  2. then
  3.   echo "TWO_TASK contains SOMETHING"
  4.   exit 1
  5. fi

Param has one of two values

  1. parm_two_ok=0
  2. for X in "VALUE1" "VALUE2"
  3. do
  4.   if [[ "${X}" = "${2}" ]]
  5.   then
  6.     parm_two_ok=1
  7.   fi      
  8. done
  9. if [[ ${parm_two_ok} = 0 ]]
  10.   then
  11.   echo "Parameter 2 is not set correctly"
  12.   echo "It should be VALUE1 or VALUE2 only"
  13.   exit 1
  14. fi

Compare two files

  1. CURSETUP=X
  2. cmp ${FILEONE} ${FILETWO} 1>/dev/null 2>&1
  3. if [[ $? -eq 1 ]]
  4. then
  5.   echo "They are diffrent"
  6. else
  7.   echo "Files match"
  8. fi

Substring

  1. string="this is a substring test"
  2. substring=`echo $string| cut -c 11-19`
  3. echo "String:"$string
  4. echo "subString:"$substring

after it is run $substring = substring

Command as VAR 2

  1. string="this is a substring test"
  2. substring=$(echo $string| cut -c 11-19)
  3. echo "String:"$string
  4. echo "subString:"$substring

Last Command Failed

  1. T_CMD="svn update $AIA_HOME/AIAMetaData --non-interactive"
  2. ${T_CMD}
  3. if [ 0 != $? ]; then
  4.     echo "Error - Was unable to update the AIAMetaData directory"
  5.     echo " ${T_CMD} failed"
  6.     exit 1
  7. fi

Log running user

  1. echo  $(date) - Start > /tmp/some.log
  2. echo  $(date) - Running as $(whoami) >> /tmp/some.log
  3. echo  $(date) - End >> /tmp/some.log

Mutli dimensional arrays

  1. #!/bin/bash
  2. #make it exit on error
  3. set -e
  4.  
  5. FILES=()
  6. FILES_N=0
  7. FILES_GRPID=()
  8. FILES_ARTID=()
  9. FILES_VERSION=()
  10.  
  11. function AddFile {
  12.         FILES_N=$((${FILES_N} + 1))
  13.         FILES+=(${FILES_N})
  14.         FILES_GRPID[${FILES_N}]=${1}
  15.         FILES_ARTID[${FILES_N}]=${2}
  16.         FILES_VERSION[${FILES_N}]=${3}
  17. }
  18.  
  19. AddFile Art1 Grp1 Ver1
  20. AddFile Art2 Grp2 Ver2
  21. AddFile Art3 Grp3 Ver3
  22. AddFile Art4 Grp4 Ver4
  23. AddFile Art5 Grp5 Ver5
  24.  
  25. echo "Release from Maven start"
  26.  
  27. for i in "${FILES[@]}"
  28. do
  29.    echo "Grp:=${FILES_GRPID[${i}]}"
  30.    echo "art:=${FILES_ARTID[${i}]}"
  31.    echo "ver:=${FILES_VERSION[${i}]}"
  32.    echo "------------"
  33. done
  34.  
  35. echo "Release from Maven end"
  36.  
  37. exit 0

Repeat a command based on output from another command

Command

  1. for a in $(ls); do echo $a; done
  2. for a in $(docker images | grep metcarob/dockjob | awk '{print $3}'); do docker rmi $a; done

Select 3rd column of output

  1. docker images | grep metcarob/dockjob | awk '{print $3}'

Search in file displaying preceeding or following text

With lines after

  1. echo $FILENAME | grep -A 3 SearchText

With lines before

  1. echo $FILENAME | grep -B 3 SearchText

Links

http://www.arachnoid.com/linux/shell_programming.html
http://stackoverflow.com/questions/428109/extract-substring-in-bash
http://tldp.org/LDP/abs/html/fto.html - file test operators

Tags

RJM Article Type
Quick Reference