Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!
# 1  
Old 11-07-2008
Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected.
That means or it is looping without exitting, or it is not loop even once.

Here example of my try when it is not processing even one loop.
You can see, I start new subshell and 'for' works fine, but returning back, the 'for' doesn't work again"
Code:
> for ((i=1;i<3;i++));do echo $i; done;
> echo mm
mm
> for ((i=1;i<3;i++));do echo $i; done;
> bash
executing file .bashrc - starting the bash shell--

----> Start of the .myset file  <---

  ----  It is SUN0 box with TEST and MODEL offices  ----

----> End   of the .myset file  <---
>
>
> for ((i=1;i<3;i++));do echo $i; done;
1
2
> 
> exit
exit
> for ((i=1;i<3;i++));do echo $i; done;
> echo boooo
boooo
>
>

Does anybody know what could be a reason?
I do not think it is related to connection soft (I use PuTTY.)

Appreciate any help or hint
# 2  
Old 11-07-2008
Code:
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$ echo booo
booo
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$
bash-3.00$ for ((i=1;i<3;i++));do echo $i; done;
1
2
bash-3.00$

I can't reproduce your symptom. And I was using putty. Smilie
# 3  
Old 11-07-2008
It is correct, you can't reproduce it!
And I too, can not reproduce it!
I have this situation second time.
First time I have spend couple hours trying to realize what is wrong with my script, untill did not gess to try it in another session, where everything works fine.
Now some how it is happened again. Hopefully some time I could learn from past; so I have realized it shortly.
Now the session is oppened and I could make some test, if anybody would advice to try anything

As I have said, I have no idea how and why that is happening and that is my question, if anybody have any clue about that?!?!
# 4  
Old 11-07-2008
Hello,
this may be a long shot, but maybe You have an alias or something that is active in Your first shell (a login shell perhaps, that read .bash_login?) and the next shell is not a login shell and hence has some other definitions that overrule the first ones?
save output from alias and set from each shell and compare them. It maybe a lot of text but You said You wanted to understand... Smilie
use sort and -u on each file to narrow down size and then diff.

/Lakris
# 5  
Old 11-07-2008
Thanks Lakris.

Relation to login is not a reason, because a new connection has no problem originally. When that happened I di not know.

I have compared the aliases:
Code:
> alias >bad_alias
> !for
for ((i=1;i<3;i++));do ec $i; done;
>
>

In another session:
Code:
> for ((i=1;i<3;i++));do ec $i; done;
1
2
> alias>good_alias
>
>
> diff bad_alias good_alias
>                                    # so files are the same
>

Also I have saved result of 'typeset' and 'env'
Later will review (some function I could add, change during mork.)

But on first glance there is no real difference.
# 6  
Old 11-08-2008
Hi.

It might be useful to see how the shell is interpreting everything. Using set -x will show you the commands. This is from a vanilla bash called as sh:
Code:
$ set -x
+ set -x
sh-2.05b$ for ((i=1;i<3;i++));do echo $i; done;
+ (( i=1 ))
+ (( i<3 ))
+ echo 1
1
+ (( i++ ))
+ (( i<3 ))
+ echo 2
2
+ (( i++ ))
+ (( i<3 ))

cheers, drl
# 7  
Old 11-10-2008
Thank you, drl, set -x realy interesting thing.

It seems the shell some how do not processing assighment in the (( )).
Also in 'let' and in 'expr' ('ec' is an alianse for echo)
Code:
>ec $i; expr i++; ec $i
+ /usr/bin/echo 2
2
+ expr i++
i++
+ /usr/bin/echo 2
2
>ec $i; expr i++; expr i=1; ec $i
+ /usr/bin/echo 2
2
+ expr i++
i++
+ expr i=1
i=1
+ /usr/bin/echo 2
2
>ec $i; expr i++; let i=1; ec $i
+ /usr/bin/echo 2
2
+ expr i++
i++
+ let i=1
+ /usr/bin/echo 2
2
>ec $i; expr i++; i=1; ec $i
+ /usr/bin/echo 2
2
+ expr i++
i++
+ i=1
+ /usr/bin/echo 1
1
>ec $i; expr i++; i=$((i++)); ec $i
+ /usr/bin/echo 1
1
+ expr i++
i++
+ i=1
+ /usr/bin/echo 1
1
>

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bourne shell is not working

I have a loop with cases I am working on Bourne shell for file in *.${Today}*.csv *.${Today}*.txt\ do case ${file} in sun_detail) do something ;; sum) do something ;; mod) do something ;; *) do something ;; (5 Replies)
Discussion started by: digioleg54
5 Replies

2. Shell Programming and Scripting

Shell script loop not working

Hi I'm using this script to transcode videos in an Ubuntu 12.04 machine. #! /bin/bash MOVDIR="/media/topaz_1/media/transcodes/transcode_mov/" MOVDESTDIR="/media/topaz_1/media/transcodes/final_mov/" DONEFILESDIR="/media/topaz_1/media/transcodes/dv_cache/"... (1 Reply)
Discussion started by: oscarfelson
1 Replies

3. Shell Programming and Scripting

BASH: Factorial using FOR loop not working

Hi, I am trying to run the factorial script but it’s not working. The code is mentioned below: ------------------------------------------------------------------ /home/gc> cat fact.sh #!/bin/bash # using one command line parameter factorial=1 for (( number = 1; number <= $1 ;... (3 Replies)
Discussion started by: Technext
3 Replies

4. AIX

Shell script stop working

I have a strange problem. I have the following in a cron to find files older than a day. find /dir1/dir2/ ! -name . -prune -name "s*.txt" -type f -mtime +1 -exec echo {} \; | wc -w It was working fine for the last few days now it suddenly stopped working. I can clearly see files in the... (5 Replies)
Discussion started by: bbbngowc
5 Replies

5. Shell Programming and Scripting

export not working in Bash shell

Hi Friends, I am presently migrating shell scripts writter in KSH to SH.I am stuck at this place and i am not able to find a work around:- Let the script name is x.sh Below are some of the codes in it... export abc=hello export abc=hi export abc=how When i am trying to compile the script ... (6 Replies)
Discussion started by: amit.behera
6 Replies

6. Shell Programming and Scripting

Shell Script not working using for loop and a funtion

Hi- Here is the shell script that for some reason is not returning results: #! /bin/ksh - avg() { AVG=0 typeset SUM=0 if then echo "You must have at least two numbers" else for NUM in "$*" do ... (2 Replies)
Discussion started by: koomba
2 Replies

7. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

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: ... (4 Replies)
Discussion started by: brandono66
4 Replies

8. Homework & Coursework Questions

Help with Interactive / Non Interactive Shell script

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (1 Reply)
Discussion started by: rits
1 Replies

9. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

10. UNIX for Dummies Questions & Answers

korn shell to bash - statement not working

Everything else seems to be working, but this isn't. Is it the "cat..." that is wrong of the condition? Thanks. cat tc_result.txt | while read LINE do if then let "files_run += 1"; echo "inside the if loop" # save current filetype case $LINE... (5 Replies)
Discussion started by: brdholman
5 Replies
Login or Register to Ask a Question