linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素

linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素,第1张

题主你好,

代码及测试截图如下:

下面这个例子可能对题主有帮助:

说明: array和array1是两个数组, 其中的内容分别为aa bb cc和dd ee ff, 然后再定义一个数组array2,这个数组中的元素为array和array1,最后使用for循环,通过array2数组将array和array1中的元素输出

希望可以帮到题主, 欢迎追问

你的意思我大概明白了,但是你的第二步不清楚是啥意思,是把所有文件当中,最后修改的那个压缩起来,还是把所有的文件都压缩起来?

我来说下思路和过程。

首先我们需要用到精易模块,这样方便一些。

1、使用精易模块命令,文件_枚举来获取目录下所有的bak文件

2、计次循环来获取取到的bak文件的每一次最后修改时间存放到一个变量

3、我们把获取到的最后修改时间转换成时间戳

4、我们把时间戳和文件路径放到一个数组里

5、然后用“数组排序”对数组里存放的时间戳进行排序。注意的是,我们排序的时候要取时间戳,不用把路径放进去排序。

6、添加zip压缩组件,来进行目录下的压缩。并对修改时间为最后修改的也就是时间戳最大的也就是数组排序中最后的三个进行标记

7、全部压缩,移动到目录321

我理解的大概是这样吧。哪里不对或者要源码的留下QQ,我加你。

| 对于初学者而言,因为没有实战经验,写不出来 Shell 脚本 很正常,如果工作了几年的运维老年还是写不出来,那就是没主动找需求,缺乏练习,缺乏经验。针对以上问题,总结了30个生产环境中经典的 Shell 脚本 ,通过这些需求案例,希望能帮助大家提升Shell编写思路,掌握编写技巧。 |

先了解下编写Shell过程中注意事项:

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">开头加解释器:#!/bin/bash

语法缩进,使用四个空格;多加注释说明。

命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用。

默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作用域。

有两个 命令 能帮助我调试脚本:set -e 遇到执行非0时退出脚本,set-x 打印执行过程。

写脚本一定先测试再到生产上。

</pre>

1、获取随机字符串或数字

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">获取随机8位字符串:

方法1:

471b94f2

方法2:

vg3BEg==

方法3:

ed9e032c

获取随机8位数字:

方法1:

23648321

方法2:

38571131

方法3:

69024815

cksum:打印CRC效验和统计字节

</pre>

2、定义一个颜色输出字符串函数

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:

function echo_color() {

if [ 2\033[0m"

elif [ 2\033[0m"

fi

}

方法2:

function echo_color() {

case 2[0m"

;;

red)

echo -e "[31;40m$2[0m"

;;

)

echo "Example: echo_color red string"

esac

}

使用方法:echo_color green "test"

function关键字定义一个函数,可加或不加。

</pre>

3、批量创建用户

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

DATE= 1 == "green" ]; then

echo -e "[32;40m 1 == "red" ]; then

echo -e "[31;40m$2[0m"

fi

}

if [ -s USER_FILE {DATE}bak

echo_color green " {USER_FILE}- USER_FILE

echo "----------------" >> USER &>/dev/null; then

PASS= RANDOM |md5sum |cut -c 1-8)

useradd PASS |passwd --stdin USER USER_FILE

echo " USER User already exists!"

fi

done

</pre>

4、检查软件包是否安装

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

if rpm -q sysstat &>/dev/null; then

echo "sysstat is already installed"

else

echo "sysstat is not installed!"

fi

</pre>

5、检查服务状态

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

PORT_C= (ps -ef |grep ntpd |grep -vc grep)

if [ PS_C -eq 0 ]; then

echo "内容" | mail -s "主题" dst@examplecom

fi

</pre>

6、检查主机存活状态

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:将错误IP放到数组里面判断是否ping失败三次

IP_LIST="192168181 19216811 192168182"

for IP in NUM -le 3 ]; do

if ping -c 1 IP Ping is successful"

break

else

# echo " NUM"

