Solved

Bash: how commandline parameter  works?

Posted on 2010-08-31
5
273 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…

762 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