Difference Between executing llike ./myscript.ksh and . ./myscript.ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference Between executing llike ./myscript.ksh and . ./myscript.ksh
# 1  
Old 03-21-2011
Question Difference Between executing llike ./myscript.ksh and . ./myscript.ksh

Hi ,

What is the diffence between executing the script like
Code:
./myscript.ksh 
. ./myscript.ksh

I have found 2 difference but could not find the reason
1. If i export a variable in myscript.ksh and execute it like . ./myscript.ksh the i can access the other scripts that are present in same directory. But when i execute like ./myscript.ksh then its not availble.

2. If i call a another script from myscript.ksh like . ./myscript_child.ksh and if myscript_child.ksh exits with a due to error i am not able to catch the error code in myscript.ksh but when is call it like ./myscript_child.ksh then i am able to catch the error code

Regards ,
Digvijay Singh

Last edited by Franklin52; 03-21-2011 at 07:45 AM.. Reason: Please use code tags, thank you
# 2  
Old 03-21-2011
The "." command is executing the script in the calling shell (a.k.a. sourcing it) instead of launching a subshell. This explains the differences you observed.
This User Gave Thanks to jlliagre For This Post:
# 3  
Old 03-21-2011
thanks jilliagre

that means the execution like . ./my_script.ksh is same as sh my_script.ksh ?
# 4  
Old 03-21-2011
Quote:
Originally Posted by max_hammer
thanks jilliagre

that means the execution like . ./my_script.ksh is same as sh my_script.ksh ?
No.
. ./my_script.kshexecutes the script within your current (probably login) shell. sh my_script.ksh creates a new shell in a child process and executes within that. Neither of them obeys the shebang (#!) in the script.

sh my_script.kshis roughly equivalent to ./my_script.ksh, but only if the shebang specifies /bin/sh as the executing shell.
# 5  
Old 03-21-2011
If you specify . ./file.ksh, it's like running the contents of that file in your current shell. It's a bit smarter than this, but think of it running:
while read line; do "$line"; done <file.ksh
Since it's run in the current shell, any variables it sets, for example, are available in your current shell.

If you run sh file.ksh, you're running "sh" (which may be linked to ksh or bash or whatever) with file.ksh as input. In many cases, "sh" is not the same as "ksh", so different syntax applies. It's run in a child process, though, so variables you set are not available later in your current shell.

If you run ./file.ksh, the first 4 bytes of the file (the file "magic") are read, determined to be a script, and the executable, if available, identified after the shbang is run, feeding the file as input. Also run in a child, so no variables are available after control is passed back to your current login shell.

In basic cases, they may all lead to successful execution of the script - it depends on the script. They're all different ways of executing, though, and follow slightly different rules for syntax - this is a hand-wavy explanation, of course, but I think it covers the gist of what you're asking.

Here's a fun example:
Code:
$ cat file.cat
#! /bin/cat

$

So, my file called "file.cat" contains the above.
Code:
$ . ./file.cat
$

My shell (bash) sources the file, and finds only a line beginning with "#", which is a comment, and a blank line. Nothing to do here - no output.
Code:
$ sh ./file.cat
$

I ran "sh" (although on my system, it is still bash, it follows slightly different behavior when called as "sh") with the file as input, still only finds a comment and blank line - nothing to do here.
Code:
$ ./file.cat
#! /bin/cat

$

This is kind of cool, actually. When invoked, the file is determined to be executable, and a script. Since the interpreter after the shbang is /bin/cat, what is essentially run is /bin/cat ./file.cat

This also demonstrates a quine.
This User Gave Thanks to LivinFree For This Post:
# 6  
Old 03-21-2011
thank you LivinFree

now the thinghs are more clear to me Smilie
your example was cool

Regards,
Digvijay Singh
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Two for loops in ksh script only one not executing

Hello, I have two "for loops" in my script and the second one is not executing the way i want. Script: #!/bin/ksh IFS=' ' printf "Enter Account name: " read A B C D E F G H I J K L M N O for i in ${A} ${B} ${C} ${D} ${E} ${F} ${G} ${H} ${I} ${J} ${K} ${L} ${M} ${N} ${O};... (3 Replies)
Discussion started by: seekryts15
3 Replies

2. Shell Programming and Scripting

executing ksh script

I am trying to call a ksh script from another ksh script. in the called script , i am doing sum calculation(used typeset etc) suppose a.ksh is the calling script and b.ksh is the called script . . b.ksh (used this inside a.ksh) this execution gives some error like bad number. but when i... (1 Reply)
Discussion started by: urfrnddpk
1 Replies

3. Shell Programming and Scripting

forking question myscript vs . myscript vs exec myscript

I have a question about the following what is the difference between running myscript vs . myscript vs exec myscript ? I know that when you just run myscript it will fork a child process and run the script and exits. What does the . myscript and exec myscript do and why is it different and... (1 Reply)
Discussion started by: jlim0930
1 Replies

4. UNIX for Dummies Questions & Answers

problem in Executing script in ksh

Hi, I am in ksh. below mentioned 3 commands I executed at command prompt & got the expected results. csh source csh_infa <special command> Now I have to do this in the script in ksh. I entered it as it is. #!/bin/ksh csh source csh_infa <special command> Now after... (1 Reply)
Discussion started by: girish_kanak
1 Replies

5. Shell Programming and Scripting

Error in executing ksh from Windows

Hi All, I wrote a script to send a mail if a particular logfile did not get updated since 5 mins. Here is the Script. log=`date +%e_%b_%Y`.log # returns todays logfile x=`date -r $log` # returns last modification time of the Log file h1=`expr substr "$x" 12 2` m1=`expr substr "$x" 15 2`... (5 Replies)
Discussion started by: AnneAnne
5 Replies

6. UNIX for Dummies Questions & Answers

Specified Ksh but executing in bash

Hi, I am a new bie to unix shell programming. I have specified ksh as the first line in my script but when executed it complains. *********** Script=test******************** #!/usr/bin/ksh echo hello print -p 123 ************************************ ++++++++ Execution... (7 Replies)
Discussion started by: akhilnagpal
7 Replies

7. Shell Programming and Scripting

ksh: difference between $* and $@

Please ignore. I found the answer at: https://www.unix.com/shell-programming-scripting/24557-difference-between.html (1 Reply)
Discussion started by: JamesByars
1 Replies

8. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 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

10. Shell Programming and Scripting

Executing ksh script from cgi

Hi all, I'm developing a system which requires me to run a ksh script from within a cgi script. What sort of syntax will I need to do this, I'm sure it's simple but can't find out how anywhere! Thanks. (1 Reply)
Discussion started by: hodges
1 Replies
Login or Register to Ask a Question