ops102:bash_scripting
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ops102:bash_scripting [2024/03/07 21:01] – [The test Command] chris | ops102: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 '' | + | You can capture the output (stdout) of a command as a string using the notation '' |
$ echo "The current date and time is: $(date)" | $ echo "The current date and time is: $(date)" | ||
Line 404: | Line 402: | ||
[[ -f " | [[ -f " | ||
+ | |||
+ | == Tips on Using Tests == | ||
+ | |||
+ | * Remember to quote any arguments which include whitespace, or which may be null (empty). | ||
+ | * Be careful with the ''<'' | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | |||
+ | Arguments to a script are called parameters. You can access the parameters using the special variables '' | ||
+ | |||
+ | The special variable '' | ||
+ | |||
+ | Here is a simple script which shows you what parameters have been received: | ||
+ | |||
+ | # | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | |||
+ | When you run this script with three parameters (red, green, and blue), you get this output: | ||
+ | |||
+ | $ ./params red green blue | ||
+ | Number of parameters: | ||
+ | Parameter 0: | ||
+ | Parameter 1: | ||
+ | Parameter 2: | ||
+ | Parameter 3: | ||
+ | Parameter 4: | ||
+ | |||
+ | The '' | ||
+ | |||
+ | $ cat params2 | ||
+ | # | ||
+ | echo " | ||
+ | | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | | ||
+ | echo "---- Performing shift ----" | ||
+ | shift | ||
+ | | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | | ||
+ | $ ./params2 red green blue | ||
+ | Number of parameters: | ||
+ | Parameter 0: | ||
+ | Parameter 1: red | ||
+ | Parameter 2: green | ||
+ | Parameter 3: blue | ||
+ | Parameter 4: | ||
+ | ---- Performing shift ---- | ||
+ | Parameter 0: | ||
+ | Parameter 1: green | ||
+ | Parameter 2: blue | ||
+ | Parameter 3: | ||
+ | Parameter 4: | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ==== Example Scripts ==== | ||
+ | |||
+ | === Computer Architecture === | ||
+ | |||
+ | This script displays a message based on the architecture of the computer: | ||
+ | |||
+ | # | ||
+ | architecture=" | ||
+ | | ||
+ | if [[ " | ||
+ | then | ||
+ | echo "Your computer architecture is Intel/AMD x86_64." | ||
+ | elif [[ " | ||
+ | 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: | ||
+ | |||
+ | # | ||
+ | read -p "Enter the customer' | ||
+ | | ||
+ | # Calculate the time in seconds that the customer turns/tuned 19 | ||
+ | D=" | ||
+ | | ||
+ | # Get the current time in seconds | ||
+ | NOW=" | ||
+ | | ||
+ | # Tell the user if the customer is old enough to be served alcohol | ||
+ | # This tests checks to see if the customer' | ||
+ | # less than (before) the current date. | ||
+ | if [[ " | ||
+ | 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: | ||
+ | |||
+ | # | ||
+ | | ||
+ | COINFLIP=$((RANDOM % 2)) | ||
+ | if [[ " | ||
+ | then | ||
+ | echo " | ||
+ | else | ||
+ | echo "Tails 😦" | ||
+ | fi | ||
+ | |||
+ | The COINFLIP variable is set to the remainder of the division of '' | ||
+ | |||
+ | 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), | ||
+ | |||
+ | # | ||
+ | if [[ " | ||
+ | then | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | F=" | ||
+ | if [ ! -f " | ||
+ | then | ||
+ | echo "The filename ' | ||
+ | elif [ ! -w " | ||
+ | then | ||
+ | echo "The file ' | ||
+ | else | ||
+ | read -p " | ||
+ | if [[ " | ||
+ | || " | ||
+ | then | ||
+ | echo " | ||
+ | rm " | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | fi | ||
ops102/bash_scripting.1709845278.txt.gz · Last modified: 2024/04/16 18:10 (external edit)