General
cancel
Showing results for 
Search instead for 
Did you mean: 

Shell script, different result in different O/S???

SOLVED
Go to solution
Qcheck
Super Advisor

Shell script, different result in different O/S???

Hi All,

Can you please let me know what the following script is doing:

#!/usr/bin/ksh

function myFunction {
ls | while read i
do
if [ -d "$i" ]; then
echo "Directory: $i";

elif [[ $i = wp_cancellation*.out ]];
then
echo "Initial Value EmailFlag $EMAILFLAG";
EMAILFLAG=2;
echo "***Altered Value EmailFlag $EMAILFLAG *****wp_cancellation => $i";
fi;

done;
}

WP_IN=/webdata/dev/wp/inbound
cd "$WP_IN";
EMAILFLAG=1;

myFunction
{
echo "Final Value EmailFlag $EMAILFLAG";
}

exit 0

**********************************************

I get the following result, when I run the script on Linux server:

Directory: kris
Initial Value EmailFlag 1
***Altered Value EmailFlag 2 *****wp_cancellation => wp_cancellation20070611112859.out
Final Value EmailFlag 1

***********************************************

But the user complains when he runs the same script on a Tru64 node he gets the following result:

Directory: kris
Initial Value EmailFlag 1
***Altered Value EmailFlag 2 *****wp_cancellation => wp_cancellation20070611112859.out
Final Value EmailFlag 2

I couldn't figure out why???

Any help is greatly appreciated. I know it is nothing related to the O/S issue. But how can I prove to them??

Since Tru64 is going to be end-of-life, we are migrating to Linux environment(node). My question why the final value is different in Linux and Tru64???

Your time is greatly appreciated.
14 REPLIES
Steven Schweda
Honored Contributor

Re: Shell script, different result in different O/S???

You have a difference in behavior between
Linux and Tru64, so you ask about it in an
HP-UX forum? How much sense does that make?

What is "/usr/bin/ksh" on each system?
Perhaps there's a bug in one of the shells.

As I read "man ksh" (on an HP-UX system), I'd
say that the Tru64 result is right, but my
opinion may not be worth much:

[...] Ordinarily,
variables are shared between the
calling program and the function.
However, the typeset special command
used within a function defines local
variables whose scope includes the
current function and all functions it
calls.
[...]

To me, that says that EMAILFLAG is EMAILFLAG,
but I may be missing something. (Similar for
the "{}" around the final "echo".)

You could try a different shell, or change
the script to pass the result differently.
(Use a function return value, for example.)
Qcheck
Super Advisor

Re: Shell script, different result in different O/S???

Hi steven,

I know that i posted in HPUX forms, because I thought u guys respond well and I trust u guys. I always had a good response from this site, thats the reason I posted here thinking that it is not the part of O/S related. It is the part of the script.

Any ways thanks for the time!
Steven Schweda
Honored Contributor

Re: Shell script, different result in different O/S???

As usual, "Linux server" is not a complete
description of the system, and "ksh" is not a
a complete description of the shell being
used. I tried a slightly modifed version of
your script on an HP TestDrive Linux system
and got the expected result:

td178> /usr/bin/ksh vtx.sh
Directory: fred
Initial Value EmailFlag 1
***Altered Value EmailFlag 2 *****wp_cancellation => wp_cancellation_fred.out
Final Value EmailFlag 2

td178> uname -a
Linux td178.testdrive.hp.com 2.6.18-8.1.4.el5 #1 SMP Fri May 4 22:16:19 EDT 2007
ia64 ia64 ia64 GNU/Linux

td178> /usr/bin/ksh --version
version sh (AT&T Labs Research) 1993-12-28 r


> it is not the part of O/S related. [...]

No, but "ksh" probably came with the OS,
although with Linux, there's always some
doubt where anything came from.
Dennis Handly
Acclaimed Contributor

Re: Shell script, different result in different O/S???

>But the user complains when he runs the same script on a Tru64 node he gets the following result:

Is he complaining that Linux is broken?

Basically Linux is broken. hp-ux's sh, ksh and ksh.93 all agree the answer should be 2.

And as Steven says, the script isn't using typeset to redeclare it.

So you should really redirect this to a Linux thread.

>Steven: (Similar for the "{}" around the final "echo".)

