Get arguments from command line with names using BASH
How you can read command line arguments with names is shown in the following script. Create a file named, ‘command_line_names. sh’ and add the following code. Here, two arguments, X and Y are read by this script and print the sum of X and Y.
#!/bin/bash
for arg in "$@"
do
index=$(echo $arg | cut -f1 -d=)
val=$(echo $arg | cut -f2 -d=)
case $index in
X) x=$val;;
Y) y=$val;;
*)
esac
done
((result=x+y))
echo "X+Y=$result"
Run the file with bash command and with two command line arguments.
$ bash command_line_names X=45 Y=30
More bash examples::
@V3rtualfreenet
@V3rtualfreenet
@V3rtualfreenet
>>Click here to continue<<