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:
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
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.
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
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.
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)
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)
-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)
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)
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)
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)
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)
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)
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)