五、流程控制之循环

目录

5.1 步进循环语句for

5.1.1 带列表的for循环语句

5.1.2 不带列表的for循环语句

5.1.3 类C风格的for循环语句

5.2 while循环语句

5.2.1 while循环读取文件

5.2.2 while循环语句示例

5.3 until循环语句

5.4 select循环语句

5.5 嵌套循环


5.1 步进循环语句for

for循环是最简单,也是最常用的循环语句。与其他的程序设计语言一样,for循环都是初学者在学习循环结构时的入门课程。for循环通常用于遍历整个对象或者数字列表。按照循环条件的不同,for循环语句可以分为带列表的for循环、不带列表的for循环以及类C风格的for循环。

5.1.1 带列表的for循环语句

带列表的for循环通常用于将一组语句执行已知的次数,其基本语法如下:

for variable in list
do
 statement1
 statement2
 ...
done

在上面的语法中,variable称为循环变量,list是一个列表,可以是一系列的数字或者字符串,元素之间使用空格隔开。do和done之间的所有的语句称为循环体,即循环结构中重复执行的语句。for循环体的执行次数与list中元素的个数有关。在带列表的for语句执行时,Shell会将in关键字后面的list列表的第1个元素的值赋给变量variable,然后执行循环体;当循环体中的语句执行完毕之后,Shell会将列表中的第2个元素的值赋给变量variable,然后再次执行循环体。当list列表中的所有的元素都被访问后,for循环结构终止,程序将继续执行done语句后面的其他的语句。

示例1:直接列出变量列表所有元素

方法1:直接列出元素方法
[root@kittod ~]# cat list.sh
#!/bin/bash

for IP in 192.168.132.128 192.168.132.129
do
    echo $IP
done

[root@kittod ~]# chmod +rx list.sh
[root@kittod ~]# ./list.sh
192.168.132.128
192.168.132.129


方法2:使用大括号
[root@kittod ~]# cat list2.sh
#!/bin/bash

for IP in 192.168.132.12{8..9}
do
    echo $IP
done
[root@kittod ~]# chmod a+rx list2.sh
[root@kittod ~]# ./list2.sh
192.168.132.128
192.168.132.129


方法3:使用seq
[root@kittod ~]# cat list3.sh
#!/bin/bash

for IP in $(seq -f "192.168.132.12%1g" 1 5)
do
    echo $IP
done
[root@kittod ~]# chmod a+rx list3.sh
[root@kittod ~]# ./list3.sh
192.168.132.121
192.168.132.122
192.168.132.123
192.168.132.124
192.168.132.125

说明:seq的用法如下
[root@kittod ~]# seq 1 5
1
2
3
4
5
[root@kittod ~]# seq 1 2 10
1
3
5
7
9

选项:
-s 指定输出的分隔符,默认为\n,即默认为回车换行

[root@kittod ~]# seq -s + 1 10 
1+2+3+4+5+6+7+8+9+10

[root@kittod ~]# seq -s "`echo -e "\t"`" 9 11   #echo -e启用转译字符
9 	10 	11

-w 指定为定宽输出,不能和-f一起用
[root@kittod ~]# seq -w 8 11
08
09
10
11
[root@kittod ~]# seq -w 99 103
099
100
101
102
103

