0

I have a unix shell script that reads parameter from command line and then exit in case parameter missing. Then i tested with parameter, which is supposed to work fine but it exit with error. anyone can check please?

SCRIPT:

#!/bin/ksh
##########################################################################
#       Remote to upstream servers to check feed files without password  #
#               ELLA YE                                                  #
#               Sept 20 2017                                             #
#               Version 1                                                #
##########################################################################

RUNDIR=/tmp
UPSTREAM_USER="$1"
UPSTREAM_SERVER=$2
UPSTREAM_DIR=$3
FILE_NAME=$4
LOCAL_DIR=$5

echo $5
#if [[ ( "$1" = "" ) || ( "$2 = "" ) || ( "$3" = "" ) || ( "$4" = "" ) || ( "$5 = "" ) ]]
if [ $5 -eq "" ];
then
        echo "Parameter missing"
exit 1;
fi

my command:

./sftpupstream.ksh abc abc.wlb2.nam.nsroot.net . .profile .

result:

.
Parameter missing
bash-4.1$
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
ella
  • 165
  • 2
  • 4
  • 17
  • 1
    I would just test that $# is of the size you want :) empty string should do not get counted in the argument list. so [ $# -eq 5 ] – Rob Sep 20 '17 at 19:18
  • The shebang line denotes `ksh` as the interpreter, not `bash`. You likely cannot use bash-only extensions in a Korn shell script. – Benjamin Bannier Sep 21 '17 at 08:41

3 Answers3

0

I think you are looking for

if [ -z "$5" ]
  then
    echo "Parameter missing"
    exit 1
fi

The -z switch will test if the expansion of "$5" is a null string or not. If it is a null string then the body is executed.

see this question

jtbaird
  • 1
  • 3
0

Put this at the top of your script :

#!/bin/ksh
if [ $# -ne 5 ]; then
        echo "$((5 - $#)) parameter(s) missing"
        exit 1
    else
        # You code
    fi

$# return the number of parameters passed to your scripts, so if different to 5 script will exit.

echo "$((5 - $#))" will do the maths and output how many parameters are missing out of 5.

Will
  • 1,792
  • 2
  • 23
  • 44
-1

What is the error message?

My blind guess is put your vars in quote. "$5"

Ali Raza
  • 90
  • 8