Shell programming loop statement and echo, IFS

Echo in brief

  • echo -n: means not to switch to output
  • echo -e: output escape character, output the escaped content to the screen
  • Commonly used escape characters
Escapes Description
\b Equivalent to (backspace), but the premise is that there are characters after
\b. \b means to delete the previous character, and \b\b means to delete the first two characters
\c No newline output. When there are no characters after \c, it is equivalent to echo -n.
But when there are still characters after \c, the characters after \c will not be output.
\n Line feed (and with the function of carriage return)
is outputted characters start a new line from \n
\f Line break, but the beginning of the new line after the line break is connected to the end of the previous line
\ v Same as \f
\t After being escaped, it means the illustration tab, that is, the horizontal tab
\r Move the cursor to the
beginning of the line, but do not wrap, which means that the characters after \r overwrite the characters of the same length before \r, but when there is no character after \r, the character before \r will not be overwritten
\ \ Means insert \ itself

loop statement

for loop statement

  • For loop operations are also collectively referred to as traversal operations
  • Read different variable values ​​to execute the same group of commands one by one
  • Logical emoticon
    Insert picture description here

Expression one

[root@localhost ~]# for a in {
    
    1..5}; do echo $a; done   
1
2
3
4
5
【使用for循环语句输出15并赋值给变量a】
[root@localhost ~]# for i in {
    
    1..5..2}
> do
> echo $i
> done
1
3
5
【使用for循环语句每次间隔2输出15并赋值给变量i】

Expression two

[root@localhost ~]# seq 1 3 10                         
1
4
7
10
【使用seq每次间隔3110开始赋值,seq仅仅执行数值的连续操作】
[root@localhost ~]# for a in $(seq 1 5)                 
> do
> echo "$(($a*2))"
> done
2
4
6
8
10
【提取seq连续操作值后赋值给变量a的值,并将此变量*2

Expression three (the iterative method of variable value)

[root@localhost ~]# for ((i=1;i<5;i++)) 
> do
> echo $i
> done
1
2
3
4
【使用for语句进行循环操作,且需赋值的变量1=i<5且每次i自加1[root@localhost ~]# for ((i=1;i<8;i+=3)); do echo $i; done
1
4
7
【使用for语句进行循环操作,且需赋值的变量1=i<8且每次+3进行赋值】

Expression four

[root@localhost ~]# for ((i=0; i<=8; i+=2))
> do
> echo "$[$i+$i+$i]"
> done
0
6
12
18
24
【使用for语句进行循环操作,且需赋值的变量1=i<8且每次+2进行赋值,echo输出的是变量i+变量i+变量i的值】

Add users in batches and the passwords are all 111222

[root@localhost ~]# vim name1.txt
[root@localhost ~]# vim dome.sh 

#!/bin/bash
q=$(cat name1.txt)
for i in $q
do
  useradd $i
  echo "$111222" | passwd --stdin $i          【通过管道符号把111222输出给pasww $1】
  #echo -e "111222\n111222\n" | passwd $i     【通过转义符-n的换行回车进行秘密的确认,从而实现变量i的密码输入】

done
[root@localhost ~]# chmod +x dome.sh 
[root@localhost ~]# ./dome.sh 
更改用户 qq 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 ww 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 ee 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 rr 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 tt 的密码 。
passwd:所有的身份验证令牌已经成功更新。

Batch check the connectivity of specified ip addresses

[root@localhost ~]# for i in 192.168.1.{
    
    0..5}; do ping -c 3 -i 0.5 -W 3 $i &> /dev/null && echo "$i 在线" || echo "$i 不在线";done
192.168.1.0 不在线
192.168.1.1 在线
192.168.1.2 不在线
192.168.1.3 不在线
192.168.1.4 在线
192.168.1.5 不在线
【使用for语句进行192.168.1.{
    
    0-5}网段的ping操作(共3个包,每次间隔0.5秒,3秒后超时)并赋值给i,并将变量i的值丢到黑洞文件。最终判断
条件是否成立,成立则输出变量i在线,不成立则输出变量i不在线】

while loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is established
  • After assigning the initial value, you need to add the iterative method yourself, otherwise you will fall into an infinite loop
  • Logical emoticon
    Insert picture description here
[root@localhost ~]# vim text1.sh

#!/bin/bash
 
i=0                                  【定义变量i=0while [ $i -le 4 ]                   【测试条件i小于等于4do                  
  echo $i                            【输出i的值】
  let i++                            【自定义1个迭代方式】
  【输出的值和迭代方式这两行如果放错位置,那么最终输出的结果也将不同】
done
[root@localhost ~]# chmod +x text1.sh 
[root@localhost ~]# ./text1.sh 
0
1
2
3
4

Example 1

  • Add users in bulk
    • The user name starts with qzqz and is numbered in numerical order
    • A total of 5 users have been added, and the initial passwords are all set to 111222
[root@localhost ~]# vim text2.sh 

#!/bin/bash

i=1
while [ $i -le 5 ]
do
  useradd qzqz$i
  echo "111222" | passwd --stdin qzqz$i &> /dev/null     【将执行完的提示丢进黑洞文件】
  i=$[$i+1]                                            【变量叠加方式为每次+1】

