exec tclsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exec tclsh
# 1  
Old 10-06-2009
Question exec tclsh

As a part of learning shell scripting I was just going through some already created scripts there I found-

Code:
exec tclsh "$0" $(1+"$@")

Here I was able to find what exec ,tclsh does & o/p of same but I could not find usage & output of $(1+"$@").

Can anybody pls expalain me usage details of it?
# 2  
Old 10-06-2009
I do not recognize that syntax, what shell is this?
# 3  
Old 10-07-2009
I ma using this on sh shell.
# 4  
Old 10-07-2009
That just doesn't look right. sh doesn't do string concatenation or math like that, and trying to duplicate it in my shell just gives me syntax errors. Are you sure that script is using sh? Does it have #!/bin/sh at the top or something else?
# 5  
Old 10-07-2009
Hi.
Code:
       An even better approach is to start your script files with the  follow-
       ing three lines:

              #!/bin/sh
              # the next line restarts using tclsh \
              exec tclsh "$0" "$@"

       This  approach  has  three advantages over the approach in the previous
       paragraph.  First, the location of the tclsh binary doesn't 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.

-- excerpt from man tclsh

For example:
Code:
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

# @(#) tcl5     Demonstrate tclsh feature.

set version [ info tclversion ]
set message " Hello, world from tclsh ($version), $auto_path"
puts stdout $message

puts stdout ""
if { $argc == 0 } then {
  puts stdout " No parameters provided."
} else {
  set i 0
  foreach arg $argv {
    set m1 " $i $arg"
    incr i
    set m2 ""
    if { [ file exists $arg ] } then {
      set m2 [ join [ list "(" [ file type $arg ] ")" ] ]
    }
    puts stdout "$m1 $m2"
  }
}

exit 0

producing:
Code:
% ./tcl5
 Hello, world from tclsh (8.4),...

 No parameters provided.

and:
Code:
% ./tcl5 a x=1 3.14
 Hello, world from tclsh (8.4), ...

 0 a 
 1 x=1 
 2 3.14

The "1+ .." part might be taken from perl:
Code:
           #!/bin/sh -- # -*- perl -*- -p
           eval 'exec perl -wS $0 ${1+"$@"}'
               if $running_under_some_shell;
-- excerpt from man perlrun

You might be able to fiddle with single quotes, specific shells, etc., to make the command in the OP work . Best wishes ... cheers, drl
# 6  
Old 10-08-2009
What does ${1+"$@"} mean?

Hi.

An explanation for the syntax:
Code:
${1+"$@"}

is not so easy to find, however, some information is at What does ${1+"$@"} mean?

basically, it's for very old Bourne shells -- not so many around these days, but there are a few as noted in the page at the URL cited ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

2. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

3. UNIX for Dummies Questions & Answers

exec

Hi, i don't understand this part of one script exec >> $Log_File 2>&1 (1 Reply)
Discussion started by: messi777
1 Replies

4. UNIX for Advanced & Expert Users

-exec cp

Hi, on AIX 6.L I want to copy the result of grep -v to test directory then : `hostname`@oracle$ls -l | grep -v RINT -exec cp {} test grep: can't open -exec grep: can't open cp grep: can't open {} test:°`. Can you help me ? Thank you. (3 Replies)
Discussion started by: big123456
3 Replies

5. Programming

exec tclsh $(1+"$@")

As a part of learning shell scripting I was just going through some already created scripts there I found- exec tclsh "$0" $(1+"$@") Here I was able to find what exec ,tclsh does & o/p of same but I could not find usage & output of $(1+"$@"). Can anybody pls expalain me usage... (1 Reply)
Discussion started by: rediffmail
1 Replies

6. Shell Programming and Scripting

How to set Field Separator for TCLSH??? :S

Hi, I am having a problem, with setting the FS in TCLSH, maybe someone knows the answer. I have a Tclsh/Tk script, and i´m trying to get all (only) the running processes for the given user like this: foreach process { puts stdout $process; } This also works, but... (2 Replies)
Discussion started by: laptop87
2 Replies

7. Shell Programming and Scripting

Can you explain me a tclsh file?

Hello, I got a file I am using, but I cannot understand what this file is doing... Can someone explain? Here is a part of the file: #! /usr/bin/tclsh set mctal r] set pow set SS set a set b set c set d set e set f set g set h (0 Replies)
Discussion started by: jolecanard
0 Replies

8. Shell Programming and Scripting

Forking with Tclsh vs Wish

Hello, I am new to this site, so sorry ahead of time if this is not the right place for this question.......anywhooooo I am having troubles with forking new processes in wish. Take the following code example: **************************** package require Tclx puts "TCL VER: " proc... (3 Replies)
Discussion started by: pghamami
3 Replies

9. Shell Programming and Scripting

tclsh

In bash wen we press tab the name of the files get completed automatically. Likewise there is no auto completion feature in tclsh. Any idea if there is any way to get this feature or should we implement ?? Please suggest (0 Replies)
Discussion started by: Laksmi
0 Replies

10. UNIX for Advanced & Expert Users

exec

I have read that exec "replaces the current process with a new one". So I did $ exec ls and after this executed, my shell disappeared. I am assuming that my shell had PID xyz, and when I did exec ls, this ls got pid xyz, and when it terminated, there was no more shell process running, and... (5 Replies)
Discussion started by: JamesByars
5 Replies
Login or Register to Ask a Question