My script not giving result with 2 or more arguments
Hi,
Can anyone help....my shell script is giving correct results with 2 or more arguments. 0 or 1 arguments it is working fine, Here is the script #!/bin/sh sname=$(basename $(readlink -nf $0)) echo "This is $sname, running at $(date)" echo "It is running on $(hostname)" echo "Script being run by" echo " User $(grep "^$USER:" /etc/passwd | cut -d: -f1)" echo " UID $(grep "^$USER:" /etc/passwd | cut -d: -f3)" echo " who is really $(grep "^$USER:" /etc/passwd | cut -d: -f5)" echo "" echo "Called with $# arguments" for ((i=0 ; i < $# ; i++)) do if test -f "$*" then echo "-> Argument $* is a file" ls -l $* else echo "-> Argument $* either is not a regular file, or doesn't exist" fi done |
'$*' means all arguments together as a single line. To echo all arguments as individual entries, use '$@'. As it is, your last section is testing the whole set of arguments every iteration of the loop.
Besides, you don't need to be so complex to loop through all the existing arguments. Code:
for i in "$@"; do |
Or.. you can also use the shift command
Hi,
You can also use the shift command to do the same.. As shown below. It is just the last part of your script.. Code:
while [ -n "$1" ] Cheers indiajoe |
All times are GMT -5. The time now is 06:31 PM. |