-f 按照指定的格式输出,不能和-w一起使用,在没有使用-f选项指定格式时,默认格式为%g,类似于printf
[root@kittod ~]# seq 8 11
8
9
10
11
[root@kittod ~]# seq -f '%g' 8 11
8
9
10
11
[root@kittod ~]# seq -f '%3g' 8 11
8
9
10
11
[root@kittod ~]# seq -f '%03g' 8 11
008
009
010
011
[root@kittod ~]# seq -f "dir%g" 1 5
dir1
dir2
dir3
dir4
dir5
[root@kittod ~]# mkdir $(seq -f 'dir%03g' 1 10)
[root@kittod ~]# ll
total 10
drwxr-xr-x. 2 root root 6 May 4 15:59 dir001
drwxr-xr-x. 2 root root 6 May 4 15:59 dir002
drwxr-xr-x. 2 root root 6 May 4 15:59 dir003
drwxr-xr-x. 2 root root 6 May 4 15:59 dir004
drwxr-xr-x. 2 root root 6 May 4 15:59 dir005
drwxr-xr-x. 2 root root 6 May 4 15:59 dir006
drwxr-xr-x. 2 root root 6 May 4 15:59 dir007
drwxr-xr-x. 2 root root 6 May 4 15:59 dir008
drwxr-xr-x. 2 root root 6 May 4 15:59 dir009
drwxr-xr-x. 2 root root 6 May 4 15:59 dir010

示例2:获取当前目录下的文件名作为变量列表打印输出

[root@kittod ~]# cat list4.sh
#!/bin/bash
for FILE in $(ls -F | grep -v /$) #ls -F 表明查看一个文件,/$表示以/结尾
do
    echo $FILE
done
[root@kittod ~]# chmod a+rx list4.sh
[root@kittod ~]# ./list4.sh
list2.sh
list3.sh
list4.sh
list.sh
说明:
[root@kittod ~]# ls -F / | grep -v /$
bin@
lib@
lib64@
sbin@

示例3:打印出下面语句中字符数不大于6的单词

rabbit is favorite to eat cabbage

[root@kittod ~]# cat 3.sh
#!/bin/bash
for n in rabbit is favorite to eat cabbage
do
    if [ `expr length $n` -le 6 ];then
        echo $n
    fi
done
[root@kittod ~]# chmod a+rx 3.sh
[root@kittod ~]# ./3.sh
rabbit
is
to
eat

Shell允许用户指定for语句的步长。当用户需要另外指定步长时,其基本语法如下:

for varibale in {start..end..step}
do
 statement1
 statement2
 ...
done

示例:通过for循环,计算100以内奇数的和

[root@kittod ~]# cat 4.sh
#!/bin/bash
sum=0
for i in {1..100..2}
do
    let "sum+=i"
done
    echo "the sum is $sum"
    
[root@kittod ~]# chmod a+rx 4.sh
[root@kittod ~]# ./4.sh
the sum is 2500


let用法:
应用变量时不需要使用$
[root@kittod ~]# no1=2
[root@kittod ~]# no2=4
[root@kittod ~]# let result=no1+no2
[root@kittod ~]# echo $result
6

自加:
[root@kittod ~]# let no1++
[root@kittod ~]# echo $no1
3

自减:
[root@kittod ~]# let no1--
[root@kittod ~]# echo $no1
2

[root@kittod ~]# a=2
[root@kittod ~]# let "a+=1"
[root@kittod ~]# echo $a
3
[root@kittod ~]# let "a-=1"
[root@kittod ~]# echo $a
2
[root@kittod ~]# let "a*=2"
[root@kittod ~]# echo $a
4
[root@kittod ~]# let "a/=2"
[root@kittod ~]# echo $a
2
[root@kittod ~]# let "a=$a**3"
[root@kittod ~]# echo $a
8

5.1.2 不带列表的for循环语句

在某些特殊情况下,for循环的条件列表可以完全省略,称为不带列表的for循环语句。如果没有为for循环提供条件列表,Shell将从命令行获取条件列表。不带列表的for循环语句的一般语法如下:

for variable
do
   statement1
   statement2
   ...
done

由于系统变量$@同样可以获取所有的参数,所以以上的语法等价于以下语法:

for variable in $@或$*
do
 statement1
 statement2
 ...
done

示例:

[root@kittod ~]# cat 5.sh
#!/bin/bash
for arg
do
    echo $arg
done
[root@kittod ~]# bash 5.sh {1..6}
1
2
3
4
5
6

5.1.3 类C风格的for循环语句

for ((expression1;expression2;expression3))
do
 statement1;
 statement2;
 ...
done

在上面的语法中,for循环语句的执行条件被2个圆括号包括起来。执行条件分为3个部分,由2个分号隔开,第1部分expression1通常是条件变量初始化的语句;第2部分expression2是决定是否执行for循环的条件。当expression2的值为0时,执行整个循环体;当expression2的值为非0时,退出for循环体。第3部分,即表达式expression3通常用来改变条件变量的值,例如递增或者递减等。

示例1:批量创建用户:

用户名以test开头,按数字序号变化;

一共添加30个账号,即test01,tes02...,test30

用户初始密码为123456

分析:
 根据要求使用循环语句完成
 创建用户使用useradd命令,根据命名规则,10以下的需要加0
 初始账号设置方法(echo “123456” |passwd –stdin username)

解答:

[root@kittod ~]# cat add_user.sh
#!/bin/bash
for ((i=1;i<=30;i++))
do
  if [ $i -lt 10 ]
  then
    user=test0$i
  else
    user=test$i
  fi
  
  if ! id -u $user &> /dev/null
  then
      useradd $user
      echo "12456" | passwd --stdin $user &> /dev/null
  else
      echo "$user is exists..."
  fi
done
[root@kittod ~]# bash add_user.sh
[root@kittod ~]# grep test /etc/passwd | cut -d ":" -f1
test01
test02
test03
test04
test05
test06
test07
test08
test09
test10
test11
test12
test13
test14
test15
test16
test17
test18
test19
test20
注意:可以简化写法,直接用for带列表的循环,这样就不用for里面嵌套if判断
比如for i in {01..30}

示例2:编写一个 Shell 程序,实现判断当前网络(假定为192.168.1.0/24,根据实际情况实现)里,当前在线用户的IP有哪些。

[root@kittod ~]# cat online_host.sh
#!/bin/bash
for ((i=1;i<=254;i++))
do
  if ping -c 1 192.168.132.$i &> /dev/null
  then
    echo "192.168.132.$i is up..."
  else
    echo "192.168.132.$i is unknown..."
  fi
done

5.2 while循环语句

while循环是另外一种常见的循环结构。使用while循环结构,可以使得用户重复执行一系列的操作,直到某个条件的发生。这听起来好像跟until循环非常相似,但是与until语句相比,while语句有着较大的区别。

while循环语句的基本语法如下:

while expression
do
 statement1
 statement2
 ...
done

在上面的语法中,expression表示while循环体执行时需要满足的条件。虽然可以使用任意合法的Shell命令,但是,通常情况下,expression代表一个测试表达式,当expression的值为0时才执行循环体中的语句,每次执行到done时就会重新判断while条件表达式是否成立,当expression的值为非0值时,将退出循环体。与其他的循环结构一样,do和done这2个关键字之间的语句构成了循环体。

5.2.1 while循环读取文件

方法一:采用exec读取文件,然后进入while循环处理
[root@kittod ~]# cat file
shuju1
shuju2
shuju3
shuju4
[root@kittod ~]# cat file.sh
#!/bin/bash
exec < file
while read a   #read是按行赋值给a
do
    echo $a
done
[root@kittod ~]# bash file.sh
shuju1
shuju2
shuju3
shuju4


方法二:使用cat读文件,然后通过管道进入while循环处理
#!/bin/bash
cat file | while read line
do
        echo $line
done


方法三:通过在while循环结尾,使用输入重定向方式
while read line
do
       echo $line
done < File

5.2.2 while循环语句示例

示例1:猜商品价格

通过变量RANDOM获得随机数价格

提示用户猜测并记录次数,猜中后退出循环

分析: 产生随机数价格 猜中退出循环,显然要用死循环(while true),满足条件退出程序
通过if条件判断,根据用户输入数值和生成随机数比较 记录猜测次数,每猜测一次加1

解答:

