Variable scop ksh vs bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable scop ksh vs bash
# 1  
Old 01-19-2015
Variable scop ksh vs bash

I am not clear why the cnt variable is not increased in the script below:

Code:
 #!/bin/bash
INPF=${1:-a.txt};
KWDS=${2:-lst}
cnt=0;
grep -v '^#' $KWDS | while read kwd;
do
        grep -q $kwd $INPF;
        if [ $? -eq 0 ]; then
                echo Found;
                ((cnt=cnt+1));
        fi
done
echo $cnt;

The files for input:
Code:
 /tmp$ cat a.txt 
ABC
XYZ
abc
xyz
/tmp$ cat lst
123
qwe
abc
zxc

when I run script I get:
Code:
 Found
0

When I change to Korn shell by using #!/bin/ksh instead script works as expected and I see result as
Code:
Found
1

Why is that? does it mean under bash the scope of cnt variable outside while loop is not the same as cnt inside the wihile loop?
Just in case, the bash is:
Code:
bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

Clarification will be appreciated.
# 2  
Old 01-19-2015
It's not a question of "scope" exactly, code running behind a pipe runs in a completely different process. It gets a copy of the current values of the parent's variables when it's created. Changes aren't shared.

KSH and everything-except-KSH consider pipes in the opposite direction:

Code:
# KSH
subshell | local-shell

# Bourne-compliant SH
local-shell | subshell

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-19-2015
Also, for bash try any of these:

Code:
POSIX?  : cnt=$(( $cnt +1 ))
POSIX?  : cnt=$[ $cnt + 1 ]
C-style : ((cnt++))

hth

EDIT:
Also, there are way too many unrequired line-ending-semi-colons - C, php or java? Smilie
Piping looks properly for bash.

Last edited by sea; 01-19-2015 at 07:01 PM..
This User Gave Thanks to sea For This Post:
# 4  
Old 01-19-2015
Quote:
Originally Posted by sea
Also, for bash try any of these:

Code:
POSIX?  : cnt=$(( $cnt +1 ))
POSIX?  : cnt=$[ $cnt + 1 ]
C-style : ((cnt++))

hth

EDIT:
Also, there are way too many unrequired line-ending-semi-colons - C, php or java? Smilie
Piping looks properly for bash.
The current POSIX standards specify arithmetic expansions:
Code:
cnt=$(($cnt + 1))
      and
cnt=$((cnt + 1))

They do not specify cnt=$[ $cnt + 1 ].
There is a proposal to include ((cnt++)) in the next revision of the POSIX standards, but it isn't required yet.
# 5  
Old 01-20-2015
Recent bash provides "process substitution", see man bash. Try
Code:
INPF=${1:-file1};
KWDS=${2:-file2}
cnt=0;
while read kwd;
        do grep -q $kwd $INPF;
           if [ $? -eq 0 ]; then
                echo    Found;  
                ((cnt=cnt+1));
           fi
        done  < <(grep -v '^#' $KWDS) 
echo $cnt;

# 6  
Old 01-20-2015
With recent bash releases, you can use the lastpipe option which was finally implemented to provide ksh behavior:

Code:
#!/bin/bash
shopt -s lastpipe
INPF=${1:-a.txt}
KWDS=${2:-lst}
cnt=0
grep -v '^#' $KWDS | while read kwd
do
        grep -q $kwd $INPF
        if [ $? -eq 0 ]; then
                echo Found
                ((cnt=cnt+1))
        fi
done
echo $cnt

Result:
Code:
Found
1

This User Gave Thanks to jlliagre For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

2. Shell Programming and Scripting

BASH to KSH

I have a script in KSH and now need to incorporate this into another script which is in BASH. OUr script contains code like below in good number of places. Eg: echo “A B C” | read VAR1 VAR2 VAR3 This works only in ksh and not in BASH. Please let me know 1. Which is the equivalent... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

3. Shell Programming and Scripting

Emulate ksh's FPATH variable in bash

Over time i have developed a library of useful (ksh) functions which i use in most of my scripts. I use the ksh's FPATH variable to locate all these functions and use a standard environment-setting-function to always have the same environment in all my scripts. Here is how i begin scripts: ... (3 Replies)
Discussion started by: bakunin
3 Replies

4. UNIX for Dummies Questions & Answers

bash preferred or ksh with bash features

I'm a user on a fairly locked down sys V server. By default, I have ksh set as my default shell. I added to my .profile: bash -o vi so when I login, it goes into bash so I can take advantage of tab completion and use the up key to bring up previous commands. However, whenever I want to exit, I... (2 Replies)
Discussion started by: mrwatkin
2 Replies

5. Solaris

ksh vs bash

Hi, I've a general question regarding shell. I 've seen that every where i worked in production environment people are using ksh .. but i like to use .. bash .. is there any particular reason why hardcore sysadmins use ksh ? (8 Replies)
Discussion started by: fugitive
8 Replies

6. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

7. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

8. Programming

the scop of variables in child process

Hi I have a question about the scope of variables for parent and a child I had written my code here and the output of this but only in child process the information is completely right even in main the informatin is wrong well the child process will see the global variable te2 and can change... (5 Replies)
Discussion started by: netman
5 Replies

9. Shell Programming and Scripting

Ksh vs Bash

Hi Buddy, Can any one help me to overcome from the below problem? #/usr/bin/ksh typeset -Z dd dd=1 echo $dd =========== out put for the above is 01 same script I'm migrating to bash but typeset -Z option is not found in bash, Pls get me the equivalent option in BASH Thanks in... (1 Reply)
Discussion started by: krishna
1 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question