C shell script passing arguments problem.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting C shell script passing arguments problem.
# 1  
Old 01-11-2017
C shell script passing arguments problem.

I found something insteresting when I tested passing arguments into my scripts.
My scripts is as below.

Code:
% cat passarg.env 
#!/bin/csh 

echo "passarg: argv[1] = $argv[1] argv[2] = $argv[2]"
passarg1.env $*

Code:
% cat passarg1.env
#!/bin/csh

echo "passarg1: argv[1] = $argv[1] argvp[2]=$argv[2]"
set str = "test passing arguments"
source passarg2.env "$str"

Code:
% cat passarg2.env
#!/bin/csh

echo "passarg2: argv[1] = $argv[1]"

When I test my scripts on Solaris 8. The results showed as following.
Code:
% passarg.env abc cde
passarg: argv[1] = abc argv[2] = cde
passarg1: argv[1] = abc argvp[2]=cde
passarg2: argv[1] = abc

But when I tested it on RHEL 6.5 the results showed as below.
Code:
% passarg.env abc cde
passarg: argv[1] = abc argv[2] = cde
passarg1: argv[1] = abc argvp[2]=cde
passarg2: argv[1] = test passing arguments

Ideally, the Linux results should be correct.
Unfortunately, I don't know how to check Solaris built-in csh version
I only found tcsh package info as below.
Code:
% pkginfo -l SUNWtcsh
   PKGINST:  SUNWtcsh
      NAME:  Tenex C-shell (tcsh)
  CATEGORY:  system
      ARCH:  sparc
   VERSION:  11.8.0,REV=2000.01.08.18.12
   BASEDIR:  /
    VENDOR:  Sun Microsystems, Inc.
      DESC:  Tenex C-shell (tcsh)
    PSTAMP:  on28-patch20040130230639
  INSTDATE:  Sep 19 2007 16:10
   HOTLINE:  Please contact your local service provider
    STATUS:  completely installed
     FILES:        7 installed pathnames
                   5 shared pathnames
                   5 directories
                   1 executables
                1034 blocks used (approx)

I kept wondering this should be a bug of Solaris 8 C-shell.
Does anybody have idea?
Except upgrading C-shell version, is there any workaround to solve this issue?

Thanks.

Last edited by rbatte1; 01-12-2017 at 07:38 AM.. Reason: Broke up the the scipts to seaparte block for clarity
# 2  
Old 01-16-2017
Code:
source passarg2.env "$str"

is a tcsh extension.
Code:
man csh
...
     source [-h] name

Code:
man tcsh
...
     source [-h] name [args ...]

In Linux, csh is a link to tcsh.
For consistent behavior you can change the shebang to
Code:
#!/bin/tcsh

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 01-16-2017
Quote:
Originally Posted by MadeInGermany
Code:
source passarg2.env "$str"

is a tcsh extension.
Code:
man csh
...
     source [-h] name

Code:
man tcsh
...
     source [-h] name [args ...]

In Linux, csh is a link to tcsh.
For consistent behavior you can change the shebang to
Code:
#!/bin/tcsh


Hi,

Thank you for response.
Just as you said, when I change #!/bin/csh to #!/bin/tcsh, everything is fine.

It seems that if I change all scripts to executable (chmod 755) and replace
"source" command like passarg2.env "$str" the result is also correct.
Does it mean csh allow executable scripts to take arguments?

Last edited by rbatte1; 01-16-2017 at 08:10 AM.. Reason: Added ICODE tags
# 4  
Old 01-16-2017
Yes, because an executable script is a command (just like all external executables).
While "source" shares shell variables, a command is completely separate.
# 5  
Old 01-16-2017
Hi.

Apparently MadeInGermany did a better job of explaining csh / tcsh differences than I did on LinuxForums -- good work.

Observations:
  1. One should avoid csh-like shells for scripting: Csh Programming Considered Harmful in favor of Bourne-shell relatives: bash, ksh, zsh.
    .
  2. One may be able to replace source <file> <args> with ./<file> <args>
    .
  3. Not all Linux have csh as link to tcsh:
Code:
$ which csh tcsh
/bin/csh
/usr/bin/tcsh
$ ls -lH $( which csh tcsh )
-rwxr-xr-x 1 root root 143144 Jul 17  2014 /bin/csh*
-rwxr-xr-x 1 root root 395504 Sep  9  2014 /usr/bin/tcsh*

for a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
csh - ( /bin/csh, 2016-02-04 )
tcsh 6.18.01

However, glad to see that there was a solution that suited the OP.

Best wishes ... cheers, drl

Last edited by rbatte1; 01-16-2017 at 08:12 AM.. Reason: Converted textual numbered list to formatted numbered list
This User Gave Thanks to drl For This Post:
# 6  
Old 01-16-2017
It seems that my issue has been clarified.
Thank you guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. Shell Programming and Scripting

Passing multiple arguments to a shell script

Hi Gurus, Need some help with the shell scripting here. #!/bin/ksh ps -ef | grep -i sample.ksh | grep -v grep > abc.txt if then echo "sample.ksh is executing" else echo "sample.ksh is not executing" fi (1 Reply)
Discussion started by: jayadanabalan
1 Replies

3. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

4. Programming

Passing arguments from java to script shell

Hello Please i want to pass parameter (the string s) to the shell script: Quote: String s="Hello"; Process process = Runtime.getRuntime().exec("sh script1.sh"); How can i do please? Thank you (0 Replies)
Discussion started by: chercheur857
0 Replies

5. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

6. Shell Programming and Scripting

Help required in passing multiple arguments from a shell script to a pl/sql block

Hi, hope everyone are fine. Please find my issue below, and I request your help in the same In a configuration file, i have a variable defined as below TEST = 'One','Two','Three' I am trying to pass this variable in to a sql script which is define in a pl/sql block as follows, In the... (1 Reply)
Discussion started by: ramakanth_burra
1 Replies

7. Shell Programming and Scripting

Problem passing arguments to a Python script

I have part of the script below and I tried calling the script using ./tsimplex.py --fstmod=chris.cmod --nxz=8x6 --varp=0.25 but am getting the error option --fstmod must not have an argument Any idea on how to fix this would be highly appreciated #! /usr/bin/python import... (0 Replies)
Discussion started by: kristinu
0 Replies

8. Shell Programming and Scripting

problem passing arguments to script

Hi, I am writing a script, which is invoked from other system using ssh. I have problems reading the arguments passing to the script. If the argument has a space in it (ex "rev 2.00"), the script considers "rev" as 1 argument and "2.00" as another. Instead i want "rev 2.00" to be considered... (5 Replies)
Discussion started by: cjjoy
5 Replies

9. Shell Programming and Scripting

passing runtime arguments to a shell script...

hi I am new to shell programming.....my question is while running one of my shell program it stops in between to accept input from the user and proceeds furthur after giving input....I want to know whether I can set this input through some files so that the shell acript reads the input from the... (10 Replies)
Discussion started by: santy
10 Replies

10. Solaris

Passing arguments to a shell script from file while scheduling in cron

Hi, I have a shell script Scp_1.sh for which I have to pass 2 arguments to run. I have another script Scp_2.sh which in turns calls script Scp_1.sh inside. How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh? -- Weblogic Support (4 Replies)
Discussion started by: weblogicsupport
4 Replies
Login or Register to Ask a Question