done
[root@localhost ~]# chmod +x text2.sh 
[root@localhost ~]# ./text2.sh 

Example 2

  • Randomly guess the fruit price
    • Get random number through variable RANDOM
    • Prompt the user to guess and record the number of guesses, when the user guesses correctly, exit the loop
    • The price range is 0-99
[root@localhost ~]# vim text3.sh
[root@localhost ~]# chmod +x text3.sh

[root@localhost ~]# vim text3.sh

#!/bin/bash
 
a=$[$RANDOM % 100]               【通过取余的方式将RANDOM随机值定义在0-99之间,RANDOM上限为99999】 
b=0
d=0
echo "猜一猜该水果的价格:"       
while [ $b -eq 0 ]               【当变量值b等于0时执行while语句操作】
do
  let d++                        【每执行一次语句操作次数+1,并赋予变量名d,以便最终输出共猜测了几次】
  read -p "输入你猜测的价格:" c     【提示用户输入猜测的价格并赋予变量c】
if [ $c -eq $a ];then            【如果用户输入的变量c等于RANDOM随机出来的数字】
  echo "你猜对了!"                
  b=1                            【因为变量b等于0时才执行while语句,所以这里b=1用于跳出while循环】
elif [ $c -gt $a ];then
  echo "你猜多了!"
elif [ $c -lt $a ];then
  echo "你猜少了!" 

fi

done
echo "你总共猜了 $d 次。" 
:wq
[root@localhost ~]# ./text3.sh 
猜一猜该水果的价格:
输入你猜测的价格:50
你猜少了!
输入你猜测的价格:65
你猜多了!
输入你猜测的价格:57
你猜少了!
输入你猜测的价格:63
你猜少了!
输入你猜测的价格:64
你猜对了!
你总共猜了 5 次。

untli loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is not established
  • Logical emoticon
    Insert picture description here
[root@localhost ~]# vim text5.sh

#!/bin/bash

a=0

until [ $a -gt 5 ]          【定义条件为a>5时untile语句不生效】
do
  echo $a       
  let a++


done
[root@localhost ~]# chmod +x text5.sh 
[root@localhost ~]# ./text5.sh 
0
1
2
3
4
5

Example

  • Calculate the sum of 1-35 by cyclic accumulation
[root@localhost ~]# vim text5.sh

#!/bin/bash

a=0
sum=0
until [ $a -gt 35 ]          【定义条件为a>35时untile语句不生效】
do
  sum=$[$sum + $a]  
  let a++


done
echo $sum
:wq
[root@localhost ~]# ./text5.sh 
630
            

break

  • break can jump out of a single loop
[root@localhost ~]# vim text6.sh

#!/bin/bash

for ((b=1;b<=3;b++))
do
  echo "外层循环为$b"

  for ((c=1;c<=3;c++))
  do
  echo "内存循环为$c"

  done

done
[root@localhost ~]# chmod +x text6.sh 
[root@localhost ~]# ./text6.sh 
外层循环为1
内存循环为1
内存循环为2
内存循环为3
外层循环为2
内存循环为1
内存循环为2
内存循环为3
外层循环为3
内存循环为1
内存循环为2
内存循环为3
[root@localhost ~]# vim text6.sh

#!/bin/bash

for ((b=1;b<=3;b++))
do
  echo "外层循环为$b"

  for ((c=1;c<=3;c++))
  do
  echo "内存循环为$c"

  if [  $c -eq 2 ];then    【当变量c等于2时执行break操作,如果break 2则代表跳出2层循环】
    break
  fi
  done

done
[root@localhost ~]# ./text6.sh 
外层循环为1
内存循环为1
内存循环为2
外层循环为2
内存循环为1
内存循环为2
外层循环为3
内存循环为1
内存循环为2

continue

  • continue to abort a command in a loop, but not completely abort the entire command
[root@localhost ~]# vim text6.sh

#!/bin/bash

for ((b=1;b<=5;b++))
do
  echo "外层循环为$b"

  if [ $b -gt 2 -a $b -lt 4 ];then  【当变量值b大于2且小于4时执行continue操作】
     continue
  fi

  echo $[$b *2] 

done
[root@localhost ~]# ./text6.sh 
外层循环为1
2
外层循环为2
4
外层循环为3     【因为当执行到3时满足变量值b大于2且小于4的条件,所以执行continue操作,中止当前一次循环命令】
外层循环为4
8
外层循环为5
10

IFS field separator

[root@localhost ~]# set | grep IFS
IFS=$' \t\n'                         【默认包含“空格,制表符,换行符”】
......
    local IFS='
[root@localhost ~]# oldifs=$IFS      【修改前先赋值给oldifs。以便使用完后好修改回初始值】
[root@localhost ~]# IFS=$'\n'        【修改成只换行】
[root@localhost ~]# set | grep IFS
IFS=$'\n'
    local option option2 i IFS=' 	
   ......
    local IFS='
[root@localhost ~]# IFS=$oldifs      【修改回初始值】
[root@localhost ~]# set | grep IFS
IFS=$' \t\n'
    ......
    local IFS='

Guess you like

Origin blog.csdn.net/TaKe___Easy/article/details/114382332