LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   for loop for a particular range of arguments (http://www.linuxquestions.org/questions/linux-newbie-8/for-loop-for-a-particular-range-of-arguments-863842/)

say_hi_ravi 02-20-2011 03:46 AM

for loop for a particular range of arguments
 
I am writing a scripts which involves for loop. I want to make use of arguments passed to scripts ranging from 2nd to last.

I tried to use below

Quote:

kint `whoami`/root
for i in $2..$*
do
ssh root@$i "cd /local/0/home;mkdir -p $1/test"
done
I am trying to run it as below

Quote:

./scriptname webtech myserver1 myserver2 myserver3
and it gives following error

Quote:

ssh: myserver1..myserver2: name or service not known
I basically want to pass arguments from myserver1 to myserver3 to the script.

Please help.

-Ravi

colucix 02-20-2011 04:00 AM

You can try the shift statement to loop trough all the positional parameters. Before the loop save the first one, then cycle trough the others, e.g.
Code:

my_dir=$1
shift
until [[ -z $1 ]]
do
  ssh -n root@$1 "cd /local/0/home; mkdir -p $my_dir/test"
  shift
done

Note the -n option of ssh to avoid the termination of the loop after the first cycle. Reasons explained here. Hope this helps.

EricTRA 02-20-2011 04:02 AM

Hello,

A better option would be to use the positional parameter $@ in my opinion.
Quote:

@
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
so your script would change to:
Code:

for i in $@
do
ssh root@$i "cd /local/0/home;mkdir -p $1/test"
done

Try that out and let us know if it works for you.

Kind regards,

Eric

EDIT: beaten by the newly baked mod :-) but with another solution.

grail 02-20-2011 04:40 AM

As an addon to Eric's solution, when dealing with positional parameters you can simply call the for loop like so:
Code:

for i
do
...
done

Explained further here

colucix 02-20-2011 05:15 AM

The OP needs to cycle through positional parameters from the 2nd to the last one, so that your scripts need a little modification. Here is another solution with indirect reference:
Code:

for i in $(seq 2 $#)
do
  ssh -n root@${!i} "cd /local/0/home; mkdir -p $1/test"
done

Regarding the -n option of ssh, maybe in this case its not necessary, since the loop doesn't get the standard input (indeed the positional parameters are passed to the script itself, not to the loop). Sorry for the confusion.

EricTRA 02-20-2011 05:28 AM

Hi,

Nice catch colucix, missed the fact about starting from the second parameter. Thanks for pointing it out and a solution.

Kind regards,

Eric

say_hi_ravi 02-20-2011 07:05 AM

Colucix,

Mate, Once again your solution worked! Many thanks..

_ravi

grail 02-20-2011 06:28 PM

Please mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 11:40 PM.