Shell parameters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell parameters
# 1  
Old 12-28-2001
Data Shell parameters

I need to check the number of input parameters in the K shell script.
Also, I want to exit if the no of parameters is not met.


How can I do that ?

Thanks
LS1429
# 2  
Old 12-28-2001
I think I see what you mean... For example, the command:
myscript.ksh -a 1 -q
has 3 input parameters... Am I correct? If so, you can use $# to count them. Here is an example:
Code:
#!/usr/bin/ksh
 if [ "$#" = "0" ]; then
     echo "You don't have any arguments! "; exit
fi
echo $#

To try this script, try this:
chmod +x scriptname
./scriptname
./scriptname 1 b 3 d
See what happens.

If you have any more questions, please feel free to post back. I hope I got what you were asking.
# 3  
Old 12-28-2001
Shell Parameters

Yeah thatz it.
One more thing. But I am not clear why the ; after condition is really needed or not

if [ $# -ne 1 ]
then
echo "Provide one Parameter "
exit 1
fi

Thanks
LS1429
# 4  
Old 12-28-2001
Re: Shell Parameters

Quote:
Originally posted by ls1429
Yeah thatz it.
One more thing. But I am not clear why the ; after condition is really needed or not
Take an easier example, the date and who commands. If we want to run both commands, we might use two lines:
date
who
or we can enter both commands on a single line if we use the semicolon like this:
date ; who
But we gotta have something separating the two commands. Usually we have the newline character as the delimiter between commands, but the semicolon also works.

So it's a matter of style whether you use
if [ ... ] ; then
or
if [ ... ]
then

I like the first style because it lets more of the script be visible on a page. But I wouldn't call the second style "wrong".
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I fetch few parameters in Shell script?

Hi All, I want to fetch few details out of a huge output of AWS CLI tools : I am using this command : ec2-describe-instances (6 Replies)
Discussion started by: Palak Sharma
6 Replies

2. Homework & Coursework Questions

Positional Parameters Shell Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: STEP 1: Create a Bash shell script (myscript) inside the a2 directory. The script should allow the user to pass... (3 Replies)
Discussion started by: tazb
3 Replies

3. Programming

csh: Parameters in Shell Script Help

I have a program that is designed to take the first parameter (a file extension) and use it to rewrite the file stored in the second paramerter with the new extension. And if the current file does not exist then the program will print an error message "No such file." For example, my program is... (1 Reply)
Discussion started by: Marhsall34
1 Replies

4. UNIX for Dummies Questions & Answers

shell script with parameters?

hi , I wanted to create a script, and I have no idea how to, and I would appreciate any help on that. Any advice on solving this is welcome. Thanks Here it goes: make a shell script in bash that show the activity of users. The results should be a summary or a complete report in a file.... (2 Replies)
Discussion started by: ubu-user
2 Replies

5. Shell Programming and Scripting

Parameters + arrays in unix shell

Say I have ./param HEY What would I do if I wanted to store each character into an array? Example. ARRAY1="H" ARRAY1="E" ARRAY1="Y" thank you! (5 Replies)
Discussion started by: puttster
5 Replies

6. Shell Programming and Scripting

Handling parameters in Shell Functions

Hi, Please help me with the below situation where I have to handle the parameters passed to a function in a unique way. Below is the code, which I am trying to execute. I basically want to pass the parameter to a function, where I am trying to get user input into array(s). I want to name... (7 Replies)
Discussion started by: bharath.gct
7 Replies

7. Shell Programming and Scripting

Accessing the parameters of a shell script

Hi, I have one situation. I am developing a shell script to which parameters will be passed from a Web based User Interface using some Business Process(BP).There are some 6 parameters for which user will enter the values in UI. These values will be passed to script by BP in the form -... (2 Replies)
Discussion started by: The Observer
2 Replies

8. UNIX for Dummies Questions & Answers

Call a UNIX shell with parameters from C

Hello...I hava quite a problem, couldn't find a solution anywhere :(. I have a C program, and from that C program I have to call a shell script. This is not difficult, I can do it using the "system" command from C. But the ugly part is how can I send as parameters some variables? For example...i... (1 Reply)
Discussion started by: dustman
1 Replies

9. Shell Programming and Scripting

Number of parameters to a shell script

Is there any restriction on number of parameters can be passed on to the shell script? I found, after 9th parameter for parameter 10, it is taking parameter 1. (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question