That just makes that execute in the same enironment. It seems that is only useful if you want to redirect the output of a whole block?
I also noticed a whole bunch of wasted ";" on the end of lines.
Steven Schweda
Honored Contributor

Re: Shell script, different result in different O/S???

> >Steven: (Similar for the "{}" around the final "echo".)

> That just makes that execute in the same enironment.

Exactly, so setting the variable in there
should not cause any trouble.

> I also noticed [...]

It may be that writing the script more
cleanly would lead to better results on the
(mystery) Linux system, but, as I said, it
worked as expected on the first Linux system
I tried, so it'd be too much work for me to
try to find the problem.

True or false: "If you've seen one Linux
system, you've seen them all."

> Since Tru64 is going to be end-of-life,
> [...]

Which is better, an end-of life system which
works, or ...?
Dennis Handly
Acclaimed Contributor
Solution

Re: Shell script, different result in different O/S???

The version of ksh on my Linux box is also broken:
Linux XXXX 2.6.5-7.139-default #1 SMP Fri Jan 14 15:41:33 UTC 2005 ia64 ia64 ia64 GNU/Linux
PD KSH v5.2.14 99/07/13.2

This is 6 years newer than Steven's.
Steven Schweda
Honored Contributor

Re: Shell script, different result in different O/S???

> This is 6 years newer than Steven's.

Well, it's 6 years newer than mine (HP's,
actually) _says_ it is.


Tad: Wake up, Tommy.
Tom Reagan: I am awake.
Tad: Your eyes are shut.
Tom Reagan: Who you gonna believe?
Stuart Browne
Honored Contributor

Re: Shell script, different result in different O/S???

The issue is that you're resetting the 'EMAILFLAG' variable within a piped (read: child) while loop. All variables within the 'while' won't live outside of it.

The way to get it to work is to not use a pipe, but a '<' on the other end of the while:

ls > file
while read i
do
...
done < file

(or if you're BASH happy:

while read i
do
...
done < <(ls)

but I don't think the Tru64 would allow it).

Both 'pdksh' and 'bash' treat piped children in this manner, where as 'ksh' on most other unicies don't.

Either way, the first form here should work on both.
One long-haired git at your service...
Qcheck
Super Advisor

Re: Shell script, different result in different O/S???

Morning All,

Staurt! thank you so much, once I removed pipe and put < it worked. See! I knew I will get answer if I post here.

Steven! See my problem has been solved even though I posted in hp forums. Thank you for your input though.

Dennis! Thanks for your time too..

Still I need to find why the same script doesn't work? when I did #/usr/bin/ksh --version, it says bad option. I also assume ksh is corrupted. Do you how can I download ksh and try???

Thank you all for your time and answers, I really appreciate, this meant alot to me.

Please try to throw some ideas if you guys find something, I am sure u will...;)
Dennis Handly
Acclaimed Contributor

Re: Shell script, different result in different O/S???

>Steven! See my problem has been solved even though I posted in hp forums.

You may be confused. This thread was moved to Linux and then Stuart answered. ;-)

>when I did #/usr/bin/ksh --version, it says bad option. I also assume ksh is corrupted.

That didn't work for me either. I got the version in vi editing mode when I typed control-V.
Qcheck
Super Advisor

Re: Shell script, different result in different O/S???

Thank you Dennis! I didn't know this was moved to Linux. Thanks anyways...
Stuart Browne
Honored Contributor

Re: Shell script, different result in different O/S???

Yeap.. I'm a Penguin..

Don't visit the HP-UX forums much any more.. Haven't had to touch one for about 3 years.. :)
One long-haired git at your service...
Qcheck
Super Advisor

Re: Shell script, different result in different O/S???

Staurt! How can I install ksh on my Linux node???
Heironimus
Honored Contributor

Re: Shell script, different result in different O/S???

Not all Korn shells are created equal. Many Linux distributions ship pdksh as a Korn shell. pdksh implements KSH88, but I don't think it's even a fully-compatible implementation of that. Several years ago AT&T released the code for their KSH93 and some Linux systems now use that instead, others don't because of the AT&T license.

I think most current UNIX platforms use KSH93, but there may still be a few KSH88 holdouts. If you want a Korn shell that will be just like the one on a modern commercial UNIX system you're almost certainly better off with the AT&T version instead of pdksh.