How to write a shell script to not bullying?

Source: Internet
Author: User

1. A SHELL script that does not log logs is bullying!
We often encounter a problem in our work, what exactly does a Shell script do, when it starts executing, and when it ends. Especially database backup, we would like to know our MySQL database backup time. So it's important to add a log to the script. Then our script should have a log function dedicated to logging:

#!/bin/bashshell_name= "$" shell_log= "/var/log/$SHELL _name.log" Shell_log () {log_info=$1 echo "' Date ' +%h:%m:%s ': $S Hell_name $log _info ">> $SHELL _log}shell_log" script start excupt ... "Sleep 3shell_log" SHELL success ..... "# above the script I wrote a log function shell_log, every time logging, we directly execute Shell_log log content as the first parameter to it can be, quickly try. [Email protected]_server/mnt]# cat/var/log/mysql.sh.log06:18:33:mysql.sh script start Excupt ... 06:18:36:mysql.sh Shell success ....

2. Shell scripts that can be executed directly are easy to bullying?
A script can be executed directly? Isn't it straightforward to execute? Imagine that you have temporarily written a particularly important script, the dry things can be destructive, accidentally by others. And most of the time we have a script that can have multiple functions, so we need to let the user choose to do it.

#!/bin/bash# shell envshell_name= "shell_template.sh" shell_dir= "/opt/shell" SHELL_LOG= "${SHELL_DIR}/${ Shell_name}.log "lock_file="/tmp/${shell_name}.lock "#Write  log shell_log () {     log_info=$1    echo  "$ (date " +%y-%m-%d ")  $ (date " +%H-%M-%S ")  :  ${shell_name} : ${log_info} " >> ${shell_log}}# shell usageshell_ Usage () {    echo $ "usage: $0 {backup}"}# backup mysql all  database with mysqldump or innobackupexmysql_backup () {    shell_ log  "Mysql backup start"     shell_log  "Mysql backup stop"}#  main functionmain () {    case $1 in         backup)             mysql_backup             ;;         *)              SHELL_USAGE;    ESAC} #Execmain  $1#  The above script we have written the Shell_usage function to tell the user, How this script is used. At the same time, I would like to emphasize that we are often process-oriented, such as writing shell, , and it is recommended to function in such a way that the script is clearly readable. Let's do the following to see the effect. [[Email protected]_server /mnt]# sh mysql.shusage: mysql.sh {backup}

For a functional script, there seems to be something missing. Yes, that's a comment! We have to say what this script is for. Or after you leave, others see this script, I wipe, so the cow broke the script who wrote it? Don't be afraid, write your name.

################################### $Name: shell_template.sh# $Version: v1.0# $Function: Backup MySQL Dat Abases Template script# $Author: Jason zhao# $organization: https://www.unixhot.com/# $Create date:2016-08-27# $De Scription:you Know what I mean,hehe###################################

Of course there are a lot of scripting skills, not one by one description, but if you can master the above three skills, immediately feel the script written a bit tall, there are wood?

This article is from the "Stranded," blog, please be sure to keep this source http://yasar.blog.51cto.com/9120455/1880949

How to write a shell script to not bullying?

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.