Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


For loop


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
For loop

i executed below command on my cmd

Code:
for i in {1..8}; do echo n; done

given me
Code:
n
n
n
n
n
n
n
n


But while I am trying to execute same thing putting in file say test.sh and running sh test.sh

its giving me only
Code:
n

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Can someone explain why?

Last edited by Don Cragun; 09-06-2016 at 04:14 PM.. Reason: Add CODE and ICODE tags.
# 2  
What operating system are you using?

What is your login shell?

What happens if you run test.sh with your login shell instead of running it with sh?
# 3  
Hi Don

Code:
piyush@L-6PSWH32:~$ ./test.sh
n
n
n
n
n
n
n
n
piyush@L-6PSWH32:~$ sh test.sh
n
piyush@L-6PSWH32:~$ echo $SHELL
/bin/bash
piyush@L-6PSWH32:~$


Last edited by Scrutinizer; 09-06-2016 at 05:24 PM.. Reason: code tags
# 6  
To expand on what Corona wrote, running with the Bourne shell sh, your loop (failing to understand {1..8} as it is a bash expression giving boundaries) will use it as a single term.
First time through, variable $i will be the literal string {1..8} and your loop will run (displaying an n)
When the loop hits the done and tries again, there are no other items in the list to process, so the loop ends.

You can see this effect by displaying the value of $i from within the loop and then try it both ways.



I hope that this helps,
Robin

Last edited by rbatte1; 09-07-2016 at 07:00 AM.. Reason: Added a test suggestions
# 7  
Or test with echo
Code:
echo {1..8}

This expands to 1 2 3 4 5 6 7 8 in bash only.
A portable loop needs
Code:
for i in 1 2 3 4 5 6 7 8

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk loop using array:wish to store array values from loop for use outside loop
klane
Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ...... Shell Programming and Scripting
2
Shell Programming and Scripting
Reset while loop to loop same file multiple times
tmalik79
Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10...... Shell Programming and Scripting
4
Shell Programming and Scripting
Array Variable being Assigned Values in Loop, But Gone when Loop Completes???
mrm5102
Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now...... Shell Programming and Scripting
5
Shell Programming and Scripting
Null Handling in Until loop. . .loop won't stop
brandono66
Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ...... Shell Programming and Scripting
4
Shell Programming and Scripting
Using variables created sequentially in a loop while still inside of the loop [bash]
DeCoTwc
I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the...... Shell Programming and Scripting
2
Shell Programming and Scripting

Featured Tech Videos