LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-06-2013, 10:06 PM   #1
flaskvacuum
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
while ...loop


Code:
total=1
c=1
test_1=ready1
test_2=ready2
test_3=ready3

while [ c -le total ]
do
	DUMP_DIR=/home/john/admin/$SID_$c/dump
	
	echo "$DUMP_DIR"

	c=`expr $c + 1`
	echo "$c"
done
hi i have trouble interpreting the loops
#SID_$c

how can i escape the "_"
i just need to loop through:
test_1
test_2
test_3

which the number is incremented by $c
 
Old 01-06-2013, 10:21 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
I see three mistakes:

1) You haven't defined SID. Looks like you want
Code:
SID=test
2) Your while condition is wrong. It needs to be:
Code:
while [ $c -le $total ]
3) When a variable is part of a word it needs to be in brackets:
Code:
        DUMP_DIR=/home/john/admin/${SID}_${c}/dump
 
Old 01-06-2013, 10:23 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You could have used array after initializing total to 2 and c to 0:

Code:
#!/bin/bash
total=2
c=0
arr=(ready1 ready2 ready3)
while [ $c -le $total ]
do
	DUMP_DIR=/home/john/admin/${arr[$c]}/dump
	echo "$DUMP_DIR"
	c=`expr $c + 1`
	echo "$c"
done

Last edited by shivaa; 01-07-2013 at 02:21 AM.
 
Old 01-07-2013, 12:01 AM   #4
flaskvacuum
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
total=1
c=1
test_1=ready1
test_2=ready2
test_3=ready3

while [ c -le total ]
do
DUMP_DIR=$SID_$c

echo "$DUMP_DIR"

c=`expr $c + 1`
echo "$c"
done

erm, is not printing the value of the variable
It should print the value of the variable test_1 -> ready1 ..etc
 
Old 01-07-2013, 12:38 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Check my post above (which I edited and saved mistakenly, sorry for that):
Use a simple array in that case, as:
Code:
#!/bin/bash
total=2
c=0
arr=(ready1 ready2 ready3)
while [ $c -le $total ]
do
	DUMP_DIR=/home/john/admin/${arr[$c]}/dump
	echo "$DUMP_DIR"
	c=`expr $c + 1`
	echo "$c"
done

Last edited by shivaa; 01-07-2013 at 02:21 AM.
 
Old 01-07-2013, 12:51 AM   #6
flaskvacuum
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
Check my post above (which I edited and saved mistakenly, sorry for that):
Use a simple array in that case, as:
Code:
#!/bin/bash
total=2
c=0
arr=(ready1 ready2 ready3)
while [ $c -le total ]
do
	DUMP_DIR=/home/john/admin/${arr[$c]}/dump
	echo "$DUMP_DIR"
	c=`expr $c + 1`
	echo "$c"
done

i have change
Code:
#!/bin/bash
total=2
c=0
arr=(ready1 ready2 ready3)
while [ $c -le total ]
do
	DUMP_DIR=${arr[$c]}
	echo "$DUMP_DIR"
	c=`expr $c + 1`
	#echo "$c"
done
but with the error.

Syntax error at line 6 : `(' is not expected.
 
Old 01-07-2013, 01:07 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Code:
DUMP_DIR=${arr[$c]}
	echo "$DUMP_DIR"
What is need to first saving the array element in DUMP_DIR and then printing DUMP_DIR? Instead, you can directly print array element, as:-
Code:
echo "${arr[$c]}"
If it still gives any error, once add set -xv just below script interpreter to debug the script, as:-
Code:
#!/bin/bash
set -xv
total=...
....
.....
 
Old 01-07-2013, 01:19 AM   #8
flaskvacuum
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
Code:
DUMP_DIR=${arr[$c]}
	echo "$DUMP_DIR"
What is need to first saving the array element in DUMP_DIR and then printing DUMP_DIR? Instead, you can directly print array element, as:-
Code:
echo "${arr[$c]}"
If it still gives any error, once add set -xv just below script interpreter to debug the script, as:-
Code:
#!/bin/bash
set -xv
total=...
....
.....
everything looks fine just that it keep giving out:
Syntax error at line 7 : `(' is not expected.

not too sure why.
 
Old 01-07-2013, 02:17 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by flaskvacuum View Post
everything looks fine just that it keep giving out:
Syntax error at line 7 : `(' is not expected.
It is just because of your script interpretter i.e. shebang. What's it? Set it to:-
Code:
#!/bin/bash
Also check the while condition, and use $total, not just total:
Code:
while [ $c -le $total ]

Last edited by shivaa; 01-07-2013 at 02:20 AM.
 
Old 01-07-2013, 07:14 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I have nothing much to add to the issue, but I do have a question ... Why use a while loop like a for loop and not just use a for loop??
Code:
# currently
total=2
c=0
while [ $c -le total ]
do
    ...
    c=`expr $c + 1`
done

# OR
for (( c = 0; c <= total; c++ ))  # yes it is correct that in (()) you do not need '$' in front of variables

# OR is total never changes
for c in {0..2}
Just a thought
 
1 members found this post helpful.
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:53 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