Starting from a simple script

Source: Internet
Author: User

Clear from a simple script: clear the log file 1 in/var/log # clear 2 # Use the root identity to run this script. 3 4 cd/var/log 5 cat/dev/null> messages 6 cat/dev/null> wtmp 7 echo "Logs cleaned up. "This is nothing unusual at all, but it is just the accumulation of commands to make it easier to input commands one by one from the console or xterm. the advantage is to put all commands in a script, so you don't need to repeat them every time. in this way, this script becomes a tool. For specific applications, this script can be easily modified or customized. but here I decided to spend more time talking about this/dev/null: in Linux,/dev/null and/dev/zero are two similar but special files, these two files are often used during shell script development and system O & M. For inux System Engineers, you must understand the differences and usage of these two files. I. Usage of/dev/null: we can regard the/dev/null file as a "black hole", which is very equivalent to a write-only file, all content written to the/dev/null file will be lost, but nothing can be read from the/dev/null file. However, thanks to these features, the/dev/null file is very useful in shell script development and command line maintenance. 1. disable the standard output. For example, if cat is used to view the $ filename file, the echo information is blank. # cat $ filename>/dev/null 2. standard forbidden error. For example, if the $ badname file does not exist when rm deletes a file, the following method filters the echo error message # rm $ badname 2>/dev/null 3. disable Standard output and standard error output. For example, view the $ filename file in cat # cat $ filename 2>/dev/null # If "$ filename" does not exist, no error message will be prompted. # If "$ filename" exists, the file content will not be printed to the standard output. # therefore, the above Code will not output any information at all. 4. clear the log file content such as: # cat/dev/null>/var/log/messages #:>/var/log/messages has the same effect, but no new process is generated. (Because: Yes Built-in) # cat/dev/null>/var/log/wtmp 5. in special usage of/dev/null, logs printed by the program are no longer recorded, so that the system space is not occupied by unnecessary logs. For example, link the log file of the cookie to/dev/null, the content written to this file will be discarded # ln-s/dev/null ~ /. Netscape/cookies 2. Usage of/dev/zero:/dev/zero is the same as/dev/null. It is also a pseudo file, however,/dev/zero can generate continuous null streams (Binary zero streams instead of ASCII streams), and the output of writing/dev/zero will be lost, it is also difficult to read a series of null values from/dev/zero, although this can also be done through dd Or a hexadecimal editor, /dev/zero is mainly used to create an empty file with a specified length for initialization. It is usually used together with the dd command. 1. use/dev/zero to create a temporary file of the specified size. For example, use the dd command to create a 1024 * BIT file/swap, of course, the file size can be adjusted through the bs and count parameters # dd if =/dev/zero of =/swap bs = 1024 count = 1000 2. use/dev/zero to fill zero in a specified size file to use some special requirements, for example, to fill the content of the RAM device with zero, to achieve the purpose of formatting RAM # dd if =/dev/zero of = $ DEVICE count = $ SIZE bs = $ BLOCKSIZE In summary, the differences and usage of/dev/null and/dev/zero files are as follows:/dev/null files are empty devices, also known as bit buckets ), it is mainly used for "writing", and any output written to it will be discarded. If you do not want to display messages in standard output or write files, you can redirect messages to/dev/null. Therefore, any data input to/dev/null is not supported! The/dev/zero file is mainly used as a standard "0" input device. It can provide 0 infinitely, and you can use/dev/zero to initialize the file. Clear: An Improved cleanup script #! /Bin/bashLOG_DIR =/var/logcd $ LOG_DIRcat/dev/null> messagescat/dev/null> wtmpecho "Logs cleaned up. "exit # This command is a correct and appropriate method to exit the script. now, let's take a look at a script of the true meaning. and we can go further... the preceding command is summarized into a bash file and processed. It is also known as "batch processing" to clear: an enhanced and generalized script for deleting logfile #! /Bin/bashLOG_DIR =/var/logROOT_UID = 0 LINES = 10E_XCD = 66E_NOTROOT = 67 if ["$ UID"-ne "$ ROOT_UID"] thenecho "Must be root ro run this script! "Exit $ E_NOTROOTfi # if [-n" $1 "] # then # lines = $1 # else # lines = $ LINES # fi E_WORNGPARAM = 65 case" $1 "in" ") lines = 10 ;;*[! 0-9] *) echo "Usage: wong param! "; Exit $ E_WRONGPARAM; *) lines = $1; esac # cd $ LOG_DIR # if [" $ PWD "! = "$ LOG_DIR"] # then # echo "Can't change to $ LOG_DIR" # exit $ E_XCD # fi cd/var/log | {echo "Can't change necessary directory. "> & 2 exit $ E_XCD} tail-$ lines messages> mesg. tmpmv mesg. tmp messages cat/dev/null> messagescat/dev/null> wtmpecho "Logs cleaned up! "Exit 0 because you may want to delete all system logs, this version leaves the last part of the log message. you will constantly find new methods to improve the script and improve the efficiency. note that sha-bang (#!) is used at the beginning of each script (#!), This means that you need to specify an interpreter for executing this file in your system .#! It is actually a 2-byte magic number, which specifies a special mark of the file type. In other words, in this case, it refers to an executable script (type man magic to get more details about this fascinating topic ). after sha-bang, a path name is followed. this path is the path where the interpreter of commands in the script is located. It may be a shell, a program language, or a command program in the Toolkit. this interpreter starts from the beginning and executes the commands in the script (starting from the line below sha-bang), ignoring the comments. it is emphasized that the first sha-bang in the file is meaningful and must start with it. If sha-bang appears again, it will not be parsed as sha-bang. The example is as follows :#! /Bin/bashecho "Part 1 of script." a = 1 #! /Bin/bash # This will not start a new script. echo "Part 2 of script." echo $ a 1 #! /Bin/sh 2 #! /Bin/bash 3 #! /Usr/bin/perl 4 #! /Usr/bin/tcl 5 #! /Bin/sed-f 6 #! /Usr/awk-f specifies a different command interpreter for each line in the script header above. If it is/bin/sh, the default shell (bash is used by default in Linux); otherwise, it is another interpreter. example :#! /Bin/rm # self-deletion script. as the rm command, you can directly delete yourself. The more command displays your content # When you run this script, nothing will happen... of course this file disappears. WHATEVER = 65 echo "This line will never print (betcha !). "Exit $ WHATEVER # It doesn't matter. The script won't exit here. Use #! /Bin/sh, because most commercial UNIX systems use the Bourne shell as the default shell, so that scripts can be transplanted to non-Linux machines, although this would sacrifice some of Bash's unique features. however, the script will be consistent with the POSIX sh standard. note that the path name given after "sha-bang" must be correct, otherwise, an error message -- usually "Command not found" -- is the only result you get when running this script. of course #! It can also be ignored. However, in this way, your script file can only be a set of commands and cannot use commands built in shell. The second example above must be #! It starts with "lines = 50" because the variable is allocated. This uses a shell-specific usage. remind you again #! /Bin/sh will call the default shell interpreter, Which is/bin/bash by default on Linux machines. the above script is analyzed as follows: 1. define variable 2. check whether the script has the root permission. There are two methods: easy to understand and more professional. 3. check the input parameters in two ways: easy to understand and more professional. 4. check whether to enter the/var/log directory. There are two methods: easy to understand and more professional. 5. execute the cleanup action and finally encourage you to write scripts in a modular way. pay more attention to collecting some representative "template" code, which may be used in future scripts. finally, you can generate a well-scalable library of routines. the following script is used as an example to test whether the number of parameters called by the script is correct. in most cases, you need to write a script to execute a specific task. In this chapter, the first script is an example, and then you will modify it to complete a different one, but similar tasks. it is a good habit to use variables to replace the constants of hard-coded (hard-wired). It is also a good habit to put repeated code into a function.

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.