Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally omit a command line option in bash

I have a long command which I want to modify in a certain case to either include an option or not.

E.g.

java -jar compiler.jar --externs "$externs"
    --foo "another" --bar "more"

where I want the --externs "$externs" to be omitted completely if $externs is empty. Is there any way to do this in bash without resorting to building up the whole command with a string?

like image 669
Chris Middleton Avatar asked Dec 07 '15 22:12

Chris Middleton


People also ask

How do you abort a command in bash?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you break an if statement in bash?

Use the break statement to exit a while loop when a particular condition realizes. The following script uses a break inside a while loop: #!/bin/bash i=0 while [[ $i -lt 11 ]] do if [[ "$i" == '2' ]] then echo "Number $i!" break fi echo $i ((i++)) done echo "Done!"

What is a conditional statement in Bash?

The conditional statement is used in any programming language to do any decision-making tasks. This statement is also used in bash to perform automated tasks like another programming language, just the syntax is a little bit different in bash. Two types of conditional statements can be used in bash. These are, ‘If’ and ‘case’ statements.

How do you delimit a condition in Bash?

In a condition in Bash, the operators and operands are arguments. So, $theVar, -eq, and 15 are arguments. If all the arguments are arithmetical, then the double parentheses can be used to delimit the condition, as the following code shows:

What does $1 mean in bash script arguments?

Therefore, $1 refers to the next element in the input each time. 3. Conclusion In this article, we looked at how arguments passed to a bash script during runtime can be processed inside the script in different ways:

How to use single conditional use of if statement in PowerShell?

This example shows the single conditional use of if statement. Create a file named ‘cond1.sh’ and add the following script. This script will take a numeric value as input and check the value is less than 100 or not by using if condition. If the condition is true then it will print a message in the terminal. Run the script.


1 Answers

This is what the ${key:+WORDS} expansion is for:

java ... ${externs:+--externs "$externs"}

It's also a common idiom to use an array for the purpose:

args=( )
if [[ $externs ]]; then
  args+=( --externs "$externs" )
fi

java ... "${args[@]}"
like image 168
Charles Duffy Avatar answered Nov 15 '22 07:11

Charles Duffy