[root@kittod ~]# cat price.sh
#!/bin/bash
PRICE=$[ $RANDOM % 100 ]  #$RANDOM 生成一个随机数
TIMES=0
while true  #注意:此处要写ture不能写1
do
   read -p "Please enter the product price [0-99] : " INT
   let TIMES++
  if [ $INT -eq $PRICE ]
  then
    echo "Good luck,you guessed it."
    echo "You have guessed $TIMES times."
    exit 0
  elif [ $INT -gt $PRICE ]
  then
    echo "$INT is too high"
  else
    echo "$INT is too low"
  fi
done
[root@kittod ~]# bash price.sh
Please enter the product price [0-99] : 80
80 is too low
Please enter the product price [0-99] : 40
40 is too low
Please enter the product price [0-99] : 90
90 is too high
Please enter the product price [0-99] : 85
85 is too low
Please enter the product price [0-99] : 88
88 is too high
Please enter the product price [0-99] : 87
87 is too high
Please enter the product price [0-99] : 86
Good luck,you guessed it.
You have guessed 7 times.
通用的算法公式%random%%%(max-min+1)+min来产生[min,max]区间里的随机数,最大值
和最小值均能取到。

示例2:while读取文件

工作过程中遇到要从一个ip列表中获取ip port,然后ssh ip 到目标机器进行特定的操作

[root@kittod ~]# cat iplist
192.168.132.128 22
192.168.132.129 22
192.168.132.130 22
[root@kittod ~]# cat ssh_port.sh
#!/bin/bash
while read line
do
  IP=$(echo $line |awk '{print $1}')   #awk '{print $1}' 截取并打印第一列
  PORT=$(echo $line | awk '{print $2}')  #awk '{print $2}' 截取并打印第二列
  echo "IP: $IP, PORT: $PORT"
done < iplist  #将iplist中的数据进行循环

[root@kittod ~]# bash ssh_port.sh
IP: 192.168.132.128, PORT: 22
IP: 192.168.132.129, PORT: 22
IP: 192.168.132.130, PORT: 22

注意:
[root@kittod ~]# cat ssh_port.sh
#!/bin/bash
while read line
do
IP=$(echo $line |awk '{print $1}')
PORT=$(echo $line | awk '{print $2}')
echo "IP: $IP, PORT: $PORT"
ssh root@$IP
done < iplist
[root@kittod ~]# bash ssh_port.sh
IP: 192.168.132.128, PORT: 22
Pseudo-terminal will not be allocated because stdin is not a terminal.
The authenticity of host '192.168.132.128 (192.168.132.128)' can't be
established.
ECDSA key fingerprint is
SHA256:Rwf0ESUa0h81yrog0NwU5f1rFuE9PJ1iByFEfwlKWKk.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
yes
Warning: Permanently added '192.168.132.128' (ECDSA) to the list of
known hosts.
root@192.168.132.128's password:
-bash: line 1: 192.168.132.129: command not found
-bash: line 2: 192.168.132.130: command not found

如果在while循环中调用了ssh命令,那么ssh就会把当前输入中所有的数据读走,也就是重定
向给while命令的数据,都被ssh命令读走了,以至于下次循环的时候,read读到的内容为
空,导致循环提前结束。

示例3:将之前用for语句创建的test01-test30用户删除

[root@kittod ~]# cat del_user.sh
#!/bin/bash
i=1
while [ $i -le 20 ]
do
  if [ $i -lt 10 ]
  then
    user=test0$i
  else
    user=test$i
  fi
  if id -u $user &> /dev/null   #id -u显示用户id
  then
    userdel -r $user
  else
    echo "$user is not exists..."
  fi
  let i++
done
注意:while判断变量也可以使用(()),比如while ((i<=20))

5.3 until循环语句

until循环语句同样也存在于多种程序设计语言中。顾名思义,until语句的作用是将循环体重复执行,直到某个条件成立为止。until语句的基本语法如下:

until expression  #表达式为假时执行
do
 statement1
 statement2
 ...
done

条件为假的时候执行

在上面的语法中,expression是一个条件表达式。当该表达式的值不为0时,将执行do和done之间的语句;当expression的值为0时,将退出until循环结构,继续执行done语句后面的其它的语句。

示例:将之前用for语句创建的test01-test30用户删除

[root@kittod ~]# cat del_user2.sh
#!/bin/bash
i=1
until [ $i -gt 20 ]   #与while循环类似但是为假的时候执行
do
  if [ $i -le 9 ]
  then
    user=test0$i
  else
    user=test$i
  fi
  if id -u $user &> /dev/null
  then
     userdel -r $user
  else
    echo "$user is not exists..."
  fi
  let i++
done

5.4 select循环语句

select循环语句的主要功能是创建菜单,在执行带有select循环语句脚本时,输出会按照数字顺序的列表显示一个菜单,并显示提示符(默认是#?),同时等待用户输入数字选择。

select语句的基本语法如下:

select 变量名 [ in 菜单值列表 ]
do
 statement1
 statement2
 ...
done

示例:

[root@kittod ~]# cat select.sh
#!/bin/bash
select mysql_version in 5.1 5.6
do
  echo $mysql_version
done
[root@kittod ~]# bash select.sh
1) 5.1
2) 5.6
#? 1
5.1
#? 2
5.6
#? 3

select可以快速创建菜单

5.5 嵌套循环

在程序设计语言中,嵌套的循环也是一种非常常见的结构。Shell同样也支持嵌套循环。通过嵌套循环,可以完成更复杂的功能。

示例1:打印九九乘法表

[root@kittod ~]# cat 99.sh
#!/bin/bash
for i in `seq 9`
do
  for j in `seq 9`
  do
    [ $j -le $i ] && echo -n "$j*$i = `echo $(($j*$i))` "
  done
  echo " "
done
[root@kittod ~]# bash 99.sh
1*1 = 1  
1*2 = 2 2*2 = 4  
1*3 = 3 2*3 = 6 3*3 = 9  
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16  
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25  
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36  
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49  
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64  
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81  

示例2:打印三角形

使用双层循环
 外层循环控制输出行数i:1-10
 内层循环:
 第一个循环打印每行空格j
 第二个循环打印数字k
 i(取值) 1 2 3 ... 9
 j(个数) 9 8 7 1 j<=10-i
 k(个数) 1 2 3 9 k<=i

解答:

[root@kittod ~]# cat sjx.sh
#!/bin/bash
for ((i=1;i<10;i++))
do
for ((j=1;j<10-i;j++))
do
echo -n " "
done
for ((k=1;k<=i;k++))
do
echo -n "$i "
done
echo " "
done
[root@kittod ~]# bash sjx.sh
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9

示例3:打印杨辉三角

分析:杨辉三角
1
1 1
1 2 1
1 3 3 1
1、每行数字左右对称,由1开始逐渐变大,然后变小,回到1。
2、第n行的数字个数为n个。   
3、第n行数字和为2^(n-1)。(2的(n-1)次方)   
4、每个数字等于上一行的左右两个数字之和。
5、将第2n+1行第1个数,跟第2n+2行第3个数、第2n+3行第5个数……连成一线,这些数的和是
第2n个斐波那契数。将第2n行第2个数,跟第2n+1行第4个数、第2n+2行第6个数……这些数之
和是第2n-1个斐波那契数。   
6、第n行的第1个数为1,第二个数为1×(n-1),第三个数为1×(n-1)×(n-2)/2,第四个数
为1×(n-1)×(n-2)/2×(n-3)/3…依此类推。   
7.两个未知数和的n次方运算后的各项系数依次为杨辉三角的第(n+1)行。

解答:

[root@localhost test5]# vim yanghui.sh
#!/bin/bash
if (test -z $1)
then
 read -p "Input Max Lines:" MAX
 else
MAX=$1
fi
i=1
while [ $i -le $MAX ] #i行控制
do
j=1
while [ $j -le $i ] #j列控制
do
f=$[i-1] #f=i-1 这是另一种计算写法。
g=$[j-1] #g=j-1 这是另一种计算写法。
if [ $j -eq $i ] || [ $j -eq 1 ] ; then
 declare SUM_${i}_$j=1 #声明变量 头尾都是1
else
 declare A=$[SUM_${f}_$j] #取上一行的j列变量
 declare B=$[SUM_${f}_$g] #取上一行的j-1列变量
 declare SUM_${i}_$j=`expr $A + $B` #声明并计算当前变量
的值
fi
echo -en $[SUM_${i}_$j]" " #输出当前变量
 let j++
done
echo #换行
 let i++
done
[root@localhost test5]# bash yanghui.sh
Input Max Lines:4
1
1 1
1 2 1
1 3 3 1

5.4 利用break和continue语句控制循环

在Shell中的循环结构中,还有2个语句非常有用,即break和continue语句。前者用于立即从循环中退出;而后者则用来跳过循环体中的某些语句,继续执行下一次循环。

break语句的作用是立即跳出某个循环结构。break语句可以用在for、while或者until等循环语句的循环体中。

continue语句则比较有趣,它的作用不是退出循环体。而是跳过当前循环体中该语句后面的语句,重新从循环语句开始的位置执行。

示例:

[root@kittod ~]# cat for_break.sh
#!/bin/bash
for i in `seq 10`
do
  if [ $i -eq 4 ]
  then
     break
  fi
  echo $i
done
[root@kittod ~]# bash for_break.sh
1
2
3
[root@kittod ~]# cat for_continue.sh
#!/bin/bash
for i in `seq 10`
do
  if [ $i -eq 4 ]
  then
     continue
  fi
  echo $i
done
[root@kittod ~]# bash for_continue.sh
1
2
3
5
6
7
8
9
10

练习:

1. 使用case实现成绩优良差的判断
2. for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
3. for ping测试指网段的主机网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 ---192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
4. 使用for实现批量主机root密码的修改成功或失败都必须记录
提示:主机IP存放在一个文件中
 SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
 # ssh-keygen 在用于管理的主上生成密钥对
 # ssh-copy-id -i 192.168.2.3

1. 使用case实现成绩优良差的判断

#!/usr/bin/env bash

read -p "输入成绩:" num
case $num in
[0-5][0-9])
    echo "不及格"
;;
[6-7][0-9])
    echo "良好"
;;
[8-9][0-9] |100)
    echo "优秀"
;;
esac

2. for创建20用户

用户前缀由用户输入

用户初始密码由用户输入

例如:test01,test10

#!/bin/bash
read -p "请输入用户前缀:" user
read -p "请输入初始密码:" pass
for ((i=1;i<=20;i++ ))
do
        if [ $i -lt 10 ];then
                users=${user}0$i
        else
                users=${user}$i
        fi
 
        if ! id -u $users &> /dev/null
        then
            useradd $users
            echo "$pass" | passwd --stdin $users &> /dev/null
        else
                echo "$users is exists..."
        fi
done

3. for ping测试指网段的主机网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 ---192.168.2.20

UP: /tmp/host_up.txt

Down: /tmp/host_down.txt

#!/bin/bash
read -p "请输入网段: " ip
for ((i=10;i<=20;i++))
do
        IP="$ip"."$i"
        echo $IP
        if ping -c 2 $IP &> /dev/null
        then
                echo "$IP is up" >>/tmp/host_up.txt
        else
 
            echo "$IP is down" >>/tmp/host_down.txt                                                  
 cat 
        fi
 
done

4. 使用for实现批量主机root密码的修改成功或失败都必须记录

提示:主机IP存放在一个文件中

SSH:实现公钥认证,执行远程中主机命令实现公钥认证

# ssh-keygen 在用于管理的主上生成密钥对

# ssh-copy-id -i 192.168.2.3

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/548093.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何使用ArcGIS Pro进行路径分析

