Bash – Reading Command Line Arguments

Background

Let us validate and display the command line arguments passed to our bash script.

 

Code

Outline

  1. Functions
    • validateArguments
      • declare variables
        • rc =0
      • Access global variable ( argsAsArray)
        • get handle to array variable => ( (“${argsAsArray[@]}”)
        • get number of array entries => {#arrayArgs[@]}
      • If number of entries in array does not match expected value
        • set rc =1
        • display help banner
        • abort script
      • return rc
    • cmdLineArgsDisplay
      • Access global variable ( argsAsArray)
        • get handle to array variable => ( (“${argsAsArray[@]}”)
        • get number of array entries => {#arrayArgs[@]}
      • Use for loop to iterate array
        • Access each element using array variable args and position (i)
          • ${args[i_]}
    • cmdLineArgsDisplayRef
      • Access array variable based on function parameter passed by reference
        • -n
          • declare -n args ( declared named argument )
          • accept named argument ( $1 )
    • Main

 

Script


#!/bin/bash

function validateArguments {

   declare arrayArgs
   declare numberofArgs=0
   declare numberofExpectedArgs=0
   declare rc=0

 
   #get number of expected args  
   numberofExpectedArgs=$1

   #store arguments in a special array 
   arrayArgs=("${argsAsArray[@]}") 

   #get number of elements
   numberofArgs=${#arrayArgs[@]}


   rc=0

   if [ $numberofArgs -ne $numberofExpectedArgs ]
   then

       rc=1

   fi

   if [ $rc -ne 0 ]
   then

	printf "Usage: %s: filename \n" $(basename $0) >&2; 

	printf "\tCurrent Function %s \n" $FUNCNAME

	printf "\tNumber of Arguments passed in %d \n" $numberofArgs

        printf "\tNumber of Arguments expected %d \n" $numberofExpectedArgs

	exit 1

   fi

   return $rc


}


function cmdLineArgsDisplay {

   declare args	
   declare dta_=0

   dta_=0

   #copy the array in another one
   #https://stackoverflow.com/questions/19417015/how-to-copy-an-array-in-bash

   #store arguments in a special array 
   args=("${argsAsArray[@]}")

   #get number of elements
   n_=${#args[@]}
   

   for (( i_=0;i_<n_;i_++)); do 
   
	dta_=${args[i_]}

	printf "\tEntry $i_) $dta_ \n"


   done	

}

function cmdLineArgsDisplayRef {

  declare i_
  declare n_  
  declare -n args
  declare dta_

  args=$1

  #get number of elements
  n_=${#args[@]}


 for (( i_=0;i_<n_;i_++)); do

        dta_=${args[i_]}

        printf "\tEntry $i_) $dta_ \n"

	
 done


}

declare rc=0

declare numberofArgsExpected

declare arrayCmdLineArgs_

#save number of expected args
numberofExpectedArgs=1

#store $* in string
argsAsString=("$*")

#store #@ in an array
argsAsArray=("$@")

#get number of elements
numberofArgs=${#argsAsArray[@]}

#validate arguments
validateArguments $numberofExpectedArgs 

#capture return code of last operation
rc=$?

#echo "\$rc is $rc"

if [ $rc -ne 0 ]
then

   printf "Number of Arguments %s does not match expected count of %s \n" $# $numberofExpectedArgs 	
   exit

fi

echo ""
echo "Display args passed as value"
echo "----------------------------"

#pass arguments as value
cmdLineArgsDisplay $argsAsArray

echo ""
echo "Display args passed as ref"
echo "--------------------------"

#pass arguments as reference
cmdLineArgsDisplayRef argsAsArray

 

Invoke

Invoke – No Arguments passed in

Code


sh ./commandLine.sh 

Output

Image

Output – Text

 

>./commandLine.sh Usage: commandLine.sh: filename 

Current Function validateArguments 

Number of Arguments passed in 0 

Number of Arguments expected 1 

Source Code

DanielAdeniji/commandLineArguments.sh

Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s