Shell common code of X Pishu

Source: Internet
Author: User
Write a die function in the directory Mind Map Sequence

You must use the root account to execute this script and run the command line to pass the parameter to kill the local literacy summary Mind Map of the specified process.

Accumulate some common methods over the past few days for future use. This is only a basic application. If you are an expert in this field, please kindly advise. Write a die Function

 #! /Bin/ bashdie () {  echo  "   error: $1. aborting   "  exit   1  }  mkdir -p'  dirname  "  /etc/zhoubc/test. sh   " '| die "   cocould not create directory  "   echo  "   OK  "   exit   0  

Check its running status

This script must be run with the root account
 
#! /Bin/BashIf['Whoami'! ="Root"];ThenEcho "You must run this script as root. Sorry!"Exit1FiExit0

Check its running status

 

There is also a way to write (I prefer the above method ):

 
#! /Bin/BashIf["$ Uid"-Ne0];ThenEcho "You must run this script as root. Sorry! Your uid: $ uid root_uid: 0"Exit1FiExit0

Check its running status

 

Command line parameter passing
#! /Bin/ Bash  Case $ 1   In      "  Start  "  )  Echo   "  You input the commond: Start  " ;;  "  Stop  "  )  Echo   "  You input the commond: Stop  "  ;;  "  Restart  "  )  Echo   " You input the commond: restart  "  ;; * )  Echo   "  $0 {START | stop | restart}  "  ;;  Esac  Exit  0 

Check its running status

 

Kill a specified process
#! /Bin/Bash # Read the idcode PID of the process to kill the process =' PS Aux | Grep $ 1 | Grep -V Grep | Awk   '  {Print $2}  '  '# Kill  If [ "  $ PID  " ! ="" ]; Then      Kill - 9 $ PID &>/dev/ Null  Fi  Exit  0 

Check its running status

 

There is also a way to kill Processes

 
Pkill-9-F $1

 

 

Special local literacy file/dev/null (bucket): Data transmitted to this file will be lost by the system. -- It is useful when you do not need to output Program When writing data to this file, it is deemed that it has successfully completed the Data Writing operation, but nothing has actually been done.

 
#! /Bin/BashEcho "No output">/Dev/NullEcho "Output"Exit0

Check its running status

 

Capture exit status (with $? Capture): 0 indicates "successful", and any other exit status indicates a failure.

 
#! /Bin/BashLs-L/dev/NullEcho$?LsNofile # ls a non-existent fileEcho$?Exit0

Check its running status

File descriptor: Unix uses a small integer to indicate the files opened by each process. (Note the space)

File descriptor Meaning
0 Standard Input
1 Standard output
2 Standard Error output

 

 

 

 

Let's see what these files have output?

 

Summarized and summarized to facilitate next use. -- Send a sentence, without accumulating any points, so that there are thousands of miles. Not mentioned, I have to learn several commands: awk, test, sort, sed. -- These commands are very useful in practical applications. I have not learned these commands. Recommendation

 

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.