Shell script command line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script command line arguments
# 1  
Old 12-20-2010
Shell script command line arguments

Hello All,
i am known to the limitation of different shells while passing more than 9 command line arguments
i just tried the example below
i do see my current shell is tcsh
Code:
echo $SHELL
/bin/tcsh

so if i make my script executable and run it

output is
Code:
%ingrx025:/home/grvobad2/shell/tes/deepak/shell >ls -l command_arg.sh
-rwxrwxr-x   1 grvobad2 grcdc        314 Dec 20 14:19 command_arg.sh
%ingrx025:/home/grvobad2/shell/tes/deepak/shell >./command_arg.sh 11 12 13 14 15 16 17 18 19 20 21
first arg passed is 11
2nd arg passed is 12
3rd arg passed is 13
4th arg passed is 14
5th arg passed is 15
6th arg passed is 16
7th arg passed is 17
8th arg passed is 18
9th arg passed is 19
10th arg passed is 110-------  as expected wrong output
11th arg passed is 111---------as expected wrong output

But if i run ,
Code:
%ingrx025:/home/grvobad2/shell/tes/deepak/shell >tcsh command_arg.sh 11 12 13 14 15 16 17 18 19 20 21
first arg passed is 11
2nd arg passed is 12
3rd arg passed is 13
4th arg passed is 14
5th arg passed is 15
6th arg passed is 16
7th arg passed is 17
8th arg passed is 18
9th arg passed is 19
10th arg passed is 20
11th arg passed is 21

i do get correct output

i am running the script in a same shell but way is different and i get differnet output

Script is :
Code:
cat command_arg.sh
echo "first arg passed is $1"
echo "2nd arg passed is $2"
echo "3rd arg passed is $3"
echo "4th arg passed is $4"
echo "5th arg passed is $5"
echo "6th arg passed is $6"
echo "7th arg passed is $7"
echo "8th arg passed is $8"
echo "9th arg passed is $9"
echo "10th arg passed is $10"
echo "11th arg passed is $11"

Please let me know why is it happening?

Last edited by vbe; 12-20-2010 at 05:39 AM.. Reason: code tags please
# 2  
Old 12-20-2010
For getting the arguments with 2 digits, You should give as below,

echo "10th arg passed is ${10}"
echo "11th arg passed is ${11}"
# 3  
Old 12-20-2010
thanks for answer i am known to this and yes, it varies for different shells

i have a doubt in

though my present shell is /bin/tcsh and if i run a script with

./script_name

the output is different than

tcsh script_name

?
# 4  
Old 12-20-2010
What system you're running the test on? I'm able to reproduce that behavior on Cygwin, but not on Linux (Ubuntu 10.10).
# 5  
Old 12-20-2010
it is on solaris

uname -a
SunOS ingrx025 5.10 Generic_118822-25 sun4u sparc SUNW,Sun-Fire-V240
# 6  
Old 12-20-2010
Quote:
Originally Posted by Deepak Dutt
thanks for answer i am known to this and yes, it varies for different shells

i have a doubt in

though my present shell is /bin/tcsh and if i run a script with

./script_name

the output is different than

tcsh script_name

?
I believe I understand why this is happening, it's should be specific to [t]csh and I suppose documented somewhere Smilie
On some (or all?) systems (Solaris included) when you invoke a script with no shebang as ./script_name from within [t]csh the script is executed by a predefined interpreter (/usr/bin/sh on Solaris):

Code:
> uname -sr ; echo $version
SunOS 5.8
tcsh 6.11.00 (Astron) 2001-09-02 (sparc-sun-solaris) options 8b,nls,dl,al,rh,color
> cat command_arg.sh
echo '----->' `ps -p$$ -oargs=`
echo "first arg passed is $1"
echo "2nd arg passed is $2"
echo "3rd arg passed is $3"
echo "4th arg passed is $4"
echo "5th arg passed is $5"
echo "6th arg passed is $6"
echo "7th arg passed is $7"
echo "8th arg passed is $8"
echo "9th arg passed is $9"
echo "10th arg passed is $10"
echo "11th arg passed is $11"
> ./command_arg.sh 11 12 13 14 15 16 17 18 19 20 21
-----> /bin/sh ./command_arg.sh 11 12 13 14 15 16 17 18 19 20 21
first arg passed is 11
2nd arg passed is 12
3rd arg passed is 13
4th arg passed is 14
5th arg passed is 15
6th arg passed is 16
7th arg passed is 17
8th arg passed is 18
9th arg passed is 19
10th arg passed is 110
11th arg passed is 111

So given the following script:

Code:
$ cat s
ps -p$$ -oargs=

We have:

Code:
$ bash -c './s'
bash -c ./s
$ ksh -c './s'
ksh -c ./s
$ tcsh -c './s'
/bin/sh ./s
$ csh -c './s'
/bin/sh ./s

Tested on Solaris e Linux.

Last edited by radoulov; 12-21-2010 at 04:33 AM.. Reason: Modified.
# 7  
Old 12-20-2010
thanks it seems the same as u explained though yet to understand your tracing techniques Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass Arguments to Command from Shell Script

Hi all, I am working on a project, in which I have to connect to Bluetooth low energy device. I am able to connect and do data transfer from command line. But I want to do from script Here is my script #!/bin/bash #sudo hcitool -i hci0 lescan sleep 1 sudo hcitool -i hci0 lecc --random... (8 Replies)
Discussion started by: nithin@embdes
8 Replies

2. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

3. UNIX for Dummies Questions & Answers

Script to iterate all command line arguments

Hi guys, I am having trouble with this script. What i want it to do is to iterate all command line arguments in reverse order. The code below does this fine but i need the output to print the words on separate lines instead of one line: #!/bin/bash #Takes in the arguments and displays them... (7 Replies)
Discussion started by: pipeline2012
7 Replies

4. Programming

How to pass the command line arguments to the shell script in c language?

hi, I am new in the shell script, and c programming with linux. I am looking to pass the arguments in c program that should be executed by the shell script. e.g. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i; for (i=1;i<argc; i++) { ... (2 Replies)
Discussion started by: sharlin
2 Replies

5. Shell Programming and Scripting

How to use case and command line arguments in shell script?

Hi... can anyone please help me out in using the CASE and command line argument in shell script... i am bit new to shell scripting...below i have explained my proble with example... say i have an executable file with name 'new1.sh' and there are 3 functions in it a(), b() and c()....and there... (5 Replies)
Discussion started by: swap21783
5 Replies

6. UNIX for Dummies Questions & Answers

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (5 Replies)
Discussion started by: ajaira
5 Replies

7. UNIX for Advanced & Expert Users

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

8. Shell Programming and Scripting

Bourne Shell Script that only takes command line arguments

Does anybody know how to Accept a “userid” as a command line argument on a Unix Bourne Shell Script? The output should be something like this: User userid has a home directory of /path/directory the default shell for this user is /path/shell (1 Reply)
Discussion started by: ajaira
1 Replies

9. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

10. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies
Login or Register to Ask a Question