路径分析是一种空间分析技术&#xff0c;用于确定两个或多个地点之间最佳路径或最短路径&#xff0c;这里为大家介绍一下在ArcGIS Pro中如何进行路径分析&#xff0c;希望能对你有所帮助。 数据来源 教程所使用的数据是从水经微图中下载的道路数据&#xff0c;除了道路数据&a…

大厂面试:获取字符串的全排列

一、概念 现有一个字符串&#xff0c;要打印出该字符串中字符的全排列。例如输入字符串abc&#xff0c;则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。 可以基于回溯法来解决这个问题。 二、代码 public class Permutation {//输出字符串str的全…

【自媒体创作利器】AI白日梦+ChatGPT 三分钟生成爆款短视频

AI白日梦https://brmgo.com/signup?codey5no6idev 引言 随着人工智能&#xff08;AI&#xff09;技术的快速发展&#xff0c;AI在各个领域都展现出了强大的应用潜力。其中&#xff0c;自然语言处理技术的进步使得智能对话系统得以实现&#xff0c;而ChatGPT作为其中的代表之一…

【Linux】磁盘阵列RAID技术

目录 一、RAID介绍 1.1 什么是RAID技术&#xff1f; 1.2 为什么要使用RAID技术&#xff1f; 二、RAID级别 2.1 常见的RAID级别 2.2 常见RAID介绍 三、RAID特性对比 一、RAID介绍 1.1 什么是RAID技术&#xff1f; 把多块独立的物理磁盘按不同的方式组合起来形成一个硬盘…

人工智能讲师大模型培训老师叶梓:基于大型语言模型的自主智能体:架构设计与应用前景

在人工智能的快速发展中&#xff0c;大型语言模型&#xff08;LLM&#xff09;已成为推动技术进步的关键力量。LLM的出现不仅改变了我们与机器的交互方式&#xff0c;也为构建具有高级认知能力的自主智能体&#xff08;AI Agent&#xff09;提供了新的可能性。本文旨在探讨基于…

(非技术) 基因遗传相关知识学习笔记

目录 一、基因遗传名词解释 二、什么叫显性遗传和隐性遗传&#xff1f; 三、如何确定遗传性质呢&#xff1f;是显性还是隐性&#xff1f; 四、常规例子1&#xff1a; 五、常规例子2&#xff1a; 六、实际案例&#xff1a; 七、思考题&#xff1a; 八、参考&#xff1a; …

云计算: OVN 集群 部署分布式交换机

目录 一、实验 1.环境 2.OVN 集群 部署云主机 3.中心端添加DVS分布式大二层交换机 二、问题 1.南向控制器查看主机名只显示localhost 2.中心端如何添加DVR分布式⼤三层路由器 一、实验 1.环境 (1) 主机 表1 宿主机 主机架构软件主要服务IP备注ovn_central中心端 ovn…

【Linux】基础I/O>文件系统软硬链接动静态库详解

主页&#xff1a;醋溜马桶圈-CSDN博客 专栏&#xff1a;Linux_醋溜马桶圈的博客-CSDN博客 gitee&#xff1a;mnxcc (mnxcc) - Gitee.com 目录 1.C语言文件接口 1.1 hello.c写文件 1.2 hello.c读文件 1.3 输出信息到显示器 1.4 stdin & stdout & stderr 1.5 总结打…

Android Framework学习笔记(3)----Binder

什么是Binder&#xff1f; Binder是linux IPC机制的其中一种。它贯穿于应用层&#xff0c;framework层&#xff0c;以及linux Core层。 什么是IPC? 跨进程通信&#xff0c; InterProcess Communication. IPC机制都有哪些&#xff1f; 通道信号量消息队列BinderSocket共享内…

从IPv4到IPv6:解密网络通信的新时代

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 从IPv4到IPv6&#xff1a;解密网络通信的新时代 前言ipv4介绍ipv6介绍IPv4与IPv6的区别IPv4地址枯竭问题和IPv6的解决方案 ipv6的优势IPv6在新兴技术领域的应用 ipv4向ipv6的过渡挑战解决方案IPv6部署…

NVIDIA全系列GPU技术路线演进分析

NVIDIA GPU 架构梳理 近期深入研究并行计算,需探究底层硬件精髓。高性能计算界,英伟达显卡稳居霸主地位。本文旨在梳理NVIDIA GPU架构之演进历程,助您洞悉其技术脉络,把握未来计算趋势。 目录: NVIDIA GPU架构历经数次革新:从Tesla架构奠定基石,到Fermi架构提升性能,再…

【JAVA基础篇教学】第十五篇:Java中Spring详解说明

博主打算从0-1讲解下java基础教学&#xff0c;今天教学第十五篇&#xff1a;Java中Spring详解说明。 Spring 框架是一个广泛应用于 Java 开发的轻量级、全栈式的企业应用开发框架&#xff0c;它提供了众多功能强大的模块&#xff0c;用于简化企业级应用程序的开发。下面详细说…

MySQL慢查询日志配置指南:发现性能瓶颈,提升数据库效率

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 MySQL慢查询日志配置指南&#xff1a;发现性能瓶颈&#xff0c;提升数据库效率 前言慢查询日志介绍配置慢查询日志配置慢查询日志失效日志格式与记录内容高级配置与注意事项配置过程中的注意事项&…

【python】flask操作数据库工具SQLAlchemy,详细用法和应用实战

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

EasyRecovery激活秘钥2024最好用的电脑数据恢复软件下载

EasyRecovery数据恢复软件是一款专业且功能强大的数据恢复工具&#xff0c;它旨在帮助用户从各种存储设备中恢复由于各种原因&#xff08;如误删除、格式化、病毒攻击、系统崩溃等&#xff09;导致丢失的数据。这款软件支持多种存储介质&#xff0c;包括但不限于硬盘驱动器、U盘…

白盒测试之路径覆盖与基本路径覆盖

白盒测试之路径覆盖与基本路径覆盖&#xff08;蓝桥课学习笔记&#xff09; 1、路径覆盖 实验介绍 程序中的路径是执行程序时经过的分支的集合。路径覆盖法是指设计一定数量的测试用例运行被测程序&#xff0c;使程序中的所有路径都至少被执行一次。路径覆盖率的计算方法为&a…

web前端js笔记

1&#xff0c;对象 let{ 属性 方法 } 2&#xff0c;闭包 只有函数内部的子函数才能读取局部变量&#xff0c;所以闭包可以理解成定义在一个函数内部的函数&#xff0c;在本质上&#xff0c;闭包是将函数内部和函数外部连接起来的桥梁。 3&#xff0c;math console.log(Math.flo…

✌粤嵌—2024/4/15—汇总区间

代码实现&#xff1a; /*** Note: The returned array must be malloced, assume caller calls free().*/ char** summaryRanges(int* nums, int numsSize, int *returnSize) {char **res malloc(sizeof(char*) * numsSize);*returnSize 0;int i, j;for (i 0; i < numsSi…

npm run serve报错:error:0308010C:digital envelope routines::unsupported

这是因为Node.js17中的OpenSSL加密库不支持当前的加密设置。 因为新版本默认使用OpenSSL 3&#xff0c;它可能不再支持旧版的加密算法或配置。 > demo0.1.0 serve > vue-cli-service serveINFO Starting development server... 10% building 2/5 modules 3 active ...…

2024蓝桥A组D题

团建 问题描述格式输入格式输出样例输入样例输出评测用例规模与约定解析参考程序难度等级 问题描述 格式输入 输入的第一行包含两个正整数n,m&#xff0c;用一个空格分隔。 第二行包含n个正整数c1,c2, ,cn&#xff0c;相邻整数之间使用一个空格分隔&#xff0c; 其中ci表示第一…
最新文章