0

I am trying to run python script by shell script. For running python script I am sending one argument. So I am passing that argument into shell script. But If argument is not pass then is should say Please insert argument and if argument is pass then python script should be start.

Example:

python script - new.py
shell script - new.sh
system argument - 'EUREKA'
run_command - bash new.sh 'EUREKA'

I am trying this shell script to run the process:

if ["$1"]
then 
    python /medaff/Scripts/python/iMedical_Consumption_load_Procs.py "$1"
else
    echo "Please insert application_name"

fi

It is always going in else part. Can you please correct me?

Shivam
  • 213
  • 5
  • 14
  • You need spaces in the test: `if [ "$1" ]`. See [this question](https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash) – Gordon Davisson Apr 27 '20 at 16:27

2 Answers2

0

You could check if the length of the string contained in $1 is nonzero. Try:

if [ -n "$1" ]
then 
    python /medaff/Scripts/python/iMedical_Consumption_load_Procs.py "$1"
else
    echo "Please insert application_name"

fi

Or better:

appname="$1"
while [ -z "$appname" ] 
do 
    read -p "Please insert application_name: " appname
done

python /medaff/Scripts/python/iMedical_Consumption_load_Procs.py "$appname"
Pierre François
  • 5,850
  • 1
  • 17
  • 38
0

You don't need an if in your case. You could instead just do a (i.e.)

 python /medaff/Scripts/python/iMedical_Consumption_load_Procs.py "${1:?application_name missing}"

The :? outputs the error message if the parameter is missing or empty and abort the script with exit code 1.

If you just want to test for the existence of the parameter, but allow empty application names, just omit the :.

user1934428
  • 19,864
  • 7
  • 42
  • 87