Solved

Bash: how commandline parameter  works?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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