My script is not giving result for 2 or more arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My script is not giving result for 2 or more arguments
# 1  
Old 10-27-2009
My script is not giving result for 2 or more arguments

Hi,

I am new to shell scripting and my below script is not giving result for 2 or more arguments. Can anyone help pls.
Code:
#!/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


Last edited by pludi; 10-27-2009 at 04:11 AM.. Reason: code tags, please..
# 2  
Old 10-27-2009
Code:
array=($@);
for ((i=0 ; i < ${#array} ; i++))
do
        echo $i;
        echo ${array[$i]};
done

replace the echo with ur test code.

HTH,
PL
# 3  
Old 10-27-2009
Doesn't work

---------- Post updated at 02:30 AM ---------- Previous update was at 02:17 AM ----------

I got the solution ....thanks...sharing with you guys
Code:
#!/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"
array=("$@")
for ((i=0 ; i < $# ; i++))
do
if test -f "${array[$i]}"
then  
echo "-> Argument ${array[$i]} is a file"
ls -l ${array[$i]}
else
  echo "-> Argument ${array[$i]} either is not a regular file, or doesn't exist"
fi
done


Last edited by pludi; 10-27-2009 at 04:11 AM.. Reason: code tags, please...
# 4  
Old 10-27-2009
Quote:
Originally Posted by baigmd
Doesn't work

---------- Post updated at 02:30 AM ---------- Previous update was at 02:17 AM ----------

I got the solution ....thanks...sharing with you guys
Code:
#!/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"
array=("$@")
for ((i=0 ; i < $# ; i++))
do
if test -f "${array[$i]}"
then  
echo "-> Argument ${array[$i]} is a file"
ls -l ${array[$i]}
else
  echo "-> Argument ${array[$i]} either is not a regular file, or doesn't exist"
fi
done

Should be:
Code:
for ((i=0 ; i < ${#array} ; i++))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

3. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

4. Shell Programming and Scripting

Script not giving o/p

Hi Below is snippet from script which is not giving the o/p.script name is alarm.sh #!/bin/sh out=`awk '(NR>1) {print $9;exit}' alarm` echo $a however when i simply run the above command i an getting the o/p $ out=`awk '(NR>1) {print $9;exit}' alarm` $ echo $a 6... (2 Replies)
Discussion started by: scriptor
2 Replies

5. Shell Programming and Scripting

Monitoring Sript giving random end result

Hi Guys, I am developing a script to monitor GUI based FileNet Component "Component Manager" which logs it's running status in a log file. Log file is a huge file so in script I put last 300 lines of log file in seperate file and run script every 5 minutes. I am searching the string... (2 Replies)
Discussion started by: dhirajdsharma
2 Replies

6. AIX

errpt not giving a result

my system get rebooted by its self after its came up i try to check the error log P690/>errpt | more Cannot open error message catalog /usr/lib/nls/msg/en_US/codepoint.cat. The error report will still run, but it will not have explanatory messages P690/>ls -lrt... (1 Reply)
Discussion started by: thecobra151
1 Replies

7. Shell Programming and Scripting

$0 not giving script name

Why does $0 return the word usage rather than the script name when used in a function? Baffeled on this one, any help appreciated. usage() { echo "$0 -cs <number of batches>\n" echo "$0 -c 4" echo "$0 -s 4" # echo "-c = Create" # echo "-s = Submit\n" exit 1 } $... (1 Reply)
Discussion started by: nhatch
1 Replies

8. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

9. UNIX for Dummies Questions & Answers

Script giving different result on Linux compared to Unix

Hi I have a script executing fine in Unix but in linux I am getting different result. I have three files under /local/home/temp/Gen test.sh list.txt shst.txt Contents of test.sh -------------------------- #!/bin/ksh K=0; SCRIPT_DIR=/local/home/temp/Gen cat... (2 Replies)
Discussion started by: malavm
2 Replies

10. Shell Programming and Scripting

Giving input through script

Script 1.ksh ========= /home/adw/a.ksh << ** a b Script 1.ksh is working fine even without ending "**" Script 2.ksh ========= if then /home/adw/a.ksh << ** a b fi But the script 2.ksh is giving error "syntax error : `<<' unmatched". Is it bcoz of fi. (1 Reply)
Discussion started by: radhika03
1 Replies
Login or Register to Ask a Question