LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 02-20-2011, 03:46 AM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 66

Rep: Reputation: 15
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
 
Old 02-20-2011, 04:00 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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.
 
Old 02-20-2011, 04:02 AM   #3
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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.
 
Old 02-20-2011, 04:40 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
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
 
Old 02-20-2011, 05:15 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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.

Last edited by colucix; 02-20-2011 at 05:17 AM.
 
Old 02-20-2011, 05:28 AM   #6
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hi,

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

Kind regards,

Eric
 
Old 02-20-2011, 07:05 AM   #7
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 66

Original Poster
Rep: Reputation: 15
Colucix,

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

_ravi
 
Old 02-20-2011, 06:28 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
Please mark as SOLVED once you have a solution.
 
  


Reply

Tags
shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
Is there a way to print a range of a char array without a for loop? trist007 Programming 3 11-07-2010 09:15 AM
[SOLVED] How to pass arguments to a function using a loop 10110111 Programming 2 03-01-2010 03:24 PM
[SOLVED] What are short range link and long range links in routing? mq15 Linux - Networking 6 06-26-2009 11:16 PM


All times are GMT -5. The time now is 03:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration