Tuesday, August 30, 2016

Linux Bash Shell Script Command Line Arguments List and Count


How the code below works:

The code lists and count all the command line arguments passed into the program.

Code To Count Arguments:

Save the code in a file named "CmdArgumentsLister.sh".
#!/bin/bash
showargs(){
a=1
for i in $*
do
echo "The $a number arg is $i"
a=$(( $a + 1 ))
done
}
echo "Listing all command line arguments."
showargs $*
echo "Total:$#"
echo "Listing End"

Running the Code:

Just type the code below in terminal. Make sure to cd into that directory if you have not opened the terminal in that directory.

bash CmdArgumentsLister.sh a b c d

No comments: