This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== BASH Scripting Reference ====== ==== Links/Resources ==== [[https://linuxhint.com/3hr_bash_tutorial/|3 Hour Bash Tutorial]] \\ ---- ==== Functions ==== <code> function double() { Result = $1 * 2 } Double 12 </code> ---- ==== If / Elif / Else ==== [[https://linuxhint.com/bash_if_else_examples/|link]] <code> if CONDITION-TO-TEST; then CODE-TO-EXECUTE-1 elif NEXT-CONDITION-TO-TEST; then CODE-TO-EXECUTE-2 elif NEXT-CONDITION-TO-TEST; then CODE-TO-EXECUTE-2 else CODE-TO-EXECUTE-2 fi </code> ---- ==== Ask a yes/no question (case insensitive comparison)==== <code> read -p "Do it? [y/n] " yesno if [[ "${yesno,,}" != "y" ]]; then echo "OK. Exiting..." exit 1 # Exit with an error code to indicate user cancelled else # Rest of your script if user confirms (y/Y) echo "OK. Continuing..." fi </code> ---- ==== Check if running as root/sudo ==== <code> if [[ $(/usr/bin/id -u) -ne 0 ]]; then echo "Not running as root" exit fi </code> <code> touch /etc/test.file if [ $? -ne 0 ]; then echo "This command needs to be run with sudo!" exit 1 fi </code> ---- ==== Pass Command Line Parameters ==== <code> #!/bin/bash echo "1st parameter = $1 " echo "2nd Parameter = $2 " </code> ---- ==== Test if variable is empty ==== <code> if [ -z "$var" ] then echo "\$var is empty" else echo "\$var is NOT empty" fi </code> ---- ==== Add a user, specifying group membership ==== <code> sudo useradd -m -g users -G sudo,docker username sudo id username </code> ----