Wednesday, 12 September 2012

Beginner's Introduction to Shell Script:PART 1

Beginner's Introduction to Shell Script:PART 1 

1. Special Characters:

1. #
Comments. Lines beginning with a #  are comments and will not be executed.

2. ;
Command separator [semicolon]. Permits putting two or more commands on the same line.
eg: $echo hello; echo there

3. ;;
Terminator in a case option [double semicolon].

4. "
partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING.

5. '
full quoting [single quote]. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than "STRING".

6. ,
comma operatorThe comma operator links together a series of arithmetic operations. All are evaluated, but only the last one is returned.

7. /
Filename path separator [forward slash]. Separates the components of a filename (as in /home/bozo/projects/Makefile).

8. `
command substitutionThe `command` construct makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.

9. :
null command [colon]. This is the shell equivalent of a "NOP".

    echo $?   # 0

10. !
reverse (or negate) the sense of a test or exit status [bang]. The ! operator inverts the exit status of the command to which it is applied.
11. *
wild card [asterisk]. The * character serves as a "wild card" for filename expansion in globbing. By itself, it matches every filename in a given directory.

bash$ echo *

      * arithmetic operatorIn the context of arithmetic operations, the * denotes multiplication.        

      ** A double asterisk can represent the exponentiation operator or extended file-match globbing.

12. ?
test operator. Within certain expressions, the ? indicates a test for a condition.

wild card. The ? character serves as a single-character "wild card" for filename expansion in globbing, as well as representing one character in an extended regular expression.
13. $
Variable substitution (contents of a variable).


echo $var1     # 5
echo $var2     # 23skidoo
$ prefixing a variable name indicates the value the variable holds.
          A "$" addresses the end of a line of text.

14. ${}
15. $' ... '
16. $*$@
17. $?
exit status variable. 
18. $$
process ID variable. The $$ variable holds the process ID of the script in which it appears.
19. ()
command group.

(a=hello; echo $a)
   ( a=321; )       

  echo "a = $a"   # a = 123
  # "a" within parentheses acts like a local variable.

20. {a..z}
Extended Brace expansion.

echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
# Echoes characters between a and z.

echo {0..3} # 0 1 2 3
# Echoes characters between 0 and 3.
21. {}
Block of code [curly brackets]. Also referred to as an inline group, this construct, in effect, creates an anonymous function
placeholder for text. Used after xargs -i (replace strings option). The {} double curly brackets are a placeholder for output text.

ls . | xargs -i -t cp ./{} $1
#            ^^         ^^

# From "" ( example.
   {} \;
pathname. Mostly used in find constructs. This is not a shell builtin.
NoteThe ";" ends the -exec option of a find command sequence. It needs to be escaped to protect it from interpretation by the shell.
22. [ ]
Test expression between [ ]. Note that [ is part of the shell builtin test (and a synonym for it), not a link to the external command /usr/bin/test.
23. [[ ]]
Test expression between [[ ]]. More flexible than the single-bracket [ ] test, this is a shell keyword.
24. [ ]
array element.
In the context of an array, brackets set off the numbering of each element of that array.

echo ${Array[1]}
25. [ ]
range of characters.
As part of a regular expression, brackets delineate a range of characters to match.
26. $[ ... ]
integer expansion.
Evaluate integer expression between $[ ].


echo $[$a+$b]   # 10
echo $[$a*$b]   # 21
Note that this usage is deprecated, and has been replaced by the (( ... )) construct.
27. (( ))
integer expansion.
Expand and evaluate integer expression between (( )).
28.  > &> >& >> < <> (Redirection) 
redirection used in a here document.
redirection used in a here string.
29. \<\>
bash$ grep '\<the\>' textfile
30. |

pipe. Passes the output (stdout) of a previous command to the input (stdin) of the next one,
31. >|
force redirection (even if the noclobber option is set). This will forcibly overwrite an existing file.
32. ||
OR logical operatorIn a test construct, the || operator causes a return of 0 (success) if either of the linked test conditions is true.
33. &
Run job in background. A command followed by an & will run in the background.
AND logical operatorIn a test construct, the && operator causes a return of 0 (success) only if both the linked test conditions are true.
34. --          The double-dash -- prefixes long (verbatim) options to commands.

35.  ~
home directory [tilde].
current working directory. 
previous working directory. 
36. Control Characters
change the behavior of the terminal or text display. 
2. Control characters are not normally useful inside a script.
  • Ctl-A Moves cursor to beginning of line of text (on the command-line).
  • Ctl-B Backspace (nondestructive).
  • Ctl-C Break. Terminate a foreground job.
  • Ctl-D Log out from a shell (similar to exit). EOF (end-of-file). This also terminates input from stdin. When typing text on the console or in an xterm window, Ctl-D erases the character under the cursor. When there are no characters present, Ctl-Dlogs out of the session, as expected. In an xterm window, this has the effect of closing the window.
  • Ctl-E Moves cursor to end of line of text (on the command-line).
  • Ctl-F Moves cursor forward one character position (on the command-line).   
  • Ctl-G BEL. On some old-time teletype terminals, this would actually ring a bell. In an xterm it might beep.
  • Ctl-H Rubout (destructive backspace). Erases characters the cursor backs over while backspacing.
  • Ctl-I Horizontal tab
  • Ctl-J Newline (line feed). In a script, may also be expressed in octal notation -- '\012' or in hexadecimal -- '\x0a'.
  • Ctl-K Vertical tab. When typing text on the console or in an xterm window,
  •  Ctl-K erases from the character under the cursor to end of line. Within a script, Ctl-K may behave differently, as in Lee Lee Maschmeyer's example, below.
  • Ctl-L Formfeed (clear the terminal screen). In a terminal, this has the same effect as the clear command. When sent to a printer, a Ctl-L causes an advance to end of the paper sheet.
  • Ctl-M Carriage return.
  • Ctl-N Erases a line of text recalled from history buffer [8] (on the command-line).
  • Ctl-O Issues a newline (on the command-line).
  • Ctl-P Recalls last command from history buffer (on the command-line).
  • Ctl-Q Resume (XON). This resumes stdin in a terminal.
  • Ctl-R Backwards search for text in history buffer (on the command-line).
  • Ctl-S Suspend (XOFF). This freezes stdin in a terminal. (Use Ctl-Q to restore input.)
  • Ctl-T Reverses the position of the character the cursor is on with the previous character (on the command-line)
  • Ctl-U Erase a line of input, from the cursor backward to beginning of line. In some settings, Ctl-U erases the entire line of input, regardless of cursor position.
  • Ctl-V When inputting text, Ctl-V permits inserting control characters. For example, the following two are equivalent:
      • echo -e '\x0a'
        echo <Ctl-V><Ctl-J>
      • Ctl-V is primarily useful from within a text editor.
      • Ctl-W When typing text on the console or in an xterm window, Ctl-W erases from the character under the cursor backwards to the first instance ofwhitespace. In some settings, Ctl-W erases backwards to first non-alphanumeric character.
      • Ctl-X In certain word processing programs, Cuts highlighted text and copies to clipboard.
      • Ctl-Y Pastes back text previously erased (with Ctl-U or Ctl-W).
      • Ctl-Z Pauses a foreground job.
  • ----------------------------------------------------------

2. 1 Variable Substitution:

1. bash$ variable1=23
   bash$ echo variable1

   bash$ echo $variable1

2. #!/bin/bash


  echo hello    # hello
  echo $hello   # 375
  echo ${hello} # 375

  # Quoting . . .
  echo "$hello"    # 375
  echo "${hello}"  # 375

  echo '$hello'  # $hello

  var1=21  var2=22  var3=$V3
  echo "var1=$var1   var2=$var2   var3=$var3"

2.2 Variable Assignment

1 #!/bin/bash
  # Naked variables

  # Assignment
  echo "The value of \"a\" is $a."

  # Assignment using 'let'
  let a=16+5
  echo "The value of \"a\" is now $a."

2. #!/bin/bash

   a=23              # Simple case
   echo $a
   echo $b

2.3 Bash Variables Are Untyped

1. #!/bin/bash

   a=2334                   # Integer.
   let "a += 1"
   echo "a = $a "           # a = 2335
   echo                     # Integer, still.

   b=${a/23/BB}             # Substitute "BB" for "23".
                         # This transforms $b into a string.
   echo "b = $b"            # b = BB35
   declare -i b             # Declaring it an integer doesn't help.
   echo "b = $b"            # b = BB35

   let "b += 1"             # BB35 + 1
   echo "b = $b"            # b = 1
   echo                     # Bash sets the "integer value" of a string to 0.

   echo "c = $c"            # c = BB34
   d=${c/BB/23}             # Substitute "23" for "BB".
                         # This makes $d an integer.
   echo "d = $d"            # d = 2334
   let "d += 1"             # 2334 + 1
   echo "d = $d"            # d = 2335
   exit $?

2.4. Special Variable Types

Arguments passed to the script from the command line : $0$1$2$3 . . . $0 is the name of the script itself, $1 is the first argument, $2 the second, $3 the third, and so forth.  After $9, the arguments must be enclosed in brackets,  for example, ${10}${11}${12}. The special variables $* and $@ denote all the positional parameters.
he shift command reassigns the positional parameters, in effect shifting them to the left one notch. $1 <--- $2$2 <--- $3$3 <--- $4, etc. The shift command works in a similar fashion on parameters passed to a function

2.5. Quoting

Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special meaning of $ is turned off. Within single quotes,
bash$ echo hello\!
bash$ echo "hello\!"

bash$ echo \
bash$ echo "\"
bash$ echo \a
bash$ echo "\a"

bash$ echo x\ty
bash$ echo "x\ty"

bash$ echo -e x\ty
bash$ echo -e "x\ty"
x       y

2.5.1 Escaping

Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to interpret that character literally.

Special meanings of certain escaped characters
used with echo and sed
means newline
means return
means tab
means vertical tab
means backspace
means alert (beep or flash)
translates to the octal ASCII equivalent of 0nn, where nn is a string of digits
gives the quote its literal meaning

echo "Hello"                     # Hello
echo "\"Hello\" ... he said."    # "Hello" ... he said.
gives the dollar sign its literal meaning (variable name following \$ will not be referenced)

echo "\$variable01"           # $variable01
echo "The book cost \$7.98."  # The book cost $7.98.
gives the backslash its literal meaning
2.6 Exit and Exit Status

The exit command terminates a script, just as in a C program.
Every command returns an exit status A successful command returns a 0, while an unsuccessful one returns a non-zero value

$? reads the exit status of the last command the function.

2.7.1. Test Constructs

An if/then construct tests whether the exit status of a list of commands is 0.
There exists a dedicated command called [ (left bracket special character). It is a synonym for test.

Bash introduced the [[ ... ]] extended test command, which performs comparisons in a manner more familiar to programmers from other languages. Note that [[ is a keyword, not a command.
The (( ... )) and let ... constructs return an exit status.

if [ condition-true ]
   command 1
   command 2
else  # Or else ...
      # Adds default code block executing if original condition tests false.
   command 3
   command 4

The if test condition-true construct is the exact equivalent of if [ condition-true ]. As it happens, the left bracket, [ , is a token [1] which invokes thetest command.

bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword
bash$ type ']'
bash: type: ]: not found

2.7.2. File test operators

Returns true if...
file exists
file exists
This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.
file is a regular file (not a directory or device file)
file is not zero size
file is a directory
file is a block device
file is a character device
file is a pipe
file is a symbolic link
file is a symbolic link
file is a socket
file (descriptor) is associated with a terminal device
file has read permission (for the user running the test)
file has write permission (for the user running the test)
file has execute permission (for the user running the test)
set-group-id (sgid) flag set on file or directory
set-user-id (suid) flag set on file
sticky bit set
you are owner of file
group-id of file same as yours
file modified since it was last read
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
f1 -ef f2
files f1 and f2 are hard links to the same file
"not" -- reverses the sense of the tests above (returns true if condition absent).

2.7.3. Other Comparison Operators

integer comparison
is equal to
if [ "$a" -eq "$b" ]
is not equal to
if [ "$a" -ne "$b" ]
is greater than
if [ "$a" -gt "$b" ]
is greater than or equal to
if [ "$a" -ge "$b" ]
is less than
if [ "$a" -lt "$b" ]
is less than or equal to
if [ "$a" -le "$b" ]
is less than (within double parentheses)
(("$a" < "$b"))
is less than or equal to (within double parentheses)
(("$a" <= "$b"))
is greater than (within double parentheses)
(("$a" > "$b"))
is greater than or equal to (within double parentheses)
(("$a" >= "$b"))
string comparison
is equal to
if [ "$a" = "$b" ]
CautionNote the whitespace framing the =.
if [ "$a"="$b" ] is not equivalent to the above.
is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
See Example 27-11 for an application of this comparison operator.
string is null, that is, has zero length

string is not null.
compound comparison
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
logical or
exp1 -o exp2 returns true if either exp1 or exp2 is true.

2.8. Operators

variable assignment
Initializing or changing the value of a variable
arithmetic operators

# Bash, version 2.02, introduced the "**" exponentiation operator.

let "z=5**3"    # 5 * 5 * 5
echo "z = $z"   # z = 125
modulo, or mod (returns the remainder of an integer division operation)

bash$ expr 5 % 3
5/3 = 1, with remainder 2
plus-equal (increment variable by a constant) [1]
let "var += 5" results in var being incremented by 5.
minus-equal (decrement variable by a constant)
times-equal (multiply variable by a constant)
let "var *= 4" results in var being multiplied by 4.
slash-equal (divide variable by a constant)
mod-equal (remainder of dividing variable by a constant)
bitwise operators
bitwise left shift (multiplies by 2 for each shift position)
let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)
bitwise right shift (divides by 2 for each shift position)
right-shift-equal (inverse of <<=)
bitwise AND
bitwise AND-equal
bitwise OR
bitwise OR-equal
bitwise NOT
bitwise XOR
bitwise XOR-equal
logical (boolean) operators

if [ ! -f $FILENAME ]
miscellaneous operators
Comma operator
The comma operator chains together two or more arithmetic operations. All the operations are evaluated (with possible side effects.

let "t1 = ((5 + 3, 7 - 1, 15 - 4))"
echo "t1 = $t1"           ^^^^^^  # t1 = 11
Similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation.

# Manipulating a variable, C-style, using the (( ... )) construct.

(( a = 23 ))  #  Setting a value, C-style,
              #+ with spaces on both sides of the "=".
echo "a (initial value) = $a"   # 23

(( a++ ))     #  Post-increment 'a', C-style.
echo "a (after a++) = $a"       # 24

(( a-- ))     #  Post-decrement 'a', C-style.
echo "a (after a--) = $a"       # 23

(( ++a ))     #  Pre-increment 'a', C-style.
echo "a (after ++a) = $a"       # 24

(( --a ))     #  Pre-decrement 'a', C-style.
echo "a (after --a) = $a"       # 23


2.8.1. Operator Precedence

In a script, operations execute in order of precedence: the higher precedence operations execute before the lower precedence ones. 
Table 8-1. Operator Precedence
var++ var--post-increment, post-decrementC-style operators
++var --varpre-increment, pre-decrement
! ~negationlogical / bitwise, inverts sense of following operator
**exponentiationarithmetic operation
* / %multiplication, division, moduloarithmetic operation
+ -addition, subtractionarithmetic operation
<< >>left, right shiftbitwise
-z -nunary comparisonstring is/is-not null
-e -f -t -x, etc.unary comparisonfile-test
< -lt > -gt <= -le >= -gecompound comparisonstring and integer
-nt -ot -efcompound comparisonfile-test
== -eq != -neequality / inequalitytest operators, string and integer
^XORexclusive OR, bitwise
&& -aANDlogicalcompound comparison
|| -oORlogical, compound comparison
?:trinary operatorC-style
=assignment(do not confuse with equality test)
*= /= %= += -= <<= >>= &=combination assignmenttimes-equal, divide-equal, mod-equal, etc.
,commalinks a sequence of operations

No comments:

Post a Comment