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

Both sides previous revisionPrevious revision
Next revision
Previous revision
ops102:bash_scripting_1 [2024/11/22 21:11] chrisops102:bash_scripting_1 [2025/03/11 15:11] (current) chris
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 188: 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 203: 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.
ops102/bash_scripting_1.1732309892.txt.gz · Last modified: 2024/11/22 21:11 by chris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki