一些脚本模板

直接执行命令和将命令作为字符串赋值给变量,然后再显示变量效果一样。

killppp="eval kill -9 `ps ax | awk '/ppp/ { print $1 }'`"
$killppp # 这个变量现在成为了一个命令.

cut -d ' ' -f2,3 filename等价于awk -F'[ ]' '{ print $2, $3 }' filename

output=$(sed -n /"$1"/p $file) # 命令替换.

if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]]


在一个脚本中, 使用后台运行命令(&)可能会使这个脚本挂起, 直到
敲ENTER, 挂起的脚本才会被恢复. 看起来只有在这个命令的结果需要输出
到stdout的时候, 这种现象才会出现. 这是个很烦人的现象.
1 #!/bin/bash
2 # test.sh 
3
4 ls -l &
5 echo "Done."
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 
test.sh
_
看起来只要在后台运行命令的后边加上一个wait命令就会解决这个问题.
1 #!/bin/bash
2 # test.sh 
3
4 ls -l &
5 echo "Done."
6 wait
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 
test.sh
如果将后台运行命令的输出重定向到文件中或/dev/null中, 也能解决这个
问题.




测试脚本参数数量是否正确

E_WRONG_ARGS=85 script_parameters="-a -h -m -z" # -a = all, -h = help, etc. if [ $# -ne $Number_of_expected_args ] then echo "Usage: `basename $0` $script_parameters" # `basename $0` is the script's filename. exit $E_WRONG_ARGS fi

Example 2-1. cleanup: A script to clean up log files in /var/log

# Cleanup
# Run as root, of course.

cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Log files cleaned up."

Example 2-2. cleanup: An improved clean-up script

#!/bin/bash
# Proper header for a Bash script.

# Cleanup, version 2

# Run as root, of course.
# Insert code here to print error message and exit if not root.

LOG_DIR=/var/log
# Variables are better than hard-coded values.
cd $LOG_DIR

cat /dev/null > messages
cat /dev/null > wtmp


echo "Logs cleaned up."

exit #  The right and proper method of "exiting" from a script.
     #  A bare "exit" (no parameter) returns the exit status
     #+ of the preceding command. 

Example 2-3. cleanup: An enhanced and generalized version of above scripts.

#!/bin/bash
# Cleanup, version 3

#  Warning:
#  -------
#  This script uses quite a number of features that will be explained
#+ later on.
#  By the time you've finished the first half of the book,
#+ there should be nothing mysterious about it.



LOG_DIR=/var/log
ROOT_UID=0     # Only users with $UID 0 have root privileges.
LINES=50       # Default number of lines saved.
E_XCD=86       # Can't change directory?
E_NOTROOT=87   # Non-root exit error.


# Run as root, of course.
if [ "$UID" -ne "$ROOT_UID" ]
then
  echo "Must be root to run this script."
  exit $E_NOTROOT
fi  

if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
  lines=$1
else  
  lines=$LINES # Default, if not specified on command-line.
fi  


#  Stephane Chazelas suggests the following,
#+ as a better way of checking command-line arguments,
#+ but this is still a bit advanced for this stage of the tutorial.
#
#    E_WRONGARGS=85  # Non-numerical argument (bad argument format).
#
#    case "$1" in
#    ""      ) lines=50;;
#    *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup";
#     exit $E_WRONGARGS;;
#    *       ) lines=$1;;
#    esac
#
#* Skip ahead to "Loops" chapter to decipher all this.


cd $LOG_DIR

if [ `pwd` != "$LOG_DIR" ]  # or   if [ "$PWD" != "$LOG_DIR" ]
                            # Not in /var/log?
then
  echo "Can't change to $LOG_DIR."
  exit $E_XCD
fi  # Doublecheck if in right directory before messing with log file.

# Far more efficient is:
#
# cd /var/log || {
#   echo "Cannot change to necessary directory." >&2
#   exit $E_XCD;
# }




tail -n $lines messages > mesg.temp # Save last section of message log file.
mv mesg.temp messages               # Rename it as system log file.


#  cat /dev/null > messages
#* No longer needed, as the above method is safer.

cat /dev/null > wtmp  #  ': > wtmp' and '> wtmp'  have the same effect.
echo "Log files cleaned up."
#  Note that there are other log files in /var/log not affected
#+ by this script.

exit 0
#  A zero return value from the script upon exit indicates success
#+ to the shell.
原文地址:https://www.cnblogs.com/fly-xiang-zhao/p/3679998.html