Script giving different result on Linux compared to Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script giving different result on Linux compared to Unix
# 1  
Old 01-23-2009
Script giving different result on Linux compared to Unix

Hi

I have a script executing fine in Unix but in linux I am getting different result.

I have three files under /local/home/temp/Gen

test.sh
list.txt
shst.txt

Contents of test.sh
--------------------------
#!/bin/ksh

K=0;
SCRIPT_DIR=/local/home/temp/Gen

cat $SCRIPT_DIR/test/list.txt | while read WRDLST
do
set -A STAT `cat $SCRIPT_DIR/test/shst.txt | grep $WRDLST | awk '{print $2}'`

OUTBSTAT[$K]=${STAT[0]}
echo "OUTBSTAT[$K]=${OUTBSTAT[$K]}"
K=`expr $K + 1 `
done
K=0

while [ $K -ne 3 ]
do
echo " OUTBSTAT[$K]=${OUTBSTAT[$K]}"
K=`expr $K + 1`
done
------------------------------------------------------

Content of list.txt
----------
abc
xyz
lmn
----------
Content of shst.txt
---------------------
abc 1000
xyz 1001
lmn 1002
----------------------

When I run test.sh on a linux machine I get below result

OUTBSTAT[0]=1000
OUTBSTAT[1]=1001
OUTBSTAT[2]=1002
OUTBSTAT[0]=
OUTBSTAT[1]=
OUTBSTAT[2]=

When I run same on unix machine I get below proper result

OUTBSTAT[0]=1000
OUTBSTAT[1]=1001
OUTBSTAT[2]=1002
OUTBSTAT[0]=1000
OUTBSTAT[1]=1001
OUTBSTAT[2]=1002

what changes need to be done to the test.sh to get same above results in Linux too ?
Please help
# 2  
Old 01-24-2009
Hi.

Please learn to use CODE tags to make your scripts and data readable.

The problem is scope -- in some versions of shells, the pipe-into-while-loop is run in a separate process, so that variables are out of scope -- they no longer exist, and variables are not passed up from children to parents.

Some versions of Linux will have a recent version of ksh and allow your script to run as you wish (code omitted, paths and names made local):
Code:
$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-1-686, i686
Distribution        : Debian GNU/Linux 5.0
ksh 93s+

OUTBSTAT[0]=1000
OUTBSTAT[1]=1001
OUTBSTAT[2]=1002
 OUTBSTAT[0]=1000
 OUTBSTAT[1]=1001
 OUTBSTAT[2]=1002

However, a portable method would be to re-direct into the while loop, useful for pdksh, for example, on this different version of Linux:
Code:
#!/bin/ksh

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)
set -o nounset
echo

K=0
SCRIPT_DIR=/local/home/temp/Gen
SCRIPT_DIR=.

# cat $SCRIPT_DIR/test/list.txt | while read WRDLST
# cat $SCRIPT_DIR/list.txt | while read WRDLST
cp $SCRIPT_DIR/list.txt t1
while read WRDLST
do
  # set -A STAT `cat $SCRIPT_DIR/test/shst.txt | grep $WRDLST | awk '{print $2}'`
  set -A STAT `cat $SCRIPT_DIR/shst.txt | grep $WRDLST | awk '{print $2}'`

  OUTBSTAT[$K]=${STAT[0]}
  echo "OUTBSTAT[$K]=${OUTBSTAT[$K]}"
  K=`expr $K + 1 `
done <t1

K=0
while [ $K -ne 3 ]
do
  echo " OUTBSTAT[$K]=${OUTBSTAT[$K]}"
  K=`expr $K + 1`
done

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
PD KSH v5.2.14 99/07/13.2

OUTBSTAT[0]=1000
OUTBSTAT[1]=1001
OUTBSTAT[2]=1002
 OUTBSTAT[0]=1000
 OUTBSTAT[1]=1001
 OUTBSTAT[2]=1002

This is a common problem among shells. Moral: don't pipe into while loops ... cheers, drl
# 3  
Old 01-24-2009
Hi drl

Thanks a lot. It works now.

Next time I will make sure I use CODE tags for scripts and data
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

3. Red Hat

Memory conmsumption by Java server is very high on Linux compared to Soalris

Hello All, We have a jave server running on both linux and Solaris environments. On solaris,it consumes only 600-700MB whereas it goes upto 21G in Linux. I am monitoring the memory consumption through top command. Is this high memory consumption expected in Linux? (1 Reply)
Discussion started by: prasperl
1 Replies

4. Shell Programming and Scripting

Monitoring Sript giving random end result

Hi Guys, I am developing a script to monitor GUI based FileNet Component "Component Manager" which logs it's running status in a log file. Log file is a huge file so in script I put last 300 lines of log file in seperate file and run script every 5 minutes. I am searching the string... (2 Replies)
Discussion started by: dhirajdsharma
2 Replies

5. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

6. AIX

errpt not giving a result

my system get rebooted by its self after its came up i try to check the error log P690/>errpt | more Cannot open error message catalog /usr/lib/nls/msg/en_US/codepoint.cat. The error report will still run, but it will not have explanatory messages P690/>ls -lrt... (1 Reply)
Discussion started by: thecobra151
1 Replies

7. Shell Programming and Scripting

$0 not giving script name

Why does $0 return the word usage rather than the script name when used in a function? Baffeled on this one, any help appreciated. usage() { echo "$0 -cs <number of batches>\n" echo "$0 -c 4" echo "$0 -s 4" # echo "-c = Create" # echo "-s = Submit\n" exit 1 } $... (1 Reply)
Discussion started by: nhatch
1 Replies

8. Red Hat

Linux system hangs giving ntp errors

Hi folks, I am facing issue of my system (host1) getting hanged after throwing following ntpd messages. I am not able to ssh to the server there after. only option is to restart the host: Dec 29 02:58:51 host1 ntpd: time reset -0.207907 s Dec 29 02:58:51 host1 ntpd: synchronisation lost Dec 29... (0 Replies)
Discussion started by: SiddhV
0 Replies

9. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

10. Shell Programming and Scripting

My script is not giving result for 2 or more arguments

Hi, I am new to shell scripting and my below script is not giving result for 2 or more arguments. Can anyone help pls. #!/bin/sh sname=$(basename $(readlink -nf $0)) echo "This is $sname, running at $(date)" echo "It is running on $(hostname)" echo "Script being run by" echo " User... (3 Replies)
Discussion started by: baigmd
3 Replies
Login or Register to Ask a Question