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?!
# 8  
Old 11-10-2008
Quote:
It seems the shell some how do not processing assighment in the (( )).
hmm, another long shot in the dark, could it be that, somehow, for this connection, you are running an original Bourne shell, not BASH.
Code:
echo $SHELL

may tell you.
# 9  
Old 11-10-2008
Hi.

Versions of expr that I have used do not understand the "++" operator. Check man expr on your system.

I suggest you re-run your original script with set -x ... cheers, drl
# 10  
Old 11-10-2008
Quote:
Originally Posted by wempy
hmm, another long shot in the dark, could it be that, somehow, for this connection, you are running an original Bourne shell, not BASH.
It is interesting.
I know that I have started 'bash', but $SHELL is shown as 'ksh' ?!
Code:
> ec $SHELL
+ /usr/bin/echo /bin/ksh
/bin/ksh
> bash
+ 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  <---
> ec $SHELL
/bin/ksh

The '.myset' has couple declaration, functions and aliases. It is ran as 'source..' (>. .myset # with dot on first position) and couple functions related to the prompt (I have disabled the PS1 for all code, presented here to make it easy to see)..; but all that still seems not related to the problem.

Acctualy, I have already lost connection to the session with that problem and, sure, new one does not have the problem.

drl
Quote:
I suggest you re-run your original script with set -x ... cheers, drl
I could not repeat what I have done before and, even, could not tell what I have done when the problem appeared.
In that day I have worked on part of pretty big script; where I have prepare couple new functions and made some changes in other parts...; and now it works as it should be, and the problem is not showed up...
Quote:
Versions of expr that I have used do not understand the "++" operator.
It is right, it is not processing the '++', but it is not complaining, so I have not seen that.

Last edited by alex_5161; 11-10-2008 at 12:57 PM..
# 11  
Old 11-10-2008
Hi.

As an aside, the variable SHELL is almost never modified to reflect the current shell.

Here is a script that demonstrates the execution of a number of shells, and how to test for them (at least in Linux, untested in other *nix, although Solaris 10 seems to require ps -p $$):
Code:
#!/bin/sh

# @(#) s3       Demonstrate display of current shell.

## command "ps $$" produces something like:
#  19746 pts/1    S+     0:00 bash
#      1     2     3        4    5

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) bash3 tcsh ksh zsh dash rc
set -o nounset
echo

echo " The process ID looks like:"
bash <<'EOF'
ps $$
EOF

echo
echo " Result of pipeline to extract command:"
bash <<'EOF'
ps $$ |
tail -1 |
sed 's/  */ /g' |
cut -d" " -f5
EOF

echo
echo " Various shells, note rc difference:"
echo " using
 1) ps
 2) proc/\$$/cmdline (needs newline)
 3) \$SHELL
 4) env | grep -i shell
"
echo

echo " Expecting bash3"
bash3 <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

echo
echo " Expecting tcsh"
tcsh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

echo
echo " Expecting ksh"
ksh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

echo
echo " Expecting zsh"
zsh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

echo
echo " Expecting dash"
dash <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

echo
echo " Expecting rc"
rc <<'EOF'
ps $pid
# cat /proc/$$/cmdline
cat /proc/$pid/cmdline
echo
echo $SHELL
env | grep -i shell
EOF

exit 0

Producing:
Code:
% ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU bash 3.00.16(1)-release
tcsh 6.13.00
pdksh 5.2.14 99/07/13.2
zsh 4.2.4
dash - no version provided for /bin/dash.
rc 1.7.1

 The process ID looks like:
  PID TTY      STAT   TIME COMMAND
26815 pts/2    S+     0:00 bash

 Result of pipeline to extract command:
bash

 Various shells, note rc difference:
 using
 1) ps
 2) proc/$$/cmdline (needs newline)
 3) $SHELL
 4) env | grep -i shell


 Expecting bash3
  PID TTY      STAT   TIME COMMAND
26824 pts/2    R+     0:00 bash3
bash3
/bin/tcsh
SHELL=/bin/tcsh

 Expecting tcsh
  PID TTY      STAT   TIME COMMAND
26829 pts/2    R+     0:00 tcsh
tcsh
/bin/tcsh
SHELL=/bin/tcsh

 Expecting ksh
  PID TTY      STAT   TIME COMMAND
26868 pts/2    R+     0:00 ksh
ksh
/bin/tcsh
SHELL=/bin/tcsh

 Expecting zsh
  PID TTY      STAT   TIME COMMAND
26873 pts/2    R+     0:00 zsh
zsh
/bin/tcsh
SHELL=/bin/tcsh

 Expecting dash
  PID TTY      STAT   TIME COMMAND
26878 pts/2    R+     0:00 dash
dash
/bin/tcsh
SHELL=/bin/tcsh

 Expecting rc
  PID TTY      STAT   TIME COMMAND
26883 pts/2    S+     0:00 rc
rc
/bin/tcsh
SHELL=/bin/tcsh

cheers, drl
# 12  
Old 11-11-2008
So, by that checking, the $SHELL is not much representing. It keeps a loging shell. Any other started shell processed as a regular running task and does not update the env-SHELL.
Maybe not completely nice, but could be understood.

(off-top, but I did not ubderstand construction
Code:
version >/dev/null 2>&1 && version =o $(_eat $0 $1) bash3 tcsh ksh zsh dash rc

I see the 'version' is checked on acceptance (ignoring all output), and, after that - I guess, it is used for list of words.
It seems that part works like 'for .. in ...; ...', but it is surprizing for me and does not clear how it is done.
'_eat' - is it something local, as a 'version'?
'=o' - it looks strange, isn't it a typo?
It seems the
Code:
version $0 bash3 tcsh ksh zsh dash rc

whould be enouth to present the version number (as I could guess the 'version' functionality) Am I wrong?
Anyway, I do not see the meaning of this execution:
Code:
$(_eat $0 $1)

Maybe you could give some description?! I will appreciate it!)
# 13  
Old 11-11-2008
Hi, alex_5161.

Generally, you analysed correctly.

The version and _eat commands are local to the business where I work. They are used to document versions of software that are used by the script.

The version command usually obtains the version of other commands by using "command --version" or "command -version", but also takes into account special cases, such as needed for perl, tcsh, python, etc. The special argument =o causes the operating system name to be printed. The _eat command extracts the appropriate token from the shebang (first line of the script).

These are not standard commands, so you will not see them in any Unix, GNU/Linux, etc. That is why my scripts first test for the existence of version (by checking the exit status), uses it if is present, but otherwise skips the remainder of the complex command. This allows people to copy, paste, and run the scripts even if version is not an available command on their systems.

Feel free to remove that section from your copy of the script as you choose.

Another simple method to see the current shell name is:
Code:
echo $0

Best wishes ... cheers, drl
# 14  
Old 11-11-2008
Thank you, drl for explanation. Now I fill better knowing that it is not a big holes in my knowlege Smilie

Regarding the topic: if I will have another case of the same situation I will bring it here (or, maybe, any explanation of that..)
 
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