shell之数组

本文详细介绍了Shell脚本中数组的定义、打印与输出方法,包括动态定义数组并利用命令输出作为内容。同时,讲解了如何使用RADEM生成随机数以及openssl创建随机字符串。文中还设置了实践练习,如用循环输出数组下标和创建带有随机密码的用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.数组定义方法

(1)
[root@base1 mnt]# array=(1 2 3)   # 定义数组
[root@base1 mnt]# echo ${array[*]}  # 输出数组中的所有
1 2 3 
(2)
[root@base1 mnt]# array=([1]=one [2]=two [3]=three) # 子定义数组元素的下标[
root@base1 mnt]# echo ${array[*]}
one two three
(3)
[root@base1 mnt]# array[0]=a
[root@base1 mnt]# array[1]=b
[root@base1 mnt]# array[2]=c
[root@base1 mnt]# array[3]=d
[root@base1 mnt]# echo ${array[0]}
a
[root@base1 mnt]# echo ${array[2]}
c
[root@base1 mnt]# echo ${array[*]}
a b c d
(4)动态定义数组变量并使用命令的输出结果作为数组的内容
[root@base1 mnt]# mkdir /array -p
[root@base1 mnt]# touch /array/{1..3}.txt
[root@base1 mnt]# ls /array/
1.txt  2.txt  3.txt
[root@base1 mnt]# array=($(ls /array))  # 把ls /array的输出结果作为数组的值
[root@base1 mnt]# echo ${array[*]}  # 打印数组
1.txt  2.txt  3.txt

2.数组的打印和输出

(1)打印数组元素
[root@base1 mnt]# array=(one two three)
[root@base1 mnt]# echo ${array[0]}  # 默认第一个元素的下标是0
one
[root@base1 mnt]# echo ${array[1]}
two
[root@base1 mnt]# echo ${array[@]}  # 打印数组全部元素,@和*是一样的效果
one two three
(2)打印元素个数
[root@base1 mnt]# array=(one two three)
[root@base1 mnt]# echo ${#array[@]}
3
[root@base1 mnt]# echo ${#array[*]}
3
(3)数组的赋值
如果下标不存在,则自动添加一个新元素,如果存在,则覆盖原来的值

[root@base1 mnt]# array=(one two three)
[root@base1 mnt]# array[3]=four
[root@base1 mnt]# echo ${array[*]}
one two three four
[root@base1 mnt]# array[0]=hello  # 重新定义下标为0的数组元素
[root@base1 mnt]# echo ${array[*]}  # 查看定义成功
hello two three four
[root@base1 mnt]# echo ${array[@]:1:3}  # 打印下标从1到3的数组元素
two three four
[root@base1 mnt]# array=(1 2 3 1 3)
[root@base1 mnt]# echo ${array[*]}
1 2 3 1 3
[root@base1 mnt]# echo ${array[*]/1/a}  # 把数组中所有的1替换成为a
a 2 3 a 3
[root@base1 mnt]# array=(1 2 3 1 3)
[root@base1 mnt]# unset array[1]   # 删除下标为1的数组元素
[root@base1 mnt]# echo ${array[*]}
1 3 1 3
[root@base1 mnt]# unset array   # 删除所有的数组元素
[root@base1 mnt]# echo ${array[*]}
练习1:用循环输出数组中元素的下标
[root@base1 mnt]# vim shuzu_xiabiao.sh
  #!/bin/bash 
 array=(7 3 4 6 2) 
  for ((i=0;i<${#array[*]};i++)) 
  do 
      echo $i 
  done

在这里插入图片描述

[root@base1 mnt]# sh shuzu_xiabiao.sh	

在这里插入图片描述

练习2:用for循环打印下面这句话中字母个数不大于6的单词

I am westos teacher welcome to westos training class

[root@base1 mnt]# vim shuzu_bianli.sh 
 #!/bin/bash 
 array=(I am westos tescher welcome to westos training class) # 定义数组元素 
 for ((i=0;i<${#array[@]};i++))  # 循环遍历数组元素 
 do 
     if [ ${#array[$i]} -lt 6 ];then  # 在数组元素中继续遍历,统计数组中每个元素的个数 
         echo ${array[$i]} 
     fi   
 done

在这里插入图片描述

[root@base1 mnt]# sh  shuzu_biali.sh

在这里插入图片描述

3.随机数

(1) RADEM #生成随机数,数值在0~32767之间

[root@base1 mnt]# echo $RANDOM
4423
[root@base1 mnt]# echo $RANDOM
3378
[root@base1 mnt]# echo $RANDOM
11089
(2)openssl rand -base64 40 # 生成随机字符串,表示生成64位的字符串40位
[root@base1 mnt]# openssl  rand  -base64 40  
Wa2LptnCNkntSStZBIaTCHNsTQSpkriz3lrPBJ1iS1GTrterBr5pPg==
练习1:创建10个用户,用户名为westos_{1…10}.密码随机
[root@base1 mnt]# vim rand_useradd.sh 
 #!/bin/bash 
 . /etc/init.d/functions   # 调用系统库函数
 user="westos"  # 定义用户名字 
 passfile="/root/Desktop/userfile"  # 定义密码保存文件 
 for num in `seq -w 10`  # 生成10个数字 
 do 
     pass="`echo $RANDOM | md5sum | cut -c 7-14`"  # 生成随机密码 
     useradd $user$num &> /dev/null && {   # 建立用户 
         echo "$pass" | passwd --stdin $user$num &> /dev/null # 修改用户密码 
         echo -e "user:$user$num\tpasswd:$pass" >> $passfile  # 把密码保存在文件里 
     } 
     if [ $? -eq 0 ];then  # 如果建立成功,就输出ok 
         action "$user$num is ok" /bin/true 
     else 
         action "$user$num is failed" /bin/false  # 如果建立失败,就输出failed 
     fi 
 done

在这里插入图片描述

[root@base1 mnt]# sh rand_useradd.sh  # 执行脚本,建立用户

在这里插入图片描述

[root@base1 mnt]# cat  /root/Desktop/userfile   # 查看密码,保存成功

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值