• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Bash: how commandline parameter works?

hello all,

say i have two bash script file: main.sh and sub.sh

the codes look like following.

i would like to know, how should i modify sub.sh to support commandline, what i want get is:

1:  if one call main.sh, it should just " isNotDebugModel" displayed.
2: If one call sub.sh, he has opportunity to input parameter, something like

 bash ./sub.sh true
or
 bash ./sub.sh false

by "true" the output should be "isDebugModel", by "false"  "isNotDebugModel".

thanks,

wantime
sub.sh

#!/bin/bash

echo isDebugModel

echo isNotDebugModel

Open in new window

main.sh

#!/bin/bash
./sub.sh

Open in new window

0
wantime
Asked:
wantime
  • 2
  • 2
3 Solutions
 
ozoCommented:
#!/bin/bash
case $1 in
true)
echo isDebugModel
;;
false)
echo isNotDebugModel
;;
*)
echo invalid parameter $1
esac
0
 
wantimeAuthor Commented:
thanks.

if i run main.sh, it should just " isNotDebugModel" displayed. how does it work with your codes?
0
 
TintinCommented:
Add the following code to the start of the script

if [ $# -eq 0 ]
then
     echo isNotDebugModel
     exit
fi
0
 
ozoCommented:
main.sh

#!/bin/bash
./sub.sh falsew

If you want sub.sh to display " isNotDebugModel" for anything other than true, you might just do
sub.sh
#!/bin/bash
case $1 in
true)
echo isDebugModel
;;
*)
echo isNotDebugModel
;;
esac

if "false" and empty are the only arguments that should display isNotDebugModel, you might do
case $1 in
true)
echo isDebugModel
;;
false|"")
echo isNotDebugModel
;;
*)
echo invalid parameter $1
;;
esac

0
 
wantimeAuthor Commented:
thanks.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now