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

Comments

Popular posts from this blog

Resolved: DOCKER: Error response from daemon: Could not attach to network / rpc error: code = 7 desc = network not manually attachable.

yum failed 6 times. Cannot continue!

unexpectedly shrunk window (repaired) in dmesg log - TCP Peer