Wednesday 17 October 2012

Beginner's Introduction to Shell Script: PART 5

Beginner's Introduction to Shell Script: PART 1

Beginner's Introduction to Shell Script: PART 2
Beginner's Introduction to Shell Script: PART 3
Beginner's Introduction to Shell Script: PART 4
Beginner's Introduction to Shell Script: PART 6

5. Advanced Topics

5. 1. A Brief Introduction to Regular Expressions
The main uses for Regular Expressions (REs) are text searches and string manipulation

1.The asterisk -- * -- matches any number of repeats of the character string.


2. The dot -- . -- matches any one character, except a newline.

3. The caret -- ^ -- matches the beginning of a line, but sometimes, 
depending on context,

4. The dollar sign -- $ -- at the end of an RE matches the end of a line.

5. Brackets -- [...] -- enclose a set of characters to match in a single RE.

6. The backslash -- \ -- escapes a special character, which means that character gets interpreted literally.

7. The question mark -- ? -- matches zero or one of the previous RE. It is generally used for matching single characters.

8. The plus -- + -- matches one or more of the previous RE. It serves a role similar to the *

9. Escaped "curly brackets" -- \{ \} -- indicate the number of occurrences of a preceding RE to match.

10. Parentheses -- ( ) -- enclose a group of REs.

11. The -- | -- "or" RE operator matches any of a set of alternate characters.

Some versions of seded, and ex support escaped versions of the extended Regular Expressions described above



5.2. Globbing

set -f command disables globbing,

6. I/O Redirection

There are always three default files  open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen).

Each open file gets assigned a file descriptor.The file descriptors for stdinstdout, and stderr are 0, 1, and 2, respectively. 

Closing File Descriptors

n<&-
Close input file descriptor n.
0<&-<&-
Close stdin.
n>&-
Close output file descriptor n.
1>&->&-
Close stdout.

6.1. Using exec

An exec <filename command redirects stdin to a file.


7. Subshells

Running a shell script launches a new process, a subshell.

8. Aliases

alias lm="ls -l | more" in the ~/.bashrc file, then each lm [1] typed at the command-line will automatically be replaced by a ls -l | more.


8.1. /dev

The /dev directory contains entries for the physical devices that may or may not be present in the hardware.the /dev directory contains loopback devices, such as /dev/loop0.
A few of the pseudo-devices in /dev have other specialized uses, such as /dev/null/dev/zero/dev/urandom/dev/sda1 (hard drive partition), /dev/udp(User Datagram Packet port), and /dev/tcp.


8.2. /proc


The /proc directory is actually a pseudo-filesystem. The files in /proc mirror currently running system and kernel processes
 and contain information and statistics about them.                                                                                 bash$ cat /proc/interrupts
bash$ cat /proc/devices

bash$ cat /proc/partitions

bash$ cat /proc/loadavg

bash$ cat /proc/apm

bash$ fgrep Mem /proc/meminfo

/dev/zero ... /dev/null

cat $filename >/dev/null
# Contents of the file will not list to stdout.





9. Options

The set command enables options within a script. At the point in the script where you want the options to take effect, use set -o option-name or, in short form, set -option-abbrev. These two forms are equivalent.

      #!/bin/bash
      set -o verbose
      # Echoes all commands before executing.





      #!/bin/bash
      set -v
      # Exact same effect as above.

10. An alternate method of enabling options in a script is to specify them immediately following the #! script header.

bash -v script-name

bash -o verbose script-name


