Daily shell scripting Exercises (01)

Source: Internet
Author: User
Tags disk usage

0. Introduction

Prepare to open a new pit and update a shell practice every day. Because shell scripts belong to the kind of skills that are unfamiliar or even forgotten when you're not practicing, I can only keep on practicing in order to resist the drag of forgetting.

1. First day Practice topics

Topic
Please generate a daily file according to the date format (XXXX-XX-XX), for example, the file generated today is 2018-04-08.log, and the disk usage is written to this file, regardless of cron, just write the script.

Analysis
1th, to understand the use of the Linux command date, print "xxxx-xx-xx" such a time format with date +%f or date +%y-%m-%d;
2nd, the command to check disk usage is: df-h.

2. Specific scripts

With the two-point analysis above, it's easy to write a script.

#!/bin/bashname=`date  %F`logfile=$name.logdf   -h  > $logfile

"Script Analysis"
Assign the date of the day to the variable name, and then define the log file name logfile.
The ">" symbol has the ability to redirect output in a shell script. It is special, you can write the output to the left of the symbol to the file to the right of the symbol.

3. Expand your knowledge
    The
    1. Shell can represent the result of a command, usually assigning a value to a variable, such as the following command:

        # n= ' Wc-l/etc/passwd |  awk ' {print '} ' # echo $n  
    2. Date also has many uses, for example:

       date +%h # hours 21date +%m # minutes 48date +%s # seconds 48date +%t # time 21:49:04date%w # Week 0 # from 0 Start, representing Sunday date-d "-1 day" +%f # means 1 days ago,-D for arithmetic operations 2018-04-07date-d "-1 Hour" ' +%t ' # means 1 hours ago 20:50:01  /pre> 
    3. ">" For proper redirection, when we run a command, we have the correct information output, and there may be incorrect information output. In contrast to the >, there is a 2> error redirection symbol, which is responsible for outputting the command's error return information to the specified file. For example:
[[email protected] work]# ls /etc/nofilels: cannot access /etc/nofile: No such file or directory上面的这条就是错误的提示信息。ls   /etc/passwd   /etc/nofile  > /tmp/log   2> /tmp/errorlog[[email protected] work]# cat /tmp/log/etc/passwd[[email protected] work]# cat /tmp/errorlog ls: cannot access /etc/nofile: No such file or directory
4. Summary

Today's script, review the use of anti-quotes, the date command and DF This command to view disk usage. These commands have also been extended to learn.

Daily shell scripting Exercises (01)

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.