This question already has answers here:
Using getopts to process long and short command line options
(32 answers)
Closed 8 years ago.
I want to write a bash shell script, it can accept parameters, and the parameter has prompt, example,
./test.sh --version=1.0
value 1.0 is the real parameter for my shell, and --version= is the prompt
is there any easy way to do it like this?
You should have have a look at man(1) getopt.
Depending what you mean by "easy" - using getopts should work. A bit of typing, but... Here are some examples to get you started:
http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
How do I parse command line arguments in Bash?
http://spin.atomicobject.com/2011/03/30/parsing-arguments-in-bash-with-getopts/
Related
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
I have a command I want to run:
sbt "testOnly com.example.testClass"
which needs to be ran with the quotes. However, what I really want to do is be able to pass the argument in a variable, while keeping the quotes.
This does not work:
TEST_CMD="\"testOnly com.example.testClass\""
sbt $TEST_CMD
This does work:
TEST_CMD="\"testOnly com.example.testClass\""
eval sbt $TEST_CMD
I read http://mywiki.wooledge.org/BashFAQ/050 and now I understand why the first doesn't work, and I've also learned that eval can be insecure and should be avoided (this is just an internal Jenkins job, would it ever be an issue?).
Also in the article, it mentioned adding the command to an array first, so I tried:
args=("\"testOnly com.example.testClass\"")
sbt "${args[#]}"
but that also does not run correctly. What's the best way to do this? Is it really that bad to use eval in my case?
Quote the variable expansion rather than the assignment.
TEST_CMD="testOnly com.example.testClass"
sbt "$TEST_CMD"
This question already has answers here:
What does double-dash do when following a command?
(4 answers)
Closed 3 years ago.
https://docs.docker.com/compose/startup-order/
Could someone point me to some docs or general knowledge of what the "--" means in this docker command?
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
I've always been weak at bash and unix things and wanted to learn more.
The precise semantics of options depend entirely on the command. In this case, examine the documentation or source code of wait-for-it.sh. But commonly, a double dash is used to signal the end of options. Here, it looks like the arguments before the double dash are parameters for the script itself, and the stuff after the double dash is a command line (which could conceivably contain additional options, which should not be misunderstood as options for wait-for-it.sh itself).
This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.
This question already has answers here:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.
This question already has answers here:
Trouble understanding parameter substitution in a script
(1 answer)
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
I've found this in a shell script that I use and I'm having trouble finding a formal description/definition of this syntax:
ACTION=${1:-update}
I'm assuming that if $1 variable does not exist (no command line arguments) then "-update" is used.
It's not esoteric. It's POSIX, and even Bourne. In every shell manpage ever. man bash or man ksh. The assumption is mostly right, if the parameter 1 is unset or empty string, then expand the alternate.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02