#SHELL编程基础一 (Multiple method evaluation 1+2+: +100)
# #为什么要学好shell
Shell scripting language is an important tool to implement Linux system management and automation operations, and the core of Linux system and basic application software mostly involve the content of shell script.
Every qualified Linux system administrator or OPS engineer needs to be able to skillfully write the Shell scripting language and be able to read the shell script content that comes with the system and various types of software.
Only in this way can we improve the efficiency of operation and maintenance personnel, adapt to the increasingly complex work environment, reduce unnecessary work, thus laying a better foundation for personal career development.
#
The purpose of this article is to familiarize yourself with the syntax and application of the for statement, while statement, and until in shell programming.
# #shell实现
# # #1, for Loop
#!/bin/sh
Sum=0 <== defines a variable sum
For n in{1: 100};do <== variable name n values from 1 to 100
Let sum= $sum + $n <==let operation
Done
echo $sum <== loop ends
#
The value range of n can be replaced by other methods. such as: SEQ 100 (command to add anti-quote ")
Algorithms can also be used in many ways: (()), expr.
#
#!/bin/sh
J=0
For ((i=0;i<=100;i++));d o <== The first is the variable initialization, the second is the range of the variable, and the third is the variable self-increment or decrement
((J=j+i))
Done
Echo $j
#
# # #2, while loop
I=1
while ((i <=100));d o <== exits the loop when I does not meet the criteria.
((J=j+i))
((i++))
Done
Echo $j
#
# # #3, until cycle
I=1
Until ((i >100));d o <== exit the loop when I meet the conditions. is the opposite of while.
((J=j+i))
((i++))
Done
Echo $j
#
# #非shell脚本实现
We've learned some commands before and we can implement 1+2+. +100 sum, here's a look back
#
1, Echo {1..100}|tr "" + "|BC
Description: The TR substitution is implemented here by replacing the space with the +, then passing the pipe to the next
Bc:linux in the calculator, support integer calculation
#
2, Seq-s + 1 1 100|BC
Description: Seq-s the specified delimiter
1 1 100
The first 1 is the initial value, the second is the step value, and the third is the end value
#
Bo has just started to learn shell programming, the level is limited. There must be other algorithms, I hope you master more enlighten. Learning shell programming is about thinking, thinking, and methods. In addition to these also have to work hard, insist.
Shell Programming Foundation One (multiple method evaluation 1+2+. +100)