Table: Bash options
AbbreviationNameEffect
-Bbrace expansionEnable brace expansion (default setting = on)
+Bbrace expansionDisable brace expansion
-CnoclobberPrevent overwriting of files by redirection (may be overridden by >|)
-D(none)List double-quoted strings prefixed by $, but do not execute commands in script
-aallexportExport all defined variables
-bnotifyNotify when jobs running in background terminate (not of much use in a script)
-c ...(none)Read commands from ...
checkjobsInforms user of any open jobs upon shell exit. Introduced in version 4 of Bash, and still "experimental." Usage: shopt -s checkjobs (Caution: may hang!)
-eerrexitAbort script at first error, when a command exits with non-zero status (except in until or while loopsif-testslist constructs)
-fnoglobFilename expansion (globbing) disabled
globstarglobbing star-matchEnables the ** globbing operator (version 4+ of Bash). Usage: shopt -s globstar
-iinteractiveScript runs in interactive mode
-nnoexecRead commands in script, but do not execute them (syntax check)
-o Option-Name(none)Invoke the Option-Name option
-o posixPOSIXChange the behavior of Bash, or invoked script, to conform to POSIX standard.
-o pipefailpipe failureCauses a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.
-pprivilegedScript runs as "suid" (caution!)
-rrestrictedScript runs in restricted mode (see Chapter 22).
-sstdinRead commands from stdin
-t(none)Exit after first command
-unounsetAttempt to use undefined variable outputs error message, and forces an exit
-vverbosePrint each command to stdout before executing it
-xxtraceSimilar to -v, but expands commands
-(none)End of options flag. All other arguments are positional parameters.
--(none)Unset positional parameters. If arguments given (-- arg1 arg2), positional parameters set to arguments.
11. Perl embedded in a Bash script


[sankar@new-host ~]$ perl -e 'print "This line prints from an embedded Perl script.\n";'
This line prints from an embedded Perl script.
[sankar@new-host ~]$

bash$ bash bashandperl.sh
Greetings from the Bash part of the script.


bash$ perl -x bashandperl.sh
Greetings from the Perl part of the script.

12. Tests and Comparisons: Alternatives

a=8

# All of the comparisons below are equivalent.
test "$a" -lt 16 && echo "yes, $a < 16"         # "and list"
/bin/test "$a" -lt 16 && echo "yes, $a < 16" 
[ "$a" -lt 16 ] && echo "yes, $a < 16" 
[[ $a -lt 16 ]] && echo "yes, $a < 16"          # Quoting variables within
(( a < 16 )) && echo "yes, $a < 16"             # [[ ]] and (( )) not necessary.

13. "Colorizing" Scripts

13.1.The simplest, and perhaps most useful ANSI escape sequence is bold text, \033[1m ... \033[0m. The \033 represents an escape, the "[1" turns on the bold attribute, while the "[0" switches it off. The "m" terminates each term of the escape sequence.

[sankar@new-host ~]$ echo -e "\033[1mThis is bold text.\033[0m"
This is bold text.

[sankar@new-host ~]$ echo -e "\033[4mThis is underlined text.\033[0m"
This is underlined text. 
This is underlined text.
NoteWith an echo, the -e option enables the escape sequences.
13.2. Other escape sequences change the text and/or background color.


[sankar@new-host ~]$ echo -e '\E[34;47mThis prints in blue.'; tput sgr0

This prints in blue.

[sankar@new-host ~]$ echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0
yellow text on blue background

[sankar@new-host ~]$ echo -e '\E[1;33;44m'"BOLD yellow text on blue background"; tput sgr0
BOLD yellow text on blue background


NoteIt's usually advisable to set the bold attribute for light-colored foreground text.

The tput sgr0 restores the terminal settings to normal. Omitting this lets all subsequent output from that particular terminal remain blue.




NoteSince tput sgr0 fails to restore terminal settings under certain circumstances, echo -ne \E[0m may be a better choice.

13.3. Use the following template for writing colored text on a colored background.

$echo -e '\E[COLOR1;COLOR2mSome text goes here.'
The "\E[" begins the escape sequence. The semicolon-separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color, according to the table below


Table : Numbers representing colors in Escape Sequences
ColorForegroundBackground
black3040
red3141
green3242
yellow3343
blue3444
magenta3545
cyan3646
white3747

14. Bash, version 3

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
echo {e..m}     #  e f g h i j k l m
echo {z..a}      #  z y x w v u t s r q p o n m l k j i h g f e d c b a
                      #  Works backwards, too.
echo {25..30}  #  25 26 27 28 29 30
echo {3..-2}    #  3 2 1 0 -1 -2
echo {X..d}     #  X Y Z [  ] ^ _ ` a b c d

1








No comments:

Post a Comment