- 论坛徽章:
- 1
|
本帖最后由 houjun19830610 于 2014-07-31 06:40 编辑
2014/7/31
Part 1
1. We give the scripts executable permission via:
chmod u+rx <scriptname>
2. We name a bash script as scriptname.bash or scriptname.sh
3. To clean up logs, we can realize this function in three ways:
a. cat /dev/null > /var/log/messages
b. > /var/log/messages
c. echo " " > /var/log/messages
4. An enhanced and generalized version of bash script that is to clean up logs
=======================================================================================================
#!/bin/bash
# Clean up logs
LOG_DIR=/var/log # That is the log folder
ROOT_UID=0 # Only user with $UID 0 has root privilege
LINES=50 # Default number of lines saved
E_XCD=86 # Can't change directory?
E_NOTROOT=87 # Non-root exit error
# Run as root
if [ "$UID" -ne "$ROOT_UID" ] # If current UID doesn't equal to 0 (Means the current user is not root)
then
echo “Must be root to run the script.” # Only root can run this script
exit $E_NOTROOT # Exit with specified signal
fi
if [ -n "$1" ] # If the first parameter $1 is not empty, then execute "then" command.
then
lines=$1 # The first parameter means the last lines you want to keep
else
lines=$LINES # Default lines you want to keep, if not specified on command-line
fi
cd $LOG_DIR || {
echo "Can't change to necessary directory." 2> /dev/null # If destination path doesn't exist, exit with specified signal
exit $E_XCD;
}
tail -n $lines messages > mesg.tmp # Save last section of messages log file
mv mesg.tmp messages # To keep as new log file
cat /dev/null > wtmp # Clean up logging information
exit 0 # A zero return means that the script is executed successfully
=======================================================================================================
5. If you want to execute a bash script, please test it by ./scriptname with user execution privilege.
6. Preliminary exercise:
A. Write a script that upon invocation shows the time and date, lists all logged-in users, and gives the system uptime. The script saves this information to a log file.
Answer:
#!/bin/bash
echo "Today is `/bin/date`.”
echo "`/usr/bin/who | awk '{ print $1 }' | sort | uniq | xargs | tr " " ","` are logging.” # Get root,alex result. As xargs can re-arrange the result delimited by blank, we can use "tr" command to replace the blank with comma. And if we don't use "sort" command, "uniq" command doesn't make sense."uniq" command only works after the results get sorted.
if [ `uptime | awk '{ print $4 }' | cut -c1-3` == "min" ] # If min parameter exists, the result is minute. Otherwise, it is day.
then
echo "The system has run `/usr/bin/uptime | awk '{ print $3 }'` minutes."
else
echo "The system has run `/usr/bin/uptime | awk '{ print $3 }'` days."
fi
7. In vi / vim editor, try "esc" button and ":nohl" to cancel highlight.
8. In /etc/vimrc, add "syntax on" to highlight syntax.
9. # is used for comments. Lines beginning with a # (with the exception of #!) are comments and will not be executed. One # will not end until you use a new line. That means if you want to use a new command, please don't use the same line as the line of comments.
But the standard quoting and escape characters (' \) escape the #. (" can't escape #)
#!/bin/bash
echo 'The # here doesn’t begin a comment.'
echo The \# here doesn’t begin a comment.
10. “;”semicolon, permits putting two or more commands on the same line.
echo "File $filename exists." ; cp $filename $filename.bak # The first command runs and then the second runs accordingly.
“;”is also used for case option. (double semicolon)
#!/bin/bash
case “$var” in
abc) echo “\$var = abc” ;;
xyz) echo “\var = xyz” ;;
esac
11. #!/bin/bash
if [ -x “$filename” ] ; then # If $filename can be executed, the result is true. That means "then" command will be executed accordingly.
12. "dot" command is equivalent to source which is a bash builtin.
A. When working with filenames, a leading dot is the prefix of a "hidden" file, a file that an ls will not normally show. (ls -al can show.)
B. When considering directory names, a single dot represents the current wiring directory, and two dots denote the parent directory. The dot often appears as the destination directory of a file movement command (sometimes means current directory.)
For instance:
cp /home/alex/junk/* . # Copy specified files to current directory ($PWD)
C. "dot" character matches a single character. This function is used in regular expression.
13. comma operator links together a series of arithmetic operations. All are evaluated, but only the last one is returned.
For example,
#!/bin/bash
let "t2 = ((a=9, 15/3))"
echo $t2
# Set "a=9" and "t2 = 15 / 3”
For another instance,
#!/bin/bash
for file in /{,usr/}bin/*calc
# Find all files ending in "calc" in /bin and /usr/bin directories.
14. escape [backslash] \ , a quoting mechanism for single characters.
\X escapes the character X. The \ may be used to quote "and ', so they are expressed literally.
15. / , filename path separator like /home/alex/projects. This is also the division arithmetic operator.
16. ` backquotes , the command in the backquotes will be executed firstly.
For instance,
echo `expr 4 + 5`
17. : , this symbol is named as null command [colon], equivalent of a "NOP"(no op, do-nothing operation). It is considered as "true". Itself is a bash builtin and its exit status is true (0)
#!/bin/bash
:
echo $? # This is to get the exit status of last command. If the last command runs successfully, the result is 0. Otherwise, the result will be from 1 to 255.
Another example:
#!/bin/bash
while :
do
echo $PWD
done
# Same as
# while true
# do
# echo $PWD
# done
|
|