Tuesday, August 30, 2016
Linux Bash Shell Script Command Line Arguments List and Count
August 30, 2016
bash
,
code
,
count command line arguments
,
example
,
linux
,
shell script
,
tutorial
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".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment