LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2009, 10:36 PM   #1
baigmd
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
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
 
Old 10-27-2009, 12:12 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
'$*' 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

if test -f "$i"
then
echo "-> Argument $i is a file"
ls -l $i
else
echo "-> Argument $i either is not a regular file, or doesn't exist"
fi
Finally, please use [code][/code] tags around your code, for readability and to preserve formatting.
 
Old 10-27-2009, 12:41 AM   #3
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
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" ]
do
if test -f "$1"
then
echo "-> Argument $1 is a file"
ls -l "$1"
else
echo "-> Argument $1 either is not a regular file, or doesn't exist"
fi
shift
done
Just another way of doing the same thing...
Cheers
indiajoe
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fork() not giving desired result anshu33331 Programming 4 07-29-2009 01:48 PM
am setting radius+ldap and its giving me this result jerykwajaffa Linux - Networking 1 09-08-2008 09:17 AM
Script arguments jnusa Programming 1 12-20-2004 01:38 AM
"who" not giving the expected result rblampain Linux - Newbie 2 11-17-2004 04:06 AM
"ifconfig -a" giving odd result balajipriya Linux - Networking 0 08-11-2004 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration