2
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

I am confusing with above 2 things.

I came to know that $0 is file name , as well we are getting the filename using argv0.

And argv are the arguments to the file , same as "$@" , But why should we use both the things in our sritps.

potrzebie
  • 1,768
  • 1
  • 12
  • 25
user2720323
  • 277
  • 3
  • 6
  • 11

3 Answers3

6

The confusion comes because $0 and ${1+"$@"} is not tcl syntax. It is shell syntax, and has nothing to do with tcl.

When you have a script like this, and you run it from the command line, the system thinks it is a shell script and will start to run each line as if it were a shell command. At this point, argv0 and similar variables don't exist.

Since this is actually a tcl script. the first thing we want the shell to do is to stop the shell and start tclsh. To do that, we exec tclsh. Also, we must make sure -- using shell syntax -- that the arguments are passed to tclsh properly. Hence, we must use $0 and similar constructs. Once tclsh has started and the shell is no longer executing, $0 disappears and argv0 is set.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1
     #!/bin/sh
     # the next line restarts using tclsh \
     exec tclsh "$0" ${1+"$@"}

This approach has three advantages. First, the location of the tclsh binary does not have to be hard-wired into the script: it can be anywhere in your shell search path. Second, it gets around the 30-character file name limit in the previous approach. Third, this approach will work even if tclsh is itself a shell script (this is done on some systems in order to handle multiple architectures or operating systems: the tclsh script selects one of several binaries to run). The three lines cause both sh and tclsh to process the script, but the exec is only executed by sh. sh processes the script first; it treats the second line as a comment and executes the third line. The exec statement cause the shell to stop processing and instead to start up tclsh to reprocess the entire script. When tclsh starts up, it treats all three lines as comments, since the backslash at the end of the second line causes the third line to be treated as part of the comment on the second line.

where as,

argv : argument variable list

argc : argument variable list count

argv0 : first in the argument variable list

Vishwadeep Singh
  • 1,043
  • 1
  • 13
  • 38
  • I agreed but why we will use this exec tclsh "$0" ${1+"$@"} , we can execute (complete or validate) the script directly by the use of argc , argv , argv0 . what is the exact use exec tclsh "$0" ${1+"$@"} and involving shell in this . – user2720323 Sep 12 '13 at 10:53
  • You say "... in the previous paragraph". What paragraph are you talking about? The only previous block of text in your answer is code. Is that what you're talking about? Can you rephrase, to make it more clear? – Bryan Oakley Sep 12 '13 at 10:53
  • @user2720323 This line is actually meant for shell for executing script by tclsh interpreter.. exec will be called for shell and $0 for script file to be interpreted in tclsh – Vishwadeep Singh Sep 12 '13 at 11:00
  • @BryanOakley Thanks for pointing it out. Information is corrected. – Vishwadeep Singh Sep 12 '13 at 11:00
1

$0 for tcl script path and ${1+$@} means "all the arguments, if the first argument is set" hence,

 exec tclsh $0 ${1+$@} 

means after stopping shell, execute tclsh by passing script file (via $0) and rest arguments (via ${1+$@})

When tclsh starts, tcl variables like argv, argv will come into picture.

Vishwadeep Singh
  • 1,043
  • 1
  • 13
  • 38