Solved

Bash: how commandline parameter  works?

Posted on 2010-08-31
5
267 Views
Last Modified: 2012-08-14
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
Comment
Question by:wantime
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 334 total points
ID: 33565002
#!/bin/bash
case $1 in
true)
echo isDebugModel
;;
false)
echo isNotDebugModel
;;
*)
echo invalid parameter $1
esac
0
 

Author Comment

by:wantime
ID: 33565026
thanks.

if i run main.sh, it should just " isNotDebugModel" displayed. how does it work with your codes?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 166 total points
ID: 33565095
Add the following code to the start of the script

if [ $# -eq 0 ]
then
     echo isNotDebugModel
     exit
fi
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 334 total points
ID: 33565168
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
 

Author Closing Comment

by:wantime
ID: 33779367
thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now