LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2015, 05:12 PM   #1
mntbighker
LQ Newbie
 
Registered: Sep 2012
Location: Silicon Valley
Posts: 27

Rep: Reputation: Disabled
bash for loop question


Can someone suggest what I should change to make this work?

Code:
# List torque_properties
for i in {0..$LN}
do
  qmgr -c "list node compute-0-${i}"
done
What I get back is qmgr -c "list node compute-0-{0..4}".

I can make it work this way:

Code:
# List torque_properties
for i in `seq 0 $LN`
do
  qmgr -c "list node compute-0-${i}"
done
But "seq" is deprecated.
 
Old 05-21-2015, 05:32 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
probably not the answer you are looking for:
Code:
[schneidz@hyper lq]$ i=0; while [ $i -le $LN ]; do  echo i = $i; ((i++)); done
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
 
Old 05-21-2015, 06:12 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by mntbighker View Post
Code:
# List torque_properties
for i in {0..$LN}
do
  qmgr -c "list node compute-0-${i}"
done
What I get back is qmgr -c "list node compute-0-{0..4}".
This is a shell expansion issue. The $LN variable is expanded after the brace expansion has taken place, which ruins the for loop.

I can think of (at least) two possible solutions/workarounds. First of all, you could use a C-style for loop instead of braces:
Code:
# List torque_properties
for (( i=0; i<=$LN; i++ ))
do
  qmgr -c "list node compute-0-${i}"
done
In addition to handling variables, C-style loops can count down as well as up, and use increments/decrements other than 1. What's not to like?

Another possibility would be to use echo and eval to force shell expansion to occur in the required order (really, really ugly):
Code:
# List torque_properties
eval `echo for i in {0..$LN}\; do qmgr -c "list node compute-0-\${i}"\; done`
Here, the echo statement expands $LN while the loop variable (and the semicolons) are preserved by means of escaping. The result is then fed to eval, which will execute the entire statement.

Now, I see no reason why any sane person would choose the latter approach in this particular case, but I've included it to show how echo and eval could be used if one really needs to force expansion to happen in a particular, non-standard order.

Last edited by Ser Olmy; 05-21-2015 at 06:17 PM.
 
1 members found this post helpful.
Old 05-21-2015, 06:28 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by mntbighker View Post
But "seq" is deprecated.
"Deprecated" suggests it's an old tool that is being phased out, which isn't the case. It's simply non-standard (not specified in POSIX).
 
Old 05-21-2015, 06:55 PM   #5
mntbighker
LQ Newbie
 
Registered: Sep 2012
Location: Silicon Valley
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thanks fellas, I resorted to the C-style solution.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script - loop question nitrohuffer2001 Programming 7 07-05-2013 03:20 PM
Bash Loop Scripting question TwinReverb Programming 2 10-21-2011 05:24 AM
Bash Script Loop Question SoulShaker Linux - Server 5 06-17-2009 01:44 PM
BASH 'while' loop question GSMD Programming 4 04-13-2007 12:51 AM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration