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
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 sed, ed, and ex support escaped versions of the extended Regular Expressions described above
6. I/O Redirection
Each open file gets assigned a file descriptor.The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively.
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
The tput sgr0 restores the terminal settings to normal. Omitting this lets all subsequent output from that particular terminal remain blue.
13.3. Use the following template for writing colored text on a colored background.
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 ExpressionsThe 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 sed, ed, and ex support escaped versions of the extended Regular Expressions described above
5.2. Globbing
A 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 stdin, stdout, 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/nullcat $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-nameTable: Bash options
Abbreviation Name Effect -B brace expansion Enable brace expansion (default setting = on) +B brace expansion Disable brace expansion -C noclobber Prevent overwriting of files by redirection (may be overridden by >|) -D (none) List double-quoted strings prefixed by $, but do not execute commands in script -a allexport Export all defined variables -b notify Notify when jobs running in background terminate (not of much use in a script) -c ... (none) Read commands from ... checkjobs Informs user of any open jobs upon shell exit. Introduced in version 4 of Bash, and still "experimental." Usage: shopt -s checkjobs (Caution: may hang!) -e errexit Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs) -f noglob Filename expansion (globbing) disabled globstar globbing star-match Enables the ** globbing operator (version 4+ of Bash). Usage: shopt -s globstar -i interactive Script runs in interactive mode -n noexec Read commands in script, but do not execute them (syntax check) -o Option-Name (none) Invoke the Option-Name option -o posix POSIX Change the behavior of Bash, or invoked script, to conform to POSIX standard. -o pipefail pipe failure Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value. -p privileged Script runs as "suid" (caution!) -r restricted Script runs in restricted mode (see Chapter 22). -s stdin Read commands from stdin -t (none) Exit after first command -u nounset Attempt to use undefined variable outputs error message, and forces an exit -v verbose Print each command to stdout before executing it -x xtrace Similar 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.
With 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
It's usually advisable to set the bold attribute for light-colored foreground text. |
Since tput sgr0 fails to restore terminal settings under certain circumstances, echo -ne \E[0m may be a better choice. |
$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
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
Color | Foreground | Background |
---|---|---|
black | 30 | 40 |
red | 31 | 41 |
green | 32 | 42 |
yellow | 33 | 43 |
blue | 34 | 44 |
magenta | 35 | 45 |
cyan | 36 | 46 |
white | 37 | 47 |
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 zecho {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