Problem with command argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with command argument
# 1  
Old 10-01-2012
Problem with command argument

I'm having problem with taking in arguments and be able to use the content of the argument for something else.
In this case I need to take in user names and check if the person is online using finger or who

For example, my script name is getuser
in shell I type
getuser username1 username2
I want to be able to take username1 and check if he's online then take username2 and check if he's online.

I'm currently just echo ing the argument to understand how to increment.

Note that there may be more then 1 argument.

I know that echo $* will print all the arguments
echo $1 will print username1
echo $2 will print username2

but since I don't know how many arguments there may be, is there a way to increment the $n instead of $1,$2,$3....etc


my code:
Code:
#!/usr/dt/bin/dtksh

echo currently logged on:

for((n=1; n<=$#; n+=1))
do
    exec 2>errs
    echo $n         //this just shows the number in n
    echo $(n+1)       //this won't work
    echo $(n = `expr $n+1`)     //this won't work either... 
done

# 2  
Old 10-01-2012
Code:
for((n=1; n<=$#; n+=1))
do
 eval 'echo $'$n
done

# 3  
Old 10-01-2012
Quote:
Originally Posted by elixir_sinari
Code:
for((n=1; n<=$#; n+=1))
do
 eval 'echo $'$n
done

Sweet.. exactly what I needed.
However, I don't recall my prof. taught us eval.
Hopefully he won't mind me using it in hw
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

2. Shell Programming and Scripting

Argument list too long problem

I have a huge set of files (with extension .common) in my directory around 2 million. When I run this script on my Linux with BASH, I get /bin/awk: Argument list too long awk -F'\t' ' NR == FNR { a=NR } NR != FNR { sub(".common", "", FILENAME) print a, FILENAME, $1 } '... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

3. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

4. Shell Programming and Scripting

argument problem

abc.sh $1 1 this a shell script inside another shell script .how $1 gets the value??this is only code in that shell script! (2 Replies)
Discussion started by: samta
2 Replies

5. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

6. Shell Programming and Scripting

Problem in argument passing

Hell all, i have a problem in argument passing. print() { a=$1 b=$2 c=$3 echo $a echo $b echo $c } x="1 2 3" y="4 5 6" z="7 8 9" print $x $y $z. (4 Replies)
Discussion started by: tsaravanan
4 Replies

7. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

8. Shell Programming and Scripting

problem with spaces and argument parsing

public class HelloWorld { public static void main(String args) { System.out.println("Welcome, master"); } } and I compiled using javac HelloWorld.java ] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorld Then it works... (8 Replies)
Discussion started by: fabulous2
8 Replies

9. Shell Programming and Scripting

Problem when passing argument to a shell script

Hi all, I'm new to Shell scripting. In my shell script for Bourne shell, the script accepts a date parameter which is optional. If the value is supplied, the supplied value should be assigned to a variable. If not, the current date will be assigned to the variable. My script is like this. #!... (9 Replies)
Discussion started by: sumesh.abraham
9 Replies

10. Shell Programming and Scripting

Problem with Argument Passing

Greetings, I am wrapping the monitoring commands like vmstat, sar, iostat and call via arguments I want ./unix_stats.sh -v vmstat -p <SEC> -d <Duration> to give vmstat values, and similarly iostat etc.,. Also if I give ./unix_stats.sh -v vmstat -i iostat -p <SEC> -d <Duration> should give... (4 Replies)
Discussion started by: A_Rod
4 Replies
Login or Register to Ask a Question