Sponsored Content
Full Discussion: ksh behavior change on RHEL5
Top Forums Shell Programming and Scripting ksh behavior change on RHEL5 Post 302416792 by alister on Tuesday 27th of April 2010 07:15:47 PM
Old 04-27-2010
Everything you are seeing is what happens when shell scripts depend on shell implementation details which are not prescribed by any standard.

I don't use RHEL and I only have one version of ksh with which to test, but I'm almost certain that the differences that you see between the old and new system is due to a ksh version update and a change in the way those versions execute pipelines/subshells.

If you take a look at the posix standard, it specifies the details of the execution environment, but it says nothing regarding implementation. The reason the command substitutions often yield a number 1 greater than the value from the same command that is not part of a substitution is because the shell has forked another copy of itself to execute the pipeline, and that new copy has the same $0.

The reason you see that 0 in your most recent post is because the shell has most likely exec'd into one of the utilities in its final pipeline before the ps utility fetches the process data. If you add another command after that pipeline, you'll probably see the count remain at 1.

In any case, these scripts make unportable assumptions. Not only are they not portable between different shells, they are not even portable between different versions of the same shell (as you have no doubt noticed Smilie).

The best course of action (unless there is a massively immovable obstacle in the way) would be to rewrite these to not depend on the low level implementation of the shell.

Regards,
Alister

---------- Post updated at 07:15 PM ---------- Previous update was at 06:19 PM ----------

Quote:
Originally Posted by bwhitehd
The last number of the non-patched system is incorrect here. "0" should be "1"
I can reproduce that behavior with the following script (pswc.sh):
Code:
#!/bin/ksh

ktrace -tc -ip $$
echo $0
SCRIPT=${0##*/}
echo $SCRIPT
/bin/ps | grep -v grep | grep $SCRIPT
/bin/ps | grep -v grep | grep $SCRIPT | /usr/bin/wc -l

KSH Version:
Code:
$ ksh --version
  version         sh (AT&T Labs Research) 1993-12-28 p

Output:
Code:
# ./pswc.sh 
./pswc.sh
pswc.sh
 1388  p1  S+     0:00.01 /bin/ksh ./pswc.sh
       0

Also, the trace output confirms that by the time ps starts to read the process list data, all of the ksh instances are gone (exec'd into greps, ps, and wc).

There is nothing "wrong" here, except the expectations of the script.

Hope that helps,
Alister

P.S. I can attach the trace output, but I doubt it's of much interest. It's not a RHEL system (it's not even linux), and you can probably reproduce it yourself with strace (or whatever is the name of the linux equivalent of ktrace).
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh parameter --- change

I am reading a file into a parameter in my program typeset nums_type if the contents of nums_type = asdfghbqwerty how do make a new paremeter eqaul to the 7th character of nums_type. like ${type} = b (2 Replies)
Discussion started by: frank
2 Replies

2. Shell Programming and Scripting

Strange behavior from 'read' statements. (ksh - but could be same on other shells)

I'm getting rather frustrated with an interactive script I'm writing. The script is divided up, with section for setting variable at the top, then functions (which make up most of the script) then basically a line at the end which calls the first function- the program moves between the... (5 Replies)
Discussion started by: alexop
5 Replies

3. UNIX for Dummies Questions & Answers

Change to ksh shell

Hi everyone: Mi default shell is sh, i need to set the TMOUT variable so i need to change the shell to ksh, I change the /etc/profile file as follows, but when i do an echo $0 stll sh been the shell: # more /etc/profile # # (c) Copyright 1990, OPEN SOFTWARE FOUNDATION, INC. # ALL RIGHTS... (2 Replies)
Discussion started by: irasela
2 Replies

4. UNIX for Dummies Questions & Answers

How can I change the behavior of the mouse in Terminal?

Hi, I hope I'm posting in the correct forum, so here goes. I would like for the mouse right/left click to work in Terminal (OSX) as it does in Putty. For instance, when I double click 'log' in file.log.gz, only the log part is highlighted (in Terminal), while in Putty the entire file name is... (0 Replies)
Discussion started by: TheChemist
0 Replies

5. UNIX for Advanced & Expert Users

How to change the permissions of ksh?

Suppose, I want to execute unix commands. For that I have to go to ksh, but if I don't have execute permission to ksh itself then is there any way to change the permission of ksh? chmod command does not work for this because, I don't have permission to ksh itself... :) Let me know, if you have... (3 Replies)
Discussion started by: ponnuvel
3 Replies

6. Shell Programming and Scripting

ksh script to change passwd

Hello All, I am trying to change a user passwd (one time password): cat /tmp/passwd mnop1234 mnop1234 #passwd abcd < /tmp/passwd (for some reason, it is not able to input the password from /tmp/passwd and comes back with "New Password: ") Is there a work around except using "expect". ... (1 Reply)
Discussion started by: solaix14
1 Replies

7. Shell Programming and Scripting

change output format using ksh

I have a script that reaches out to several systems and pulls back infomation in serveral files. I would like to take the infomation returned and reformat it so I can export it to excel. Below is current output: File1:item1:abc=1 File1:item2:efg File2:item1:ab=1 File2:item2:efg... (3 Replies)
Discussion started by: oldman2
3 Replies

8. Shell Programming and Scripting

ksh behavior in scripts spawned w/nohup

I have a need to run any number of identical scripts simultaneously, so I've created a driver script which reads a template script, edits these appropriately and then submits them via nohup. The spawned scripts all check to see at some point how many of their number are running and once the count... (7 Replies)
Discussion started by: safedba
7 Replies
All times are GMT -4. The time now is 05:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy