Check input parameter

Hi all

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

./test1.sh -a"-v"

then do smothing

if user execute the script without giving input paramater then

./test1.sh

then do something

how can we check this input parameter

#!/usr/bin/sh


var1=$1;
var2=$2;

u can get the parameters in $ variable....
FYI: $0 is the script name itself

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

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:

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

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

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

I hope this helps.

bakunin

hi everyone
thanx for your help
i have one more query
suppose if user can enter parameter only like this

./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

./test1.sh -a

and

./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

Hi.

I think that problem goes away with getopts:

$ 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.

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