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
-
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
-
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>
- ">" 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)