4. Several examples of common shell scripts
4.0. When writing scripts (also for programming), it is best to write well-developed annotations
4.1.kill_processes.sh (a script that kills the process)
#!/bin/bashcurrent_pid=$ $ps-aux | grep "/USR/SBIN/HTTPD" | Grep-v "grep" | awk ' {print $} ' >/tmp/${current_pid}.txtfor PID in ' cat/tmp/${current_pid}.txt ' do{ echo "kill-9 $pid" Kill -9 $pid}9donerm-f/tmp/${current_pid}.txt
4.2.cpdir.sh
#!/bin/bash# This script is used to copy all subdirectories under the source directory to the destination directory, not to copy files from the source directory, to ensure that subdirectories in the destination directory are empty directory # script usage function usage () {echo "cpdir.sh Source directory destination Directory"} #判断是否为两个参数 , otherwise prompt for script usage if[$#-ne 2]then{usage exit}fisrcdir=$1desdir=$2# determine if source directory ${srcdir} is a directory, otherwise prompt for error message and usage if [!-D $srcdir]then{ Usage echo "error: Source directory ${srcdir} is not a directory" exit}fi# determine the destination directory ${srcdir} is a directory, otherwise prompt error message and use IF [!-D $desdir]then{usage EC Ho "Error: Destination directory ${desdir} is not directory" exit}fiprocessid=$$; #查找源目录下所有的子目录, output and save to/tmp/srcdir_ process number. txt file, echo "source directory ${srcdir} all subdirectories "Echo"------------------------------"Find $srcdir/*-type D | /usr/bin/tee/tmp/srcdir_tmp_${processid}.txtsed "s/^${srcdir}/${desdir}/g"/tmp/srcdir_tmp_${processid}.txt >/ tmp/srcdir_${processid}.txt# set up the empty directory under the destination directory RM-RF ${desdir}/*for subdir in ' cat/tmp/srcdir_${processid}.txt ' do{mkdir ${ Subdir}}doneecho "echo" target directory ${desdir} All subdirectories "Find $desdir/*-type D | /usr/bin/tee/tmp/desdir_${processid}.txt# diff echo "echo"--------------------------"diff/tmp/after creating an empty directory under the destination directory Desdir_${processid}.txt/tmp/srcdir_${proceSsid}.txtrm-f/tmp/srcdir_${processid}.txtrm-f/tmp/desdir_${processid}.txtrm-f/tmp/srcdir_tmp_${processid}.txt
4.3. My doubts
<<mayday
Various information
Mayday
What does the shell code above mean?
Linux Shell Advanced Programming Tips 4----Several examples of common Shell scripts