Thursday, 31 October 2013

How to find the last argument passed to a Shell Script:-

To find the last argument passed to a Shell Script:

$1 - first arguments.

$* / $@ -  all arguments.

$# - number of arguments.

Here is a script to find the last argument passed,

# cat arguments.sh
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No Arguments supplied"
else
echo $* > .ags
sed -e 's/ /\n/g' .ags | tac | head -n1 > .ga
echo "Last Argument is: `cat .ga`"
fi

Output:

# ./arguments.sh
No Arguments supplied

# ./arguments.sh testing for the last argument value
Last Argument is: value

No comments:

Post a Comment