Print arguments with the help of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print arguments with the help of variable
# 1  
Old 02-20-2011
Question 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
Code:
for i in $4_till_$#; do
#do something with $i
convert $i ~/$output
done

The first 3 arguments are used as options for other things....
Thanks in advance!
# 2  
Old 02-20-2011
Perhaps like: ${@:3:5}
This User Gave Thanks to tornow For This Post:
# 3  
Old 02-20-2011
Hey thanks, this worked....!
Can you explain me please what the number 5 represents here?
I mean if I need to take the arguments from $7 and next, should I do
${@:7:5}
?
# 4  
Old 02-20-2011
It means: from-to. It works for all arrays, not only for $@. (not sure about variables, but i think there too).
Code:
 So: ${@:0:1} would be only the first

Code:
${@:3:6} elements of array (arguments in this case) 3,4,5,6

Code:
 ${@:4:7} elements 4,5,6,7

Ups, i am not allowed to post links yet. Go to "wiki bash-hackers org" and search for Substring/Element expansion: Arrays
as far i know
# 5  
Old 02-20-2011
Quote:
Originally Posted by hakermania
Hey thanks, this worked....!
Can you explain me please what the number 5 represents here?
I mean if I need to take the arguments from $7 and next, should I do
${@:7:5}
?
Basically the parameter expansion is ${name : offset : length}. When the parameter name is that of a variable the offset and length are string related, e.g:
Code:
$ str="123andrew789"
$ echo ${str:3:6}
andrew

When it is @ offset and length refer to positional parameters. so
Code:
${@:3:5}

will pick up parameters 3-7 (if that many exist). However
Code:
${@:3}

will pick up all positional parameters after the third.

Andrew
# 6  
Old 02-22-2011
What about getting all the arguments after e.g. the third?
# 7  
Old 02-23-2011
Quote:
Originally Posted by hakermania
What about getting all the arguments after e.g. the third?
Write a test script and see:
Code:
$ cat args
#!/usr/bin/env bash
echo ${#}
echo All: "$@"
echo 3 to 5: ${@:3:3}
echo 2 to $#: ${@:2}
$ bash args a b c d e f g
7
All: a b c d e f g
3 to 5: c d e
2 to 7: b c d e f g
$ bash args a b c d e f g h i j
10
All: a b c d e f g h i j
3 to 5: c d e
2 to 10: b c d e f g h i j

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arguments in variable vs direct string

Hello Community! Let's say that we have some script which counts its arguments number: arguments_count.sh: #!/bin/sh echo "Number of arguments="$#and some test script: test.sh: #!/bin/sh my_args="1 2 3 '4 5' 6" echo "Count of arguments when using my_args:" ./arguments_count.sh $my_args... (12 Replies)
Discussion started by: break_da_funk
12 Replies

2. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

3. Shell Programming and Scripting

Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments. Example: prargv "-e" "--examples" Inside prargv, I want to print all the arguments using echo echo "$@" This returns --examples rather than -e --examples" This problem can be fixed... (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

5. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

Unable to print dynamic arguments in for loop

Hi All, I want to pass few dynamic arguments to shell script. The number of arguments differ each time I call the script. I want to print the arguments using the for loop as below. But not working out. for (( i=1; i<=$#; i++ )) do echo $"($i)" done /bin/sh test.sh arg1 arg2 arg3 ... (1 Reply)
Discussion started by: laalesh
1 Replies

7. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

8. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Shell Programming and Scripting

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 x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

10. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies
Login or Register to Ask a Question