User Tools

Site Tools


ops102:bash_scripting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ops102:bash_scripting [2024/03/07 20:58] – [Conditional Logic: if / then / elif / else / fi] chrisops102:bash_scripting [2024/04/16 18:10] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== NOTE: This page is being edited and is NOT ready for use. ===== 
- 
 ====== Bash Scripting ===== ====== Bash Scripting =====
  
Line 176: Line 174:
 ===== Command Capture ===== ===== Command Capture =====
  
-You can capture the output (stdout) of a command as a string using the notation ''$( )''and then use that string in a variable assignment or as a command argument:+You can capture the output (stdout) of a command as a string using the notation ''$( )'' and then use that string in a variable assignment or as a command argument:
  
   $ echo "The current date and time is: $(date)"   $ echo "The current date and time is: $(date)"
Line 383: Line 381:
   [[ integer1 -le integer2 ]]  # Integer1 is less than or equal to integer2   [[ integer1 -le integer2 ]]  # Integer1 is less than or equal to integer2
  
 +== Other Tests ==
 +
 +The four groups of tests above will cover the vast majority of situations. There are additional tests available to test other conditions, such as whether a variable is defined, or a file refers to a device. See the man page for bash(1) for more information if you're interested in other tests: ''man bash''
 + 
 == Negating and Combining Tests == == Negating and Combining Tests ==
  
Line 400: Line 402:
  
   [[ -f "$FILE" && -r "$FILE" && -w "$FILE" ]] # True if "$FILE" is a readable, writable regular file.   [[ -f "$FILE" && -r "$FILE" && -w "$FILE" ]] # True if "$FILE" is a readable, writable regular file.
 +
 +== Tips on Using Tests ==
 +
 +  * Remember to quote any arguments which include whitespace, or which may be null (empty).
 +  * Be careful with the ''<'' and ''>'' comparison operators: if you have a syntax error, you may accidentally redirect data (which in the case of ''>'' might overwrite a file!)
 +
 +==== Parameters ====
 +
 +Arguments to a script are called parameters. You can access the parameters using the special variables ''$0'', ''$1'', ''$2'', and so forth. ''$0'' contains the name of the script, ''$1'' contains the first parameter, ''$2'' contains the second parameter, and so forth.
 +
 +The special variable ''$#'' contains the total number of parameters.
 +
 +Here is a simple script which shows you what parameters have been received:
 +
 +  #!/usr/bin/bash​
 +  echo "Number of parameters:  $#"​
 +  echo "Parameter 0:           $0"
 +  echo "Parameter 1:           $1"
 +  echo "Parameter 2:           $2"
 +  echo "Parameter 3:           $3"
 +  echo "Parameter 4:           $4"
 +
 +When you run this script with three parameters (red, green, and blue), you get this output:
 +
 +  $ ./params red green blue​
 +  Number of parameters:  3​
 +  Parameter 0:           ./params
 +  Parameter 1:           red​
 +  Parameter 2:           green​
 +  Parameter 3:           blue​
 +  Parameter 4:
 +
 +The ''shift'' command discards parameter ''$1'' and moves each of the remaining parameters to the previous position (so the value in parameter ''$2'' is moved to ''$1'', and ''$3'' is moved to ''$2''). We can modify the previous script to demonstrate this:
 +
 +  $ cat params2​
 +  #!/usr/bin/bash​
 +  echo "Number of parameters:  $#"​
 +  
 +  echo "Parameter 0:           $0"
 +  echo "Parameter 1:           $1"
 +  echo "Parameter 2:           $2"
 +  echo "Parameter 3:           $3"
 +  echo "Parameter 4:           $4"
 +  
 +  echo "---- Performing shift ----"
 +  shift
 +  
 +  echo "Parameter 0:           $0"
 +  echo "Parameter 1:           $1"
 +  echo "Parameter 2:           $2"
 +  echo "Parameter 3:           $3"
 +  echo "Parameter 4:           $4"
 +  
 +  $ ./params2 red green blue
 +  Number of parameters:  3
 +  Parameter 0:           ./params2
 +  Parameter 1:           red
 +  Parameter 2:           green
 +  Parameter 3:           blue
 +  Parameter 4:           
 +  ---- Performing shift ----
 +  Parameter 0:           ./params2
 +  Parameter 1:           green
 +  Parameter 2:           blue
 +  Parameter 3:           
 +  Parameter 4:  
 +
 +The ''shift'' command is useful for looping through parameters, and for accessing parameters higher than number 9.
 +
 +==== Example Scripts ====
 +
 +=== Computer Architecture ===
 +
 +This script displays a message based on the architecture of the computer:
 +
 +  #!/usr/bin/bash​
 +  architecture="$(uname -m)" # uname gets system information​
 +  ​
 +  if [[ "$architecture" == "x86_64" ]]​
 +  then​
 +     echo "Your computer architecture is Intel/AMD x86_64."
 +  elif [[ "$architecture" == "aarch64" ]]​
 +  then​
 +     echo "Your computer uses the 64-bit Arm architecture."
 +  else​
 +    echo "Your computer uses an unrecognized architecture."
 +  fi
 +
 +=== Age Check ===
 +
 +This script checks whether a customer is of legal drinking age in Ontario:
 +
 +  #!/usr/bin/bash​
 +  read -p "Enter the customer's date of birth: " B​
 +  ​
 +  # Calculate the time in seconds that the customer turns/tuned 19​
 +  D="$(date -d "$B + 19 years" +%s)"
 +   ​
 +  # Get the current time in seconds
 +  NOW="$(date +%s)"
 +  
 +  # Tell the user if the customer is old enough to be served alcohol​
 +  # This tests checks to see if the customer's 19th birthday is
 +  # less than (before) the current date.
 +  if [[ "$D" -lt "$NOW" ]]​
 +  then​
 +      echo "The customer is of legal drinking age in Ontario."
 +  else​
 +      echo "The customer is too young to legally drink in Ontario."
 +  fi
 +
 +=== Coinflip ===
 +
 +This script flips a virtual coin:
 +
 +  #!/usr/bin/bash​
 +  
 +  COINFLIP=$((RANDOM % 2))​  # % is the modulus operator
 +  if [[ "$COINFLIP" == 0 ]]​
 +  then​
 +      echo "Heads! 🙂"
 +  else​
 +      echo "Tails 😦"
 +  fi
 +
 +The COINFLIP variable is set to the remainder of the division of ''$RANDOM'' by 2. Therefore, it will have a random value of 0 or 1.
 +
 +Note that this script uses extended Unicode characters -- however, for these to display properly, your terminal and your terminal font must both support the extended characters. Besides emoji, extended characters may be used to display accented characters, symbols, and characters from other languages.
 +
 +=== Cautious File Delete ===
 +
 +This script checks a file, provided as a positional argument (parameter), to ensure that it is a regular file and is writable, and then asks the user if they want to delete it. Note that this script uses the ''-f'' (file) test, ''-w'' (writeable) test, and combines a number of string tests with the ''||'' (OR) operator:
 +
 +  #!/usr/bin/bash​
 +  if [[ "$#" -ne 1 ]]
 +  then
 +      echo "$(basename $0): Error: one filename argument must be provided." >&2
 +      exit 1
 +  fi
 +  F="$1"  # Put the first (and only!) argument value into the variable F
 +  if [ ! -f "$F" ]​
 +  then​
 +      echo "The filename '$F' does not refer to a regular file - skipping."
 +  elif [ ! -w "$F" ]​
 +  then​
 +      echo "The file '$F' is not writeable (by you) - skipping."
 +  else​
 +      read -p "Delete the regular file '$F'? (Y/N): " YESNO​
 +      if [[ "$YESNO" == "Y" || "$YESNO" == "y" || "$YESNO" == "Yes"
 +             || "$YESNO" == "yes" || "$YESNO" == "YES" ]]​
 +      then​
 +          echo "Deleting the file '$F'..."
 +          rm "$F"    # We should add some code to check if the rm succeeds or fails​
 +          echo "...done."
 +      else​
 +          echo "Skipping the file '$F' as requested."
 +      fi​
 +  fi
  
  
ops102/bash_scripting.1709845137.txt.gz · Last modified: 2024/04/16 18:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki