more scripting
(Written by Paul Cobbaut, https://github.com/paulcobbaut/, with contributions by: Alex M. Schapelle, https://github.com/zero-pytagoras/, Bert Van Vreckem, https://github.com/bertvv)
case
You can sometimes simplify nested if statements with a case construct. For example, the following script uses a case statement:
#!/bin/bash
#
# help.sh - Wild Animals Helpdesk Advice
#
read -rp "What animal did you see ? " animal
case "${animal}" in
"lion" | "tiger")
echo "You better start running fast!"
;;
"cat")
echo "Let that mouse go..."
;;
"dog")
echo "Don't worry, give it a cookie."
;;
"chicken" | "goose" | "duck" )
echo "Eggs for breakfast!"
;;
"liger")
echo "Approach and say 'Ah you big fluffy kitty...'."
;;
"babelfish")
echo "Did it fall out your ear ?"
;;
*)
echo "You discovered an unknown animal, name it!"
;;
esac
Running the script produces the following output:
[student@linux ~]$ ./help.sh
What animal did you see ? lion
You better start running fast!
[student@linux ~]$ ./help.sh
What animal did you see ? dog
Don't worry, give it a cookie.
The syntax of the case statement is as follows:
case STRING_VALUE in
PATTERN11 | PATTERN12 )
COMMANDS
;;
PATTERN21 | PATTERN22 | PATTERN23 )
COMMANDS
;;
* )
COMMANDS
;;
esac
A given STRING_VALUE is compared against the globbing patterns in the case statement. You can use the | operator to separate multiple patterns that should match the same commands.
The patterns are terminated with a bracket ), which is a bit weird, since we're used to always having balanced brackets in code. In fact, you can write an opening bracket before the pattern, but this is very rarely done in practice.
If a match is found, the corresponding commands are executed. The list of commands must be terminated with a double semicolon ;;. The case command completes when it encounters a ;;. A case' statement is terminated with theesackeyword, which iscase` spelled backwards.
Since patterns can contain globbing characters, you can use * to match any string. This is often used as a catch-all pattern at the end of the case statement.
Here's an example with some more globbing patterns:
#! /bin/bash
#
# casedemo.sh - Demonstrate the case statement
#
case "${1}" in
[Hh?] | [Hh][Ee][Ll][Pp] )
echo "If you need help, try the man pages"
;;
[Aa]* )
echo "${1} starts with an A"
;;
??? )
echo "${1} has three characters"
;;
-* )
echo "That looks like an option"
;;
* )
echo "Sorry, I didn't understand"
;;
esac
[student@linux ~]$ ./casedemo.sh HeLp
If you need help, try the man pages
[student@linux ~]$ ./casedemo.sh ?
If you need help, try the man pages
[student@linux ~]$ ./casedemo.sh apple
apple starts with an A
[student@linux ~]$ ./casedemo.sh cat
cat has three characters
[student@linux ~]$ ./casedemo.sh -v
That looks like an option
[student@linux ~]$ ./casedemo.sh banana
Sorry, I didn't understand
What output would ./casedemo.sh abc produce?
Finally, here's an example of a case statement that is used to parse command line arguments. The idea is to loop over the positional parameters with while, check the first positional parameter with case, and then shift it away to move to the next one.
#! /bin/bash
#
# Usage: info.sh [OPTION]... [FILE]...
#
# The script builds a format string for stat from the options given on the
# command line, and prints the output in neat columns.
# The format string always starts with the file name
format_string='%n'
# Loop for parsing CLI arguments
while [ "$#" -gt '0' ]; do
# Check the first positional parameter
case "${1}" in
# Was help asked? Print usage message and exit the script immediately
-h | --help )
printf 'Usage: %s [OPTION]... [FILE]...\n\n' "${0}"
printf ' Show info about files:\n\n OPTIONS:\n'
grep '[^*])$' "${0}" | sed 's/)$//'
exit 0
;;
# Several options (with a short and long variant) that expand the format
# string with the appropriate code. We use the colon as a separator.
-g | --group )
format_string="${format_string}:%G"
;;
-p | --permissions )
format_string="${format_string}:%A"
;;
-s | --size )
format_string="${format_string}:%s"
;;
-t | --type )
format_string="${format_string}:%F"
;;
-u | --user )
format_string="${format_string}:%U"
;;
# Catch-all rule for any option not described above (e.g. -b, --verbose,
# etc.). Immediately exit the script with an error code.
-*)
printf 'Unrecognized option: %s\n' "${1}" >&2
exit 2
;;
# If a positional parameter does not start with a -, it's an argument,
# rather than an option. All remaining parameters should be considered to
# be arguments (i.e. file names), so break out of the while loop.
*)
break
;;
esac
# After processing a positional parameter, shift it away and move to the
# next one.
shift
done
# If no positional parameters remain, then no arguments were given.
# In that case, take all files (*) in the current directory.
if [ "$#" -eq '0' ]; then
set ./*
fi
# If you want to see the resulting stat command, uncomment the next line
# set -x
# Use stat to print the requested info of the remaining positional parameters
# and pipe the output to column to format it nicely. The colon is now used
# to separate the different "fields".
stat --printf="${format_string}\n" "${@}" \
| column -t -s ':'
Try it out in a directory with some files and try the different options. For example:
[student@linux ~]$ ls
casedemo.sh help.sh info.sh 'my file.txt'
README.txt tennis.txt while.sh
[student@linux ~]$ ./info.sh -u --permissions
./casedemo.sh student -rwxr-xr-x
./help.sh student -rwxr-xr-x
./info.sh student -rwxr-xr-x
./my file.txt student -rw-r--r--
./README.txt student -rw-r--r--
./tennis.txt student -rw-r--r--
./while.sh student -rw-r--r--
[student@linux ~]$ ./info.sh -t -s *.txt
my file.txt regular file 128
README.txt regular file 4067
tennis.txt regular file 51
arithmetics
Bash was not designed to be a full-fledged programming language and we use it mainly to automate system administration tasks. Arithmetic is consequently not a strong point of Bash. It is possible to perform some calculations and logical operators on integer values, but floating point numbers are not supported at all. For more advanced calculations, you can use the bc command (or use a scripting or programming language that is more suited for mathematical operations).
There are several ways to perform arithmetic operations in shell scripts. Some are defined by the POSIX standard (and are therefore more portable), others are specific to Bash. We won't go into details here, but will show you the most common ways to perform arithmetic operations in Bash.
The most common ways are:
$(( ))- arithmetic expansion(( ))- compound command for arithmetic evaluationlet- built-in command for arithmetic evaluation
The builtin let is subject to word splitting and pathname expansion, so it is recommended to use the (( )) compound command instead.
arithmetic expansion
In Bash, you can use the $(( )) syntax to perform arithmetic expansion. This allows you to evaluate an arithmetic expression and substitute its result into the command line. Basically: use arithmetic expansion if you need the result of a calculation.
student@linux:~$ echo $(( 1 + 1))
2
student@linux:~$ echo $(( 2 - 4 ))
-2
student@linux:~$ echo $(( 123 * 456 ))
56088
student@linux:~$ echo $(( 9 / 2 ))
4
student@linux:~$ echo $(( 9 % 2 ))
1
student@linux:~$ echo $(( 2 ** 16 ))
65536
student@linux:~$ echo $(( 999999999999999999 * 10 ))
-8446744073709551626
student@linux:~$ echo $(( 9223372036854775806 + 1))
9223372036854775807
student@linux:~$ echo $(( 9223372036854775807 + 1))
-9223372036854775808
We already see a few caveats here. The division operator / performs integer division, so if the result is not an integer, it will be truncated. Use the modulo operator % to get the remainder of a division.
Also, the last examples show that Bash's arithmetic expansion doesn't check for overflow, so when calculating large numbers, the result may not be what you would expect. The maximum value for a signed 64-bit integer is 9223372036854775807, so adding 1 will cause an overflow.
You can use variables in arithmetic expansion as well. The use of the $ is optional.
student@linux:~$ x=42
student@linux:~$ echo $(( x + 1 ))
43
student@linux:~$ echo $(( $x + 1 ))
43
student@linux:~$ echo $(( ${x} + 1 ))
43
Evaluating logical expressions is also possible:
student@linux:~$ echo $(( x < 100 ))
1
student@linux:~$ echo $(( x > 100 ))
0
student@linux:~$ echo $(( x == 42 ))
1
Remark that true is represented by 1 and false by 0, which is the opposite of how Bash normally evaluates exit codes, where 0 is true and non-zero is false. This may be very confusing, so be careful when using arithmetic expansion in conditional statements!
arithmetic evaluation compound command
The (( )) compound command allows you to evaluate arithmetic expressions. It is similar to $(( )), but it does not perform substitution of the result into the command line. Instead, it returns an exit status of 0 (true) if the expression evaluates to a non-zero value, and 1 (false) if the expression evaluates to zero.
student@linux:~/test42$ (( 42 > 33 )) && echo true || echo false
true
student@linux:~/test42$ (( 42 > 1201 )) && echo true || echo false
false
student@linux:~/test42$ var42=42
student@linux:~/test42$ (( 42 == var42 )) && echo true || echo false
true
student@linux:~/test42$ (( 42 == $var42 )) && echo true || echo false
true
student@linux:~/test42$ var42=33
student@linux:~/test42$ (( 42 == var42 )) && echo true || echo false
false
You can use the (( )) compound command in conditional statements, such as if and while, to evaluate arithmetic expressions.
The following example also shows how to decrement the value of a variable with the -- operator.
In the first iteration of the loop, i is 10 and the expression i-- evaluates to the value of i before it is decremented, i.e. 10. This is nonzero, so the exit status of the compound command is 0 (true) and the loop body is executed. At this time, the value of i is decremented to 9, which is printed.
This continues to the iteration where i is 1. Now, the expression i-- evaluates to 1. This is still nonzero, so the exit status of the compound command is 0 (true) and the loop body is executed, printing 0.
In the next iteration, i is 0 and the expression i-- evaluates to 0. This is zero, so the exit status of the compound command is 1 (false) and the loop terminates.
Other operators supported by the (( )) compound command are:
+,-,*,/,%,**- arithmetic operators++,--- increment and decrement operators (prefix and postfix)+=,-=,*=,/=,%=- compound assignment operators==,!=,<,>,<=,>=- comparison operators&&,||,!- logical AND, OR, and NOT operators&,|,^,~,<<,>>- bitwise operators (and, or, xor, not, left shift, right shift, respectively)
eval
eval reads arguments as input to the shell, i.e. dynamically evaluates and executes code that is constructed or stored in a variable.
This allows e.g. using the value of a variable as a variable.
student@linux:~/test42$ answer=42
student@linux:~/test42$ word=answer
student@linux:~/test42$ eval x=\$$word ; echo $x
42
Both in bash and Korn the arguments can be quoted.
kahlan@solexp11$ answer=42
kahlan@solexp11$ word=answer
kahlan@solexp11$ eval "y=\$$word" ; echo $y
42
For this use case, Bash has specific syntax, referred to as indirect substitution. This may be preferrable to using eval, but it is less portable as it won't work in sh or zsh.
Sometimes the eval is needed to have correct parsing of arguments. Consider this example where the date command receives one parameter 1 week ago.
When we set this command in a variable, then executing that variable fails unless we use eval.
student@linux~$ lastweek='date --date="1 week ago"'
student@linux~$ $lastweek
date: extra operand `ago"'
Try `date --help' for more information.
student@linux~$ eval $lastweek
Thu Mar 8 21:36:39 CET 2012
Be aware that using eval is a security risk. Evaluating code based on user input opens the door to command injection and has been the root cause of real-world vulnerabilities.
practice : more scripting
-
Write a script that uses a case statement to check whether the first positional parameter has one of the following properties (in the order listed), and outputs an appropriate message:
- a number between 0 and 999 (inclusive)
- a "truthy" value, i.e. a string that can be interpreted as a boolean value (true/false, yes/no, on/off, T/F). Make the check case insensitive.
- a string that starts with a capital letter
- a string that starts with a lowercase letter
- a "short" or "long" option (single or double dash)
- a string that is empty
- a string that does not match any of the above properties
student@linux:~$ ./casecheck.sh 99 number (0-999) student@linux:~$ ./casecheck.sh Yes true student@linux:~$ ./casecheck.sh FALSE false student@linux:~$ ./casecheck.sh AZERTY capitalized word student@linux:~$ ./casecheck.sh qWeRtY lowercase word student@linux:~$ ./casecheck.sh --help long option student@linux:~$ ./casecheck.sh -a short option student@linux:~$ ./casecheck.sh '' empty string student@linux:~$ ./casecheck.sh 1a2b3c something else -
Is it possible to use a case statement to check whether a string is a positive or negative integer value of arbitrary length? Why (not)?
-
Write a script that asks for two numbers, and outputs the sum and product (as shown here). Use Bash's arithmetic capabilities whenever possible.
-
Rewrite the previous script to test that the numbers are between 0 and 100, exit with an error if necessary.
-
Rewrite the previous script to congratulate the user if the sum equals the product.
-
Rewrite the previous script to take the two numbers as command line arguments. Exit with an error message if not exactly two arguments are given.
solution : more scripting
-
Write a script that uses a case statement to check whether the first positional parameter has one of the given properties and outputs an appropriate message.
#! /bin/bash case "${1}" in [0-9] | [0-9][0-9] | [0-9][0-9][0-9] ) echo "number (0-999)" ;; [Tt] | [Tt][Rr][Uu][Ee] | [Yy][Ee][Ss] | [Oo][Nn] ) echo "true" ;; [Ff] | [Ff][Aa][Ll][Ss][Ee] | [Nn][Oo] | [Oo][Ff][Ff] ) echo "false" ;; [A-Z]* ) echo "capitalized word" ;; [a-z]* ) echo "lowercase word" ;; --* ) echo "long option" ;; -* ) echo "short option" ;; '' ) echo "empty string" ;; * ) echo "something else" ;; esac -
Is it possible to use a case statement to check whether a string is a positive or negative integer value of arbitrary length? Why (not)?
No, it is not. Globbing patterns do not allow you to specify a minimum or maximum number of specific characters. For example, the pattern
[0-9]*would match the string '1abc', which is not a valid integer. -
Write a script that asks for two numbers, and outputs the sum and product (as shown here). Use Bash's arithmetic capabilities whenever possible.
-
Rewrite the previous script to test that the numbers are between 0 and 100, exit with an error if necessary.
Add the following lines after the
readcommands: -
Rewrite the previous script to congratulate the user if the sum equals the product.
Add as the last lines of the script:
-
Rewrite the previous script to take the two numbers as command line arguments. Exit with an error message if not exactly two arguments are given.
In this solution, we continue using
$1and$2throughout the script. Remark that you could also assign them to the variables from the previous versions, e.g.n1=${1}andn2=${2}.#! /bin/bash if (( $# != 2)); then printf 'Expected 2 arguments, got %d\n' "$#" >&2 exit 1 fi sum=$(( ${1} + ${2} )) pro=$(( ${1} * ${2} )) if (( ${1} < 0 || ${1} > 100 || ${2} < 0 || ${2} > 100 )); then printf 'Value out of range (0 - 100), exiting\n' >&2 exit 1 fi printf 'Sum\t: %d + %d = %d\n' "${1}" "${2}" "${sum}" printf 'Product\t: %d * %d = %d\n' "${1}" "${2}" "${pro}" if (( sum == pro )); then printf 'Congrats, %d == %d\n' "${sum}" "${pro}" fi