Beginner's Introduction to Shell Script:PART 1
1. #
Comments. Lines beginning with a # are comments and will not be executed.
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.
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].
partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING.
full quoting [single quote]. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than "STRING".
comma operator. The comma operator links together a series of arithmetic operations. All are evaluated, but only the last one is returned.
Filename path separator [forward slash]. Separates the components of a filename (as in /home/bozo/projects/Makefile).
command substitution. The `command` construct makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.
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 *
abs-book.sgml add-drive.sh agram.sh alias.sh
* arithmetic operator. In 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.
condition?result-if-true:result-if-false
?
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).
var1=5
var2=23skidoo
echo $var1 # 5
echo $var2 # 23skidoo
A $ 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=123
( 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 "ex42.sh" (copydir.sh) example.
{} \;
The ";" 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.
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.
Array[1]=slot_1
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 $[ ].
a=3
b=7
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 operator. In 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 operator. In 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.^ beginning-of-line.
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=23bash$ echo variable1 variable1 bash$ echo $variable1 232. #!/bin/bash # ex9.sh
a=375 hello=$a
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
echo "var1=$var1 var2=$var2 var3=$var3"
2.2 Variable Assignment
1 #!/bin/bash
# Naked variables
# Assignment
a=879
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
b=$a
echo $b
2.3 Bash Variables Are Untyped
1. #!/bin/bash
# int-or-string.sh
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.
c=BB34
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
echo
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\!
hello!
bash$ echo "hello\!"
hello\!
bash$ echo \
>
bash$ echo "\"
>
bash$ echo \a
a
bash$ echo "\a"
\a
bash$ echo x\ty
xty
bash$ echo "x\ty"
x\ty
bash$ echo -e x\ty
xty
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
- \n
- means newline
- \r
- means return
- \t
- means tab
- \v
- means vertical tab
- \b
- means backspace
- \a
- means alert (beep or flash)
- \0xx
- 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 executed.in 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 ]
then
command 1
command 2
...
else # Or else ...
# Adds default code block executing if original condition tests false.
command 3
command 4
...
fi
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 builtinbash$ type '[' [ is a shell builtinbash$ type '[[' [[ is a shell keywordbash$ type ']]' ]] is a shell keywordbash$ type ']' bash: type: ]: not found2.7.2. File test operators
-pReturns true if...
- -e
- file exists
- -a
- file exists
This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.- -f
- file is a regular file (not a directory or device file)
- -s
- file is not zero size
- -d
- file is a directory
- -b
- file is a block device
- -c
file is a character device
- file is a pipe
- -h
- file is a symbolic link
- -L
- file is a symbolic link
- -S
- file is a socket
- -t
- file (descriptor) is associated with a terminal device
- -r
- file has read permission (for the user running the test)
- -w
- file has write permission (for the user running the test)
- -x
- file has execute permission (for the user running the test)
- -g
- set-group-id (sgid) flag set on file or directory
- -u
- set-user-id (suid) flag set on file
- -k
- sticky bit set
- -O
- you are owner of file
- -G
- group-id of file same as yours
- -N
- 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 comparisonstring comparison
- -eq
- is equal to
if [ "$a" -eq "$b" ]- -ne
- is not equal to
if [ "$a" -ne "$b" ]- -gt
- is greater than
if [ "$a" -gt "$b" ]- -ge
- is greater than or equal to
if [ "$a" -ge "$b" ]- -lt
- is less than
if [ "$a" -lt "$b" ]- -le
- 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"))
- =
- is equal to
if [ "$a" = "$b" ]
Note 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.-z string is null, that is, has zero length-n
string is not null.
2.8. Operators
assignmentarithmetic operators
- variable assignment
- Initializing or changing the value of a variable
- =
+= 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)
- <<=
- left-shift-equal
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
|| OR miscellaneous operators Similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation.
#!/bin/bash # c-vars.sh # Manipulating a variable, C-style, using the (( ... )) construct.echo (( 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 echo2.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
Operator Meaning Comments HIGHEST PRECEDENCE var++ var-- post-increment, post-decrement C-style operators ++var --var pre-increment, pre-decrement ! ~ negation logical / bitwise, inverts sense of following operator ** exponentiation arithmetic operation * / % multiplication, division, modulo arithmetic operation + - addition, subtraction arithmetic operation << >> left, right shift bitwise -z -n unary comparison string is/is-not null -e -f -t -x, etc. unary comparison file-test < -lt > -gt <= -le >= -ge compound comparison string and integer -nt -ot -ef compound comparison file-test == -eq != -ne equality / inequality test operators, string and integer & AND bitwise ^ XOR exclusive OR, bitwise | OR bitwise && -a AND logical, compound comparison || -o OR logical, compound comparison ?: trinary operator C-style = assignment (do not confuse with equality test) *= /= %= += -= <<= >>= &= combination assignment times-equal, divide-equal, mod-equal, etc. , comma links a sequence of operations LOWEST PRECEDENCE
No comments:
Post a Comment