0

My shell script is failing with syntax error for the function with a simple if statement in it. Not sure what I am missing in the below code:

#!/bin/ksh
if [ $# -ne 1 ]; then
    echo Param: [ENV]
    exit 1
fi

ENV=$1
if [ $ENV == "qa" ] 
then 
    HDFS_LOCATION="QA" && HIVE_DB="qa"
else 
    HDFS_LOCATION="prod" && HIVE_DB="prod"
fi

if [ $ENV == "prod" ] 
then 
    ISILON="isilonprod"
else 
    ISILON="isilondev"
fi

check() {
    if [$1 -ne 0]; then
        echo "----------------------Error-------------------"
        exit 1
    else
        echo "----------------------------------------------"
        echo "----------------------Done--------------------"
        echo "----------------------------------------------"
    fi
}

echo "#################### CREATE TABLES IN HIVE ##################################"

hconnect -f --hivevar hdfs_location=${HDFS_LOCATION} --hivevar isilon=${ISILON} /tmp/nanda/deployment/poc_src_dallas_al.hql
check $? 

echo "#################### END HIVE ONLY TABLES ##################################"


ERROR:

[root@myhost deployment]# sh deploy.ksh qa
'eploy.ksh: line 22: syntax error near unexpected token `{
'eploy.ksh: line 22: `check() {
tripleee
  • 139,311
  • 24
  • 207
  • 268
  • Line endings? Can you pass your script through dos2unix and see if the issue persists? Btw, you have an error in your script: `if [$1 -ne 0];` should be `if [ $1 -ne 0 ];`. Also quote your variables, not `[ $ENV == "prod" ] ` but `[ "$ENV" == "prod" ] `, not `$1` but `"$1"`. If you pass variables with spaces you will see many errors. Also `[` test builtin command takes `=` for string comparision not `==`, so it should be `[ "$ENV" = "prod" ]`. Also you may interest yourself in using `(( ... ))` for arithmetic comparisons, like `if (( $1 != 0 )); then`. – KamilCuk Nov 14 '18 at 09:33
  • http://shellcheck.net/ readily identifies the syntax error earlier on in your script. – tripleee Nov 14 '18 at 10:08
  • shellcheck.net says "No issues detected" in my script – Nandakumar_bigdata Nov 14 '18 at 10:46

0 Answers0