How to call arguments with variable in a script??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to call arguments with variable in a script??
# 1  
Old 05-06-2010
How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable.

For example:

sh script.sh yes no good bad
Code:
x=$#

while [ x -gt 0 ]
do
echo (last argument, then second last etc until first argument) let x=($x-1)
done

should print out

bad
good
no
yes

Thanks for any help with thisSmilie
# 2  
Old 05-06-2010
try this...

Code:
for x in $*; do echo $x; done

# 3  
Old 05-06-2010
I didn't even think of that, simple but ingenious. Thanks!
# 4  
Old 05-06-2010
Code:
x=$#

while [ $x -gt 0 ]
do
eval echo \${$x}
let x=x-1
done

# 5  
Old 05-06-2010
Code:
#! /bin/bash

ARGS=( "$@" )
X=$(( $#-1 ))
while [ $X -ge 0 ]
do
  echo "${ARGS[$X]}"
  X=$(( $X-1 ))
done

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call same function using 2 different arguments

I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you! cat backup.sh #!/bin/bash function usage { echo "USAGE: $(basename $0)... (6 Replies)
Discussion started by: mbak
6 Replies

2. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

3. Shell Programming and Scripting

Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function? calc_pref_avail $database $service Best regards, Vishal (7 Replies)
Discussion started by: Vishal_dba
7 Replies

4. Shell Programming and Scripting

Script variable help, Varying number of arguments to excute script

Hi Guy's. Hopefully someone can help me with what I am trying to archieve. So situation currently is, I have a script already setup however I have another script that sits infront of it. The main script basically goes and searchs multiple platforms for a list of entered data. In... (10 Replies)
Discussion started by: mutley2202
10 Replies

5. Shell Programming and Scripting

Call a awk script with variable and input filename

HI, MY question is a very simple one: if i want to call an awk script with the input file name and also pass a variable value , then how to do it. #>awk -f my_script.awk -v variable=value my_inputfile.txt I can't do it like this. throws error: awk: my_script.awk:18:... (0 Replies)
Discussion started by: Onkar Banerjee
0 Replies

6. Shell Programming and Scripting

How to funnel stdout into call arguments?

This command successfully gives me the clearcase views I want in stdout, one line per view name. Each view name appears to have no spaces. cleartool lsview | grep -i `uname -n` | grep -v "ViewStorage\\|vgr_tools_sv" | cut -f 3 -d ' ' How can write a cygwin bash "for" loop that will iterate... (4 Replies)
Discussion started by: siegfried
4 Replies

7. UNIX for Dummies Questions & Answers

write() system call arguments

Hi, I'm trying to understand the arguments from this system call, can someone help me figure it out? write(1, "/home/nick/11sp/fred\n", 27/home/nick/11sp/fred) = 27 for argument 1, i know it is a file descriptor which specifies standard output. Argument 2, i believe is "what is to be... (4 Replies)
Discussion started by: l flipboi l
4 Replies

8. Shell Programming and Scripting

Print arguments with the help of variable

Let's say I want to print the arguments $4 till $#, how can I do this? $# contains the number of arguments $@ contain all the arguments as string What i need is something like for i in $4_till_$#; do #do something with $i convert $i ~/$output done The first 3 arguments are used as options... (6 Replies)
Discussion started by: hakermania
6 Replies

9. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

10. Programming

can we send arguments to system() call

Hi friends my C code is int main() { system("cp <source> <destination>"); } my question is how to set variables for <source> and <destination> how can we pass it to system() call. can you suggest me thankyou kingskar (6 Replies)
Discussion started by: kingskar
6 Replies
Login or Register to Ask a Question