why the script name not displayed and not sh invoked?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users why the script name not displayed and not sh invoked?
# 1  
Old 03-30-2009
why the script name not displayed and not sh invoked?

Say there is a shell script named test.sh. I intentionally omit the #! line in test.sh for testing perpose. I did the following :

$ echo $0
-ksh ---> current shell
$ echo $$
12919 ---> PID of the current shell
$./test.sh
----> test.sh contains a sleep command to prevent it from terminating immediately

Then I switched to another terminal session to continue my testing:

Code:
$ ps -ef|grep 12919|grep -v grep
wesadmin  5083 12919  0 10:10:27 pts/3     0:00 -ksh
wesadmin 12919  7616  0 08:10:37 pts/3     0:00 -ksh

You can see that the process id associated with the script test.sh is 5083.

Questions here:

1. Why shell itself displayed in the process list without test.sh as the argument? We usually expect " -ksh test.sh " displayed. My another testing shows that the argument test.sh can be displayed if #! line added in the script.

2. Why the invoked shell is ksh instead of ssh? To my knowledge,, if #! line not specified explicitly, the default shell, i.e. sh is used as the interpreter .


Thanks!

Last edited by Yogesh Sawant; 03-30-2009 at 11:40 AM.. Reason: added code tags
# 2  
Old 03-30-2009
My guess would be that without the shebang line (#!), the script automatically is executed by the current shell (maybe in a subshell as it seems from the example you gave). With the shebang line the script is passed to an exec call, where the system, instead of the currently running shell, decides which interpreter to start.
# 3  
Old 03-30-2009
# 4  
Old 03-30-2009
Yes, the following words from The Whole Story on #! /usr/bin/ksh can explain my doubt ( in fact, I also guessed ksh works this way for this case)

Quote:
Let's say that you use ksh exclusively and you have a script and you leave off the "#!". Your interactive ksh will try to exec the script and fail. So your interactive ksh with fall back to running your script as a ksh script. It does this by forking a copy of itself to create a subshell.
However my confusion is still not completly clarified. I aways told that if #! line is not specified, sh is invoked as the default interpreter. Is it wrong? And for the statment '...ksh will try to exec the script and fail ...', why exec fail? I did some test by making a small c program in which I try to execute a script without #! line using execl ( ...) system call, it works! In other words, execl can execute this kind of script successfully and morever the sh is invoked as defaut shell as the interpreter of the script! Then why doesn't ksh use exec() system call directly to handle a script without #! line? And also, I did adittional tests in posix-sh and bash enviroment, seems that both of them treat a script without shabang in the same way as ksh. Again, why don't they exec() directrly to execute the script?

Last edited by Yogesh Sawant; 03-30-2009 at 11:42 AM.. Reason: added quote tags
# 5  
Old 03-31-2009
Your remaining questions have mostly been answered in the linked article. But as for why things are this way... A shell script cannot truly be exec'ed. A program must be in machine language for the kernel to be able to transfer control to it. The machine language is very close to assembler. A machine language program will just be a binary file. The cpu cannot directly run a shell script. The kernel must actually exec an interpreter (some shell) to run the shell script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Oracle function invoked from shell script doubt

hi gurus, I have tried myself to invoke an oracle function. there are three different function available need to be called for differnt. can you tell me whether the below code is correct to call oracle function from shell script. Any help would be highly appreciated. cat location.sh ... (5 Replies)
Discussion started by: arun888
5 Replies

3. Shell Programming and Scripting

Problem with script invoked from a keyboard shortcut

-EDIT- I have solved my problem below by using a different program. Instead of xsel I am using xclip which basically does the same thing and works fine from a script invoked by a global hotkey. -END EDIT- Hi, I've written a simple script to copy my email address into both the... (0 Replies)
Discussion started by: gencon
0 Replies

4. AIX

How to know the first time a daemon was invoked?

I have an investigation job at hand where I have to know when was the FIRST time the daemon was invoked. Now, currently what I can see as the time since invoked was some where near the last reboot which is kind of obvious that the daemon stopped and recycled after reboot. But I want to know the... (6 Replies)
Discussion started by: TH3M0Nk
6 Replies

5. Shell Programming and Scripting

How to ensure a script can only be invoked from another?

Hi All, I have two scripts - ScriptA and ScriptB ScriptA has logic to invoke ScriptB : - with some parameter - or without any parameter ScriptB can also be invoked by the user from the command line. Is there anyway to ensure that when I execute ScriptB from the command line, it does... (3 Replies)
Discussion started by: chaitanya.gvc
3 Replies

6. Shell Programming and Scripting

Script invoked using "sh" is not executing. Urgent help required

Hi , I am new to shell scripting. I am using Linux for doing scripting. Below is my script, which takes 2 parameters as input. test.sh has the below: #!/bin/bash . $HOME/.profile gpg --yes --no-use-agent -r "$(eval echo \$$2_Var)" -e $1 1st parameter is command line... (7 Replies)
Discussion started by: rangarb
7 Replies

7. Shell Programming and Scripting

CRON: Script not getting invoked

Hi, I have the following script - fixpart="/files/myScript # Transfer Script" echo "Specify the transfer frequency in minutes - " echo "every 1, 2, 3, or 5 minutes (default every 1 minute) " echo $nn "Frequency ? :" $cc read ans case $ans in 2) echo... (9 Replies)
Discussion started by: angshuman_ag
9 Replies

8. UNIX for Advanced & Expert Users

Passing the values to the secondary script when it invoked by primary script

Hi, When I invoke a script s1.sh it will call an another script s2.sh by itself. This script s2.sh will call some java files, so while running the script it asks for a file name to be processed. Which we can see in the screen. The file name to be processed is present in script s1.sh Now I... (2 Replies)
Discussion started by: venu_eie
2 Replies

9. Shell Programming and Scripting

Passing the values to the secondary script when it invoked by primary script

Hi, When I invoke a script s1.sh it will call an another script s2.sh by itself. This script s2.sh will call some java files, so while running the script it asks for a file name to be processed. Which we can see in the screen. The file name to be processed is present in script s1.sh Now... (1 Reply)
Discussion started by: venu_eie
1 Replies

10. Shell Programming and Scripting

determine if the script has been invoked manually or not?

Hi, Is there a way to determine if the script has been invoked manually or not( might be invoked by a schedular or crontab)? Thanks, (8 Replies)
Discussion started by: hitmansilentass
8 Replies
Login or Register to Ask a Question