Just create a function:
function masterScript()
{
if [ -e /home/arun/Desktop/scripts/myMasterScript.sh ]
then
bash /home/arun/Desktop/scripts/myMasterScript.sh
fi
}
And make sure your script is executable:
chmod 755 /home/arun/Desktop/scripts/myMasterScript.sh
Within sh
which is actually dash
on Ubuntu the builtin command return
can returns only numerical values - exit statuses, which have a meaning in a context of a function or sourced script. Source man sh
:
The syntax of the return command is
return [exitstatus]
Everything other with your shell script looks correct. I think you need to use echo $COLOR
instead return and suppress other echo-es.
In case you need to return more data to the main script you can output everything as one line and divide the separate fields by some character that will play role of a delimiter in the main script on which base you can convert the string into an array. For example (where ,
is our delimiter and -n
will suppers the newline character within echo
):
echo -n "$COLOR","$exitstatus"
The other information that is provided by the script and is not required by the main script could be redirected to some log file:
$ cat whiptail.sh
#!/bin/sh
log_file='/tmp/my.log'
COLOR=$(whiptail --inputbox "What is your favorite Color?" 8 78 Blue --title "Example Dialog" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Ok and entered $COLOR" > "$log_file"
echo -n "$COLOR","$exitstatus"
else
echo "User selected Cancel." >> "$log_file"
echo -n "CANCEL","$exitstatus"
fi
Unfortunately I don't have much experience with Python, but here is a sample .py script that can handle the output of the above .sh script (reference):
$ cat main-script.py
#!/usr/bin/python
import subprocess
p = subprocess.Popen(['./whiptail.sh'], stdout=subprocess.PIPE)
p = p.communicate()[0]
p = p.split(",")
print "Color: " + p[0]
print "ExitCode: " + p[1]
Best Answer
When you are calling a function, it should be defined and known, when you are calling the
function1
at this like:Interpreter has no idea where this function is, because it has not seen it yet; so put your functions above the
case
sentence.That would fix the issue.