Linux Shell Advanced Programming Tips 4----Several examples of common Shell scripts

Source: Internet
Author: User
Tags diff

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.