System administrators often write scripts to automate common tasks. Bash has become a de facto standard for shell scripting on most flavors of UNIX. Most of the principles this Tutorial covers apply equally well to scripting with other shells, such as the Korn Shell, from which Bash derives some of its features.
Creating a script to clean up the log files in /var/log
#!/bin/bash # Script Name: cleanup.sh # Run as root user. LOG_DIR=/var/log cd $LOG_DIR ls -ltr cat /dev/null > messages ls -ltr # -rw------- 1 root root 0 Feb 14 10:20 messages exit 0 |
# This number sign indicates that anything followed by # sign is commented and # will not be executed if [ -x "$filename" ]; then # Note the space after the semicolon. echo "The # here does not begin a comment." echo 'The # here does not begin a comment.' echo The \# here does not begin a comment. echo The # here begins a comment. echo ${PATH#*:} # Parameter substitution, not a comment. echo $(( 2#101011 )) # Base conversion, not a comment. |
# Command separator [semicolon]. Permits putting two or more commands on the same line Example: if [ -x "$filename" ]; then # Note the space after the semicolon. echo "File $filename exists."; cp $filename $filename.bak else echo "File $filename not found."; touch $filename fi; echo "File test complete." |
case "$variable" in abc) echo "\$variable = abc" ;; xyz) echo "\$variable = xyz" ;; esac |
A signle dot and two dots
When considering directory names, a single dot represents the current working directory, and two dots
denote the parent directory.
bash$ pwd /home/oracle/app bash$ cd . bash$ pwd /home/oracle/app bash$ cd .. bash$ pwd /home/oracle/ |
partial quoting [double quote]
"STRRING"
full quoting [single quote].
'STRING'. This is astronger form of quoting than "STRING"
comma operator.
Comma operator links together arithmetic operations. All evaluated but only the last one is returned.
for file in /{,usr/}bin/*sum # Find all executable files ending in "sum" #+ in /bin and /usr/bin directories. do if [ -x "$file" ] then echo $file fi done |
"\" escape [backslash].A quoting mechanism for single characters. \M 'M' char will be scaped.
"/" This forward slas is used for file path separator i.e. /home/oracle/app.
"`" command substitution. The `command` substitution makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.
":" colon or null command which is shell equivalent of a NOP (no op, do-no-operation).
The ":" command is itself a Bash builtin, and its exit status is true (0).
:
echo $? # 0
while true do ... doneor
################################ if condition then : # Do nothing and branch ahead else # Or else ... take-some-action fi ################################ : > test.log # File "test.log" now empty. # Same effect as cat /dev/null > test.log # However, this does not fork a new process, since ":" is a builtin. #########################################################################
"!" is what we call "bang" operator. The ! operator inverts the exit status of the command to which it is applied. This also refer to NOT EQUAL as in "!="
"*" 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. The * also represents any number (or zero) characters in a regular expression.In the context of arithmetic operations, the * denotes multiplication
ls -ltr|grep control.*
"?" test operator. =================== Within certain expressions, the ? indicates a test for a condition. In a parameter substitution expression, the ? tests whether a variable has been set. wild card. The ? character used as a single-character search "wild card" for filename expansion in globbing. "$" Dollar sign (variable substitution). ========================================= It is used to substitute variable as in the following example:
No comments:
Post a Comment