Thinking of comparing the shell to a script like Python and Java, I gave up when I had the idea. This is too insulting to the shell. (Crying smiley!) )
As a programmer, Linux is the most basic requirement. and the shell script will sometimes show its unique charm in Linux, let's learn it together!!!!
I love learning!!
Case One
Print 99 multiplication table
> Loop statement (for)
> variable calculation (subtraction)
Statement:
1 for in $ (seq9does for with $ (seqdoEcho "$i * $j =$ (($i * $j)) "done; echo"" Done
Results:
Description
>for Loop:for ARG in Range;do Cmd;done
Common Scenarios for loops:
# #1, limited numbers (separated by spaces)
For I in 1 2 3 4 5;do echo $i;d One
# #2, sequence data (seq start step last)---step default 1
For I in $ (SEQ 1 3 100); Do echo $i;d one
# #3, command result (default space is delimiter)
For i in ' cat 01.txt ';d o echo $i;d one
For i in ' ls | grep "Heh" ';d o echo $i;d one
# #4, Syntax loops (like C, note as double brackets, semicolon separated)
For ((i=1;i<3;i+=2));d o echo i;d one
for ((;;); do echo "Infinite loop";d one
# #5, other situations
Specific situation, specific analysis
> Arithmetic Operations
The arithmetic calculations involved in the shell are the same as in other languages (or in other languages, like the shell, the order is ignored by obsessive-compulsive patients), plus (+), minus (-), multiply (*), divide (/), modulo (\), take-up (%), exponentiation (* *).
There are several ways to implement operations in the shell.
Method one, bracket: [...] # #中括号完成算术执行, you need to print it by "$" reference
Echo $[2+3]
Method two, double parenthesis: ((...)) # #等同于let
echo $ ((3+2)) # #echo $ "Let 3+2"
Method Three, let, expr expressions
Let i= 3+2;echo $i;
echo $ (Expr $i * 3); (There are spaces between operators, otherwise all outputs are not performed) (expr has other advanced applications, not to mention)
Note:bash does not support floating-point type calculations, which can only be done with integer arithmetic . Floating-point operations require the use of Bc/awk
Echo 0.5*4-0.2|BC;
echo $ (awk ' begin{print 0.5*4-0.2} ');
Case Two
Cycle Print Date (you can specify a start date or a default date) and return the number of days
> Cycle
>if judgment
>date Date
1#!/bin/Bash2 3 # #判断变量, whether it is empty (if there is no or only one, make the default assignment)4 if[" $"=="" ] 5 Then6Start_date= 'Date-D"Today last month" "+%y%m01"`7End_date= 'Date-D"Today" "+%y%m%d"`8 Else9 if[" $"=="" ]Ten Then OneStart_date= 'Date-D" $" "+%y%m%d"` AEnd_date= 'Date-D"Today" "+%y%m%d"` - fi - fi the - # #判断两个变量是否有问题 (extensible for availability identification) - if["$start _date"-gt"$end _date" ] - Then + Echo "error! \nplease input a right date" - Exit + fi A at # #通过循环, return date value (contains start and end date, closed interval) - forIinch`seq 0 100000` - Do -T_date= 'Date-D"${start_date} +$ (($i + 1)) Day" "+%y%m%d"` - Echo$t _date -cnt_days=$i in - # #如果循环到当天, just quit. to if[$t _date = =$end _date] + Then - Break the fi * Done $ Panax Notoginseng Echo "The days between and the date is"+ $cnt _days+" !"
>if judgment
Format: if condition; then cmd; elif cmd; else cmd; Fi
Conditional formatting requirements: There is at least one space at both ends of the bracket, such as [* * *]; the middle of the judge also needs a space, otherwise the execution is not an error but not the result we want!
Common conditions:
Numerical judgment
Digital 1-EQ number 22 number equal to True (equal)
Digital 1-ne Number 22 number is true (not equal)
Digital 1-GT digit 2 digit 1 greater than the number 2 is true (great than)
Digital 1-ge Digit 2 number 1 is greater than or equal to the number 2 is true (great equal)
Digital 1-LT digit 2 digit 1 less than the number 2 is true (lesser than)
Digital 1-le Digit 2 number 1 is less than or equal to the number 2 is true (lesser equal)
File judgment
-e file exists (directory or normal file, exists) "Example: [-E./02.txt] && echo" hehe ", this is a simple version of If judgment, first execute the inner parenthesis command, true to execute && after the command; "
-F is a normal file"Example: [-F./02.txt] && echo" Heh ""
-D is a directory file (directory)example: [-D./111] && echo "heh" "
-r file Permissions: readable (read) "Example: [-R./111] && echo" Heh ""
-W File permissions: Writable (write) Example: [-W./111] && echo "Heh" "
-X File permissions: Executable (execute) Example: [-X./111] && echo "Heh" "
Character segment judgment
= = whether the same (test "=" can also) "[" hehe "= =" Xizao "] && echo" Good ""
! = does not equal "[" hehe "! = "Xizao"] && echo "Good" "
-Z is empty (whether the length is 0) "[-Z"] && echo "Good" "
Case Three
System Monitoring
The performance of the system does not matter for development, it is very important for operation and maintenance. The performance of the automated monitoring system is very important. We take the monitoring of single task CPU consumption as an example.
Requirements: Monitor the CPU consumption of the system more than 40% of the task (in real work, top will know)
> Task script: Find the corresponding task (main, share, conf, log, mail)
> Real-time monitoring (Shell)
Don't think well, how to write! Let's talk about it later!
Original blog, reproduced please specify the source! Welcome Email communication: [Email protected]
Shell instance Practice + detailed