Visit Our UNIX and Linux User Community


Check input parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check input parameter
# 1  
Old 10-04-2010
Check input parameter

Hi all

i need to check that if user has passed any input parameter while executing he shell script like

Code:
./test1.sh -a"-v"

then do smothing

if user execute the script without giving input paramater then

Code:
./test1.sh

then do something

how can we check this input parameter

Last edited by Scott; 10-04-2010 at 11:39 AM.. Reason: Code tags
# 2  
Old 10-04-2010
Code:
#!/usr/bin/sh


var1=$1;
var2=$2;

u can get the parameters in $ variable....
FYI: $0 is the script name itself
# 3  
Old 10-04-2010
Code:
if [[ $# -gt 1 ]]
then
        do something
else
        do some-other thing
fi

Include this in your script for which the input parameters needs to be checked.

$# -- holds the number of arguments passed to the script
# 4  
Old 10-04-2010
For a thorough checking of options and arguments you might want to use the "getopts" program or - depending on your shell - the getopts built-in respectively.

For checks on datatypes (like "string", "integer", "filename", etc.) of arguments you might read the following posts:

https://www.unix.com/302409986-post4.html

https://www.unix.com/78621-post5.html

https://www.unix.com/302168712-post2.html

I hope this helps.

bakunin
# 5  
Old 10-04-2010
hi everyone
thanx for your help
i have one more query
suppose if user can enter parameter only like this

Code:
./test1.sh -a"-v"

there is no space between -a and "-v" then if we have to follow two operation based on parameter passed like

Code:
./test1.sh -a

and

Code:
./test1.sh -a"-v"

then i suppose we cannot take $1 and $2 because for that we must have space between them
then how we will solve this

Last edited by Scott; 10-04-2010 at 11:39 AM.. Reason: Code tags
# 6  
Old 10-04-2010
Hi.

I think that problem goes away with getopts:

Code:
$ cat testscript
while getopts a: ARG; do
  case "$ARG" in
    a)  echo "OPTARG is $OPTARG"
  esac
done

$ ./testscript -a" -v"

OPTARG is  -v

Not exactly clear what you're getting at, though, or why.
# 7  
Old 10-04-2010
hi aishsimplesweet
In your case, shell script can have only one argument i.e $1

what you can do is declare a variable var=$1
then split based on delimiter. (i.e ")

Let us know the complete functionality of the code so that it can be implemented accordingly

Previous Thread | Next Thread
Test Your Knowledge in Computers #911
Difficulty: Easy
In C, we can get the address of a variable as follows: pointer = &variable;
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace value from input parameter

Hi Guys, I am having a script file where in getting input parameter as string. I need to assign the variable but not able to achieve #!/bin/bash input=$1 replace=string_$input_string2 echo $replace I am getting but should get string_<input_value>_string2 string_ (1 Reply)
Discussion started by: rohit_shinez
1 Replies

2. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

3. UNIX for Dummies Questions & Answers

Best Alternative for checking input parameter contains required value or not

Any good way to check if code has the required output # /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts net.ipv4.icmp_echo_ignore_broadcasts = 1 /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts | grep "= 1" net.ipv4.icmp_echo_ignore_broadcasts = 1 What I can think of is above, and it... (16 Replies)
Discussion started by: alvinoo
16 Replies

4. Shell Programming and Scripting

Verify input parameter is in the list

I need write a Korn shell which accept input parameter. But this input paramter must match one of the string in an existsing file (listkeyword). Can someone one help, how this can be done ? (3 Replies)
Discussion started by: cpchiu
3 Replies

5. Shell Programming and Scripting

How to pass the password as input parameter to scp

Dear all Does anybody know how to pass the password as input parameter to scp or rsync in unix scripts? I have tried echo <password> | scp filename username@<ip address>:/filepath/ . But it does not work. BTW, I dont want to setup ssh trust between servers in this adhoc task. Regards,... (2 Replies)
Discussion started by: eldonlck
2 Replies

6. Shell Programming and Scripting

Passing string as a input parameter for a shell script

Hi i have a shell script which needs a string as an input parameter. How to pass the string param as an input? In command line am running the script. for e.g., a="who is a buddy?" sh sample.sh $a Inside the script i get this input param as $1 but only the value "who" is accepted... (12 Replies)
Discussion started by: vidhyaS
12 Replies

7. Shell Programming and Scripting

How to Get the File Input from Parameter

pdir=`pwd` if ; then echo current directory $pdir ls -altr echo fi for f in $* do # directory if ; then echo current directory $f cd $f ls -latr echo fi # but you can test file/dir # regular file only if ; then echo... (4 Replies)
Discussion started by: wtolentino
4 Replies

8. Shell Programming and Scripting

awk/input parameter

Hi, My script takes in one input parameter($1-email id) on the command line... The script contains something like this... awk '$1 == 400' abc.log >def.log mail -s subject $1 <def.log abc.log looks something like this... 300 222 330 123 445 400 098 890 727 663 How do i make the... (3 Replies)
Discussion started by: wannalearn
3 Replies

9. Shell Programming and Scripting

Input parameter format

Hi, My script expects 2 inputs and one of them is supposed to be time, I would like to check if the given input is in validate format (i.e. 16:00), could anyone help me out here? thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script with input parameter

Can anyone help me how to write a shell script which accepts input parameter. My requirement is as follows: I need to run a shell script with a input parameter, and inside the script i will create a file with this input parameter name. Please help me out to create such a shell script. ... (1 Reply)
Discussion started by: jhmr7
1 Replies

Featured Tech Videos