FAIL_COUNT[ IP

let NUM++

fi

done

if [ {FAIL_COUNT[1]} Ping is failure!"

unset FAIL_COUNT[]

fi

done

方法2:将错误次数放到FAIL_COUNT变量里面判断是否ping失败三次

IP_LIST="192168181 19216811 192168182"

for IP in IP >/dev/null; then

echo " IP Ping is failure FAIL_COUNT -eq 3 ]; then

echo "$IP Ping is failure!"

fi

done

方法3:利用for循环将ping通就跳出循环继续,如果不跳出就会走到打印ping失败

ping_success_status() {

if ping -c 1 IP Ping is successful"

continue

fi

}

IP_LIST="192168181 19216811 192168182"

for IP in IP Ping is failure!"

done

</pre>

7、监控CPU、内存和硬盘利用率

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)CPU

借助vmstat工具来分析CPU统计信息。

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (( SY))

if [ DATE

Host: USE

" | mail -s "CPU Monitor" $MAIL

fi

2)内存

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (free -m |awk /Mem/{print (free -m |awk /Mem/{print 6- (( USE))

if [ DATE

Host: TOTAL,Use= FREE

" | mail -s "Memory Monitor" $MAIL

fi

3)硬盘

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (fdisk -l |awk -F [: ]+ BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,", 3} )

PART_USE= 1,int( 6} )

for i in (echo (echo (echo USE -gt 80 ]; then

echo "

Date: IP

Total: PART= MOUNT)

" | mail -s "Disk Monitor" $MAIL

fi

done

</pre>

8、批量主机磁盘利用率监控

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">前提监控端和被监控端SSH免交互登录或者密钥登录。

写一个配置文件保存被监控主机SSH连接信息,文件内容格式:IP User Port

HOST_INFO=hostinfo

for IP in 1} (awk -v ip= 1{print HOST_INFO)

PORT= IP ip== 3} PORT IP df -h > (awk BEGIN{OFS="="}/^/dev/{print 5)} USE_RATE_LIST; do

PART_NAME= {USE_RATE#=}

if [ PART_NAME Partition usage $USE_RATE%!"

fi

done

done

</pre>

9、检查网站可用性

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)检查URL可用性

方法1:

check_url() {

>

看下你使用的bash版本到没到40,使用指令bash --version查看

使用变量前先确认该变量是否在之前已经被定义过了 使用source 命令或  命令启动脚本时是不会开辟新线程的所以你在bash窗口里定义的没加local 变量 都会被脚本继承 此时你可以先删除这个变量 即定义phone 时 unset phone

呃~~

从整体来说,这些东西是有机组合的,你中有我,我中有你。

变量是最基本的,找怪过程,把怪的识别码输入变量a中,找第一个npc代码输入b中

if

a=b

then

找到怪

esle

没找到

end

if

如果你要找10个怪

你重复上面那段就显得太不专业了,那就可以用到变量了。

a(0)=a1

//第一个怪的识别码存入数组a(0)

a(1)=a2

//第二个怪的识别码存入数组a(1)

a(9)=a10//第十个怪的识别码存入数组a(9)

s=0

while

s<10

if

a(s)=b

then

当前目标是第s+1种怪

s=11//找到怪,s设为11,没找到的话,s最终为10

esle

当前目标不是第s+1种怪

end

if

s=s+1

wend

如果每次找怪都要写同一段代码的话,那显得不专业,这时就可以用sub了。

把上面那段找怪代码做成sub,需要时写入 call 找怪 ,即方便又明了。

linux脚本文件中,从文件中读取数据并赋值给变量的 *** 作方法和步骤如下:

1、首先,创建一个名为shellsh的文件:vi shellsh,如下图所示。

2、其次,完成上述步骤后,输入头文件#!/bin/bash,如下图所示。

3、接着,完成上述步骤后,保存并打开权限(chmod +x  shellsh)并运行:/shellsh,如下图所示。

4、然后,完成上述步骤后,输入一个字符串,如下图所示。

5、随后,完成上述步骤后,要计算乘以5的值,需要按如下方式编辑shellsh(vi shellsh)脚本命令,如下图所示。

6、最后,保存,开权限运行:输出一个整数,如下图所示。这样,问题就解决了。

使用下标来获取数组中的元素,数组下标从0开始计数。要获取数组vararr中的元素6,可以使用下标4来获取,即vararr[4]。数组是有序的元素序列。若将有限个类型相同的变量的集合命名,那么这个名称为数组名。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。

以上就是关于linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素全部的内容,包括:linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素、易语言实现备份压缩获取修改时间 *** 作、shell脚本上等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9632047.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存