Running scripts without a hashbang - ksh anomaly?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Running scripts without a hashbang - ksh anomaly?
# 1  
Running scripts without a hashbang - ksh anomaly?

I noticed some strange looking parameters on some processes on one of our servers, and after a little playing around we deduced that ksh seemed to be adding a (somewhat random) extra parameter when calling a script without a hashbang in it.

It looks like it's the partial name of the parent script (and not always the same number of characters). I'm thinking it's not just a display artifact as it's in /proc/xxx/cmdline as well. However, the script arguments don't see it.

It doesn't happen if the child script has a hashbang or is called explicitly with ksh, or if the parent script is run with bash.

It doesn't appear to be causing any actual problem for the real script, but it seems a tad odd.


Can anyone shed any light on this?

An example:
Code:
$ cat SomeScriptOrOther.sh
#!/bin/ksh

ksh ./sleep.sh PARAM1 PARAM2
./sleep.sh PARAM1 PARAM2

echo "Wibble"

$ cat sleep.sh
echo $# "$@"
ps -Hfu $USER
sleep 1
$ ./SomeScriptOrOther.sh
2 PARAM1 PARAM2
UID        PID  PPID  C STIME TTY          TIME CMD
14347     8996  8989  0 15:53 ?        00:00:00 sshd: carlo@pts/0
14347     8997  8996  0 15:53 pts/0    00:00:00   -bash
14347    11273  8997  0 16:00 pts/0    00:00:00     /bin/ksh ./SomeScriptOrOther.sh
14347    11274 11273  0 16:00 pts/0    00:00:00       ksh ./sleep.sh PARAM1 PARAM2
14347    11275 11274  0 16:00 pts/0    00:00:00         ps -Hfu carlo
2 PARAM1 PARAM2
UID        PID  PPID  C STIME TTY          TIME CMD
14347     8996  8989  0 15:53 ?        00:00:00 sshd: carlo@pts/0
14347     8997  8996  0 15:53 pts/0    00:00:00   -bash
14347    11273  8997  0 16:00 pts/0    00:00:00     /bin/ksh ./SomeScriptOrOther.sh
14347    11278 11273  0 16:00 pts/0    00:00:00       ./sleep.sh PARAM1 PARAM2 her.sh
14347    11279 11278  0 16:00 pts/0    00:00:00         ps -Hfu carlo
Wibble

Code:
$ ksh --version
  version         sh (AT&T Research) 93t+ 2010-06-21

# 2  
The man page for the execvp() call tells what happend to scripts without a #!. They go to sh stdin usually. Permissions bits have no effect on the process except it must not be no read.
# 3  
Note that what /bin/sh is can vary from system to system. On Linux you might get BASH or DASH. Some other UNIX you might get KSH. In Solaris you might get old-fashioned, 1970's-era sh as written by Bourne himself...
# 4  
I'm aware that sh (which is bash on this system) is used to execute scripts without a specified executable, but how does that explain where the extra 'ghost' parameter is coming from (and only when the parent script is run with ksh, not with bash)?
# 5  
With the shebang, the OS loads the interpreter. without the shebang, the shell implements this. Must be how ksh93 copies the environment, overwriting the parent's without zeroing it first? Just looking briefly, it may start with sh_fork()/ sh_ntfork() in xec.c
# 6  
The hash-bnag supports one parameter for commands like awk and sed that need a parameter to introduce a script. So, script parameters shift right in the real world, "sed -f scriptname $@" or "ksh scriptname $@". Dunno about ghosts. Try running it under truss/tusc/strace -fae (follow forks, report args and environment) and learn what is really happening.
# 7  
ksh does indeed have a special way to deal with child scripts that have no #! leading line. This introduces a number of special features that expands the traditional unix environment. These expanded features are very non-standard. Basically ksh then also you to export arrays, functions, and aliases to the environment. My feeling is that this is too non-standard. It's well documented, but as this thread attests, few people read the ksh documention. This makes exported arrays, functions, and aliases a support issue even when they are used correctly. Even worse are folks who unintentionally export arrays, functions, and aliases and then are bewildered by the result.

My advice is to always use #! in every script every time.

ksh cannot rely on the standard Unix environment for this stuff. So it implements non-#! child scripts by simply forking. The forked child ksh process knows every variable, array, function, and aliases that the parent knew including stuff that was not exported to the child script. So the child ksh process takes a moment and deletes everything not explicitly exported to the child script. Then the child script can run apparently pulling exported arrays, functions, and aliases from the "environment".

In "ps" this child ksh process will look like a duplicate of the parent. It is. It was forked from the parent. Some Unix systems allow a process to change the arguments that appear in "ps". ksh tries to do this. There is no guarantee that it will succeed. In this case, the final 6 characters of the original arguments were not over written. I might call that a "bug". I might even report it if I actually cared about non-#! ksh scripts.
These 5 Users Gave Thanks to Perderabo For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #492
Difficulty: Medium
Transpilers are source-to-source compilers that directly produce assembly or machine code,
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Restrict access to .ksh scripts

Hi, How to restrict access to a .ksh script in such the way that the users can only execute the script, neither read nor write. I tried the below code so that my user alone has the rwx and other users can only execute. chmod 711 sample.ksh But when I logged in as a different user... (26 Replies)
Discussion started by: machomaddy
26 Replies

2. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

3. Shell Programming and Scripting

hashbang line

Hi All, I am new to this forum. I would really appreciate if some one from you expert team could answer my qns: 1) whats the difference between the below commands. what events occur in the background when I fire each of the three commands. >./script.ksh >sh script.ksh >script.ksh ... (11 Replies)
Discussion started by: agrawal.prachi
11 Replies

4. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

5. Shell Programming and Scripting

Ksh Associating scripts

Im writing a script in the Ksh, as the title suggests. OK so im sincerely tring to be lazy. Im trying to make a script that will use another file as a sort of variable library So basically i dont need to include the variables themselves, just want to make a reference to the file, so the... (2 Replies)
Discussion started by: Demon002
2 Replies

6. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

7. Shell Programming and Scripting

Running scripts via su

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin .profile when i execute the command start.sh by logging directly into bin account it's... (8 Replies)
Discussion started by: ravi.sri24
8 Replies

8. Shell Programming and Scripting

converting ksh scripts to sh

Hello All, I have a whole bunch of shell scripts written in a ksh environment and which successfully execute there. However, I found out that they eventually need to be used in a sh environment. So some commands like some_variable=$(some_command) fail because sh doesn't understand $(.....). I... (11 Replies)
Discussion started by: sherkaner
11 Replies

9. Shell Programming and Scripting

executing variables in ksh scripts?

In a ksh script on an AIX box running a jillion oracle database processes, I'm setting a variable to one of two possible arguments, depending on cmd line arguments. FINDIT="ps -ef | grep oracle | grep DBexport | grep rshrc" -or- FINDIT="ps -ef | grep oracle | grep prod | grep runback" I... (3 Replies)
Discussion started by: zedmelon
3 Replies

Featured Tech Videos