[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Bash: how commandline parameter  works?

Posted on 2010-08-31
5
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1336 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 664 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 1336 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

649 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