Tags: cat, created, dot-execute, dummy, execute, home, ksh, linux, programming, relatedto, result-, script, shell, spdwpss, unix

Difference between dot-execute and normal execute in ksh shell

On Programmer » Unix & Linux

7,547 words with 6 Comments; publish: Mon, 12 May 2008 14:07:00 GMT; (20061.52, « »)

Hi all,

I have created a dummy script to test something related

to dot-execute a shell script.

Here is my testing result:

---

/home/spdwpss > cat dummy_script

true

/home/spdwpss > dummy_script HI, World

/home/spdwpss > echo $1 $2 $3 $4 $5 $6

/home/spdwpss > . dummy_script HI, World

/home/spdwpss > echo $1 $2 $3 $4 $5 $6

HI, World

/home/spdwpss >

---

I am using

AIX 5.2

ksh

It seems that, when dot-execute a shell script

the command line parameters are stored-up

and then the next command can use these stored-up parameters.

Any explaination on this quite strange behaviour ?

Anyway, my testing is just for fun.

Appreciate for all helps and suggestion.

Alvin SIU

All Comments

Leave a comment...

  • 6 Comments
    • In article <c558on$ret12.unix-linux.itags.org.imsp212.netvigator.com>,

      Alvin SIU <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      > It seems that, when dot-execute a shell script

      > the command line parameters are stored-up

      > and then the next command can use these stored-up parameters.

      > Any explaination on this quite strange behaviour ?

      When you use the "." command, the script is executed by the current

      shell. If any variables are assigned, these changes will be visible in

      the shell after the script completes (this method of execution is

      typically only used for scripts whose purpose is to change variables in

      this way).

      When you execute a script the normal way, a new shell process is

      started. Any variable changes will take place in that process, but will

      have no effect on the original process.

      Whether or not the "." command allows parameters to be supplied depends

      on the particular shell you're using. But if it supports it, it's

      consistent with the rest of the behavior of the "." command that it

      should persist.

      Barry Margolin, barmar.unix-linux.itags.org.alum.mit.edu

      Arlington, MA

      *** PLEASE post questions in newsgroups, not directly to me ***

      #1; Mon, 12 May 2008 14:08:00 GMT
    • Barry Margolin wrote:

      > In article <c558on$ret12.unix-linux.itags.org.imsp212.netvigator.com>,

      > Alvin SIU <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      >

      >

      > When you use the "." command, the script is executed by the current

      > shell. If any variables are assigned, these changes will be visible in

      > the shell after the script completes (this method of execution is

      > typically only used for scripts whose purpose is to change variables in

      > this way).

      > When you execute a script the normal way, a new shell process is

      > started. Any variable changes will take place in that process, but will

      > have no effect on the original process.

      > Whether or not the "." command allows parameters to be supplied depends

      > on the particular shell you're using. But if it supports it, it's

      > consistent with the rest of the behavior of the "." command that it

      > should persist.

      >

      Thanks for your information.

      This bring out anohter question.

      If my dummy_script looks like this:

      echo $0

      then what will be echo ?

      Surely, normal execution of the script like:

      $ dummy_script

      will give the script name "dummy_script"

      But, if dot-executing it like:

      $ . dummy_script

      what will be displayed ?

      If it displays

      /bin/ksh

      (which is my login shell),

      how can I get back the name "dummy_script" ?

      By the way, this dot-executing behaviour is quite interesting.

      Could anyone show me any web sites with more details.

      Also, is there any 'standard' on implementing this dot-executing behaviour ?

      Appreciate for any help and information.

      Alvin SIU

      #2; Mon, 12 May 2008 14:09:00 GMT
    • Barry Margolin wrote:

      > In article <c55e0m$ren14.unix-linux.itags.org.imsp212.netvigator.com>,

      > Alvin SIU <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      <snip>

      >

      > You can't.

      Yes, you can. Put this in a script and excute it as ". script":

      set -- `fc -l -0`

      shift 2

      echo $.unix-linux.itags.org.

      and it'll print out the name of the script plus it's arguments.

      It works in ksh88, I haven't tried other shells but I assume this or

      something similair will work.

      Regards,

      Ed.

      #3; Mon, 12 May 2008 14:10:00 GMT
    • On Fri, 09 Apr 2004 12:28:14 +0800, Alvin SIU

      <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      > It seems that, when dot-execute a shell script

      > the command line parameters are stored-up

      > and then the next command can use these stored-up parameters.

      > Any explaination on this quite strange behaviour ?

      >

      This behavior doesn't seem to exist in bash, pdksh or zsh under Linux.

      (Yet another difference between pdksh and the real Korn shell).

      However, in bash or zsh, if you use the "set" command to set

      parameters in a sourced script, the parameters remain after the

      script is finished. ("source" is a synonym for "." in bash and zsh).

      Nothing is illegal if one hundred businessmen decide to do it.

      -- Andrew Young

      #4; Mon, 12 May 2008 14:12:00 GMT
    • "Bill Marcum" <bmarcum.unix-linux.itags.org.iglou.com.urgent> wrote in message news:022jk1-6rc.ln

      1.unix-linux.itags.org.don.localnet...

      : On Fri, 09 Apr 2004 12:28:14 +0800, Alvin SIU

      : <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      : > It seems that, when dot-execute a shell script

      : > the command line parameters are stored-up

      : > and then the next command can use these stored-up parameters.

      : >

      : > Any explaination on this quite strange behaviour ?

      : >

      : This behavior doesn't seem to exist in bash, pdksh or zsh under Linux.

      : (Yet another difference between pdksh and the real Korn shell).

      : However, in bash or zsh, if you use the "set" command to set

      : parameters in a sourced script, the parameters remain after the

      : script is finished. ("source" is a synonym for "." in bash and zsh).

      It's not POSIX behaviour. It was removed from ksh93 and probably

      later (posixified) versions of ksh88.

      Dan Mercer

      :

      :

      : --

      : Nothing is illegal if one hundred businessmen decide to do it.

      : -- Andrew Young

      #5; Mon, 12 May 2008 14:12:00 GMT
    • Bill Marcum <bmarcum.unix-linux.itags.org.iglou.com.urgent> wrote:

      > On Fri, 09 Apr 2004 12:28:14 +0800, Alvin SIU

      > <ANTI.SPAM.unix-linux.itags.org.anti.spam.com> wrote:

      > This behavior doesn't seem to exist in bash, pdksh or zsh under Linux.

      man bash:

      . filename [arguments]

      source filename [arguments]

      Read and execute commands from filename in the current shell environment and

      return the exit status of the last command executed from filename. If file?

      name does not contain a slash, file names in PATH are used to find the

      directory containing filename. The file searched for in PATH need not be

      executable. When bash is not in posix mode, the current directory is

      searched if no file is found in PATH. If the sourcepath option to the shopt

      builtin command is turned off, the PATH is not searched. If any arguments

      are supplied, they become the positional parameters when filename is exe?

      cuted. Otherwise the positional parameters are unchanged. The return sta?

      tus is the status of the last command exited within the script (0 if no com?

      mands are executed), and false if filename is not found or cannot be read.

      William Park, Open Geometry Consulting, <opengeometry.unix-linux.itags.org.yahoo.ca>

      Linux solution/training/migration, Thin-client

      #6; Mon, 12 May 2008 14:13:00 GMT