User Tools

Site Tools


ops102:bash_scripting_1

Differences

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

Link to this comparison view

Next revision
Previous revision
ops102:bash_scripting_1 [2024/03/07 22:52] – created chrisops102:bash_scripting_1 [2025/03/11 15:11] (current) chris
Line 1: Line 1:
-====== Bash Scripting =====+====== Bash Scripting =====
  
 ===== What is a script? ===== ===== What is a script? =====
Line 38: Line 38:
   The current date and time is:   The current date and time is:
   Sat Mar  6 12:03:32 EST 2038   Sat Mar  6 12:03:32 EST 2038
 +
 +===== Comments =====
 +
 +A comment in a bash script starts with a sharp symbol (#) and is ignored by the shell interpreter:
 +
 +<code># test script #1
 +# written by Jason Bourne</code>
 +
 +A comment may also be just one portion of a line:
 +
 +<code>cd - # change to the user's previous working directory</code>
 +
 +Note that a shbang line is a comment from the point of view of the shell interpreter -- it's there for the kernel to use, not the shell!
  
 ===== Variables ===== ===== Variables =====
Line 47: Line 60:
   A=5   A=5
   B=World   B=World
 +  TheNameOfTheUser=Jason
  
 If the variable does not exist, it will be created. If it does exist, the previous value will be discarded. If the variable does not exist, it will be created. If it does exist, the previous value will be discarded.
Line 54: Line 68:
 Unlike some computer languages such as C, variables do not need to be declared. Variables are not typed -- they may be used as strings, integers, or decimal values. Unlike some computer languages such as C, variables do not need to be declared. Variables are not typed -- they may be used as strings, integers, or decimal values.
  
 +Note that there must not be a space on either side of the equal sign.
 ==== Accessing a Variable ==== ==== Accessing a Variable ====
  
Line 102: Line 117:
   Hello $B   Hello $B
  
-You should always double-quote variables that may contain a space in their value when using them as command arguments.+You should always double-quote variables that may contain a space in their value when using them as command arguments. This is especially true for filenames -- you never know when a user is going to put a space in a filename! Many scripts work find with opaque filenames (those containing no whitespace) but fail with non-opaque names.
  
-This is especially true for filenames -- you never know when a user is going to put a space in a filename! Many scripts work find with opaque filenames (those containing no whitespacebut fail with non-opaque names.+Here is an example -- the difference between the first ''ls'' statement (which does not double-quote the variableand the second ''ls'' statement (where the variable expansion is double-quoted): 
 + 
 +  $ touch "red maple" 
 +   
 +  $ FILE="red maple" 
 +   
 +  $ ls $FILE 
 +  ls: cannot access 'red': No such file or directory 
 +  ls: cannot access 'maple': No such file or directory 
 +   
 +  $ ls "$FILE" 
 +  'red maple'
  
 === Backslashes === === Backslashes ===
Line 170: Line 196:
   read -p "Please enter a filename: " FILE   read -p "Please enter a filename: " FILE
   echo "Saving your name into the file..."   echo "Saving your name into the file..."
-  echo "NAME=$NAME" >>$FILE echo "Done."+  echo "NAME=$NAME" >>$FILE 
 +  echo "Done."
  
 ===== Command Capture ===== ===== Command Capture =====
Line 187: Line 214:
   $ A=`ls`   $ A=`ls`
  
-This is an archaic syntax with is depricated -- avoid doing this. Some fonts make it hard to distiguish between backticks and single-quotes, and nesting backticks is difficult.+This is an archaic syntax which is depricated -- avoid doing this. Some fonts make it hard to distiguish between backticks and single-quotes, and nesting backticks is difficult.
  
 ===== Arithmetic ===== ===== Arithmetic =====
  
-Bash can perform __integer__ arithmetic.+Bash can perform integer arithmetic.
  
 To evaluate an arithmetic expression and return a value, use ''<nowiki>$(( ))</nowiki>'': To evaluate an arithmetic expression and return a value, use ''<nowiki>$(( ))</nowiki>'':
Line 202: Line 229:
   12   12
   $ echo $B   $ echo $B
 +  13
  
 Note that inside the double-parenthesis, spaces don't matter, and it is not necessary to use a dollar-sign [$] in front of variables being accessed. Note that inside the double-parenthesis, spaces don't matter, and it is not necessary to use a dollar-sign [$] in front of variables being accessed.
Line 213: Line 241:
   101   101
   $ ((C=A*B*2))   $ ((C=A*B*2))
-  $ echo "The answer is $C'+  $ echo "The answer is $C"
   The answer is 2626   The answer is 2626
  
Line 273: Line 301:
     echo "The course code 'OPS102' was found in the file."     echo "The course code 'OPS102' was found in the file."
   elif grep -q "ULI101" courses.txt   elif grep -q "ULI101" courses.txt
 +  then
     echo "The course code 'ULI101' was found in the file.     echo "The course code 'ULI101' was found in the file.
   else   else
Line 288: Line 317:
     echo "The course code 'OPS102' was found in the file $FILE."     echo "The course code 'OPS102' was found in the file $FILE."
   elif grep -q "ULI101" courses.txt   elif grep -q "ULI101" courses.txt
 +  then
     echo "The course code 'ULI101' was found in the file $FILE.     echo "The course code 'ULI101' was found in the file $FILE.
   else   else
Line 308: Line 338:
   fi   fi
  
-However, this syntax is a bit ugly! So bash provides a synonym for ''test'' which is ''[[ ]]'':+However, this syntax is a bit ugly! So bash provides a synonym for ''test'' which is ''<nowiki>[[ ]]</nowiki>'':
  
   if [[ "$NAME" == "Chris" ]]   if [[ "$NAME" == "Chris" ]]
Line 329: Line 359:
   [[ -f filename ]]   # True if filename is a regular file   [[ -f filename ]]   # True if filename is a regular file
   [[ -d filename ]]   # True if filename is a directory   [[ -d filename ]]   # True if filename is a directory
-  [[ -filename ]]   # True if filename is a symbolic link+  [[ -filename ]]   # True if filename is a symbolic link
  
 **Note:** If a symbolic link points to a file, then both the ''-f'' and ''-l'' tests will succeed. If a symbolic link points to a directory, then both the ''-d'' and ''-l'' tests will succeed. **Note:** If a symbolic link points to a file, then both the ''-f'' and ''-l'' tests will succeed. If a symbolic link points to a directory, then both the ''-d'' and ''-l'' tests will succeed.
Line 408: Line 438:
   * 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!)   * 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 ====+===== 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. 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.
Line 471: Line 501:
 The ''shift'' command is useful for looping through parameters, and for accessing parameters higher than number 9. The ''shift'' command is useful for looping through parameters, and for accessing parameters higher than number 9.
  
-==== Example Scripts ====+===== Example Scripts =====
  
 === Computer Architecture === === Computer Architecture ===
ops102/bash_scripting_1.1709851957.txt.gz · Last modified: 2024/04/16 18:10 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki