shell编程之存储读写测试实战脚本
阅读原文时间:2023年08月22日阅读:1

Shell编程是一种在命令行环境中编写程序的技术,常用于Linux和Unix系统。它主要使用Shell脚本语言来编写程序。Shell编程常用于系统管理、自动化任务、批处理等领域。

常用的Shell脚本语言包括Bash、Csh、Tcsh和Zsh等。Bash是最常用的Shell编程语言,它是大多数Linux和Unix系统的默认Shell。

Shell编程中,变量使用"$“来引用,变量赋值使用”="。Shell程序中可以使用多种内建命令和Unix工具,如grep、sed、awk等。

编写Shell脚本程序需要在文本编辑器中编写代码,并使用"chmod +x"命令给予可执行权限,最后使用"./"执行该脚本程序。

举个例子,在文本编辑器中编写如下代码,保存为"test.sh":

#!/bin/bash echo “Hello, World!”

使用如下命令给该脚本添加可执行权限:

chmod +x test.sh

然后,执行该脚本:

./test.sh

这样就能在终端中看到输出 “Hello, World!”

请注意,在Windows上不能直接执行sh脚本,但是可以通过在Windows子系统 Linux (WSL) 中执行


实战示例:

#!/bin/bash

log=/root/rw_test.log
test_dir=/test__remotehost_mount

r_dir1=01/suyan_nfs_remote
r_dir2=02/suyan_nfs_remote
r_dir3=03/suyan_nfs_remote
r_dir4=04/suyan_nfs_remote

zabbix_sender="/opt/aspire/product/zabbix/bin/zabbix_sender -z 30.153.1.77 -s"

host1=30.1.177.151
host2=30.1.177.152
host3=30.1.177.153
host4=30.1.177.154
read="time timeout 30 df -h"
write="time timeout 30 touch"

#test rw 30.1.177.151
function RW_151()
{

$read $test_dir$r_dir1 >> $log 2>&1 && $write $test_dir$r_dir1/$host1.txt >> $log 2>&1
if [ $? -eq 0 ];then
echo "RW.$host1 successfully `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host1 -k "$host1"_rwtest -o "0"
else
echo "RW.$host1 failed `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host1 -k "$host1"_rwtest -o "1"
fi
}

#test rw 30.1.177.152
function RW_152()
{
$read $test_dir$r_dir2 >> $log 2>&1 && $write $test_dir$r_dir2/$host2.txt >> $log 2>&1
if [ $? -eq 0 ];then
echo "RW.$host2 successfully `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host2 -k "$host2"_rwtest -o "0"
else
echo "RW.$host2 failed `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host2 -k "$host2"_rwtest -o "1"
fi
}

#test rw 30.1.177.153
function RW_153()
{
$read $test_dir$r_dir3 >> $log 2>&1 && $write $test_dir$r_dir3/$host3.txt >> $log 2>&1
if [ $? -eq 0 ];then
echo "RW.$host3 successfully `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host3 -k "$host3"_rwtest -o "0"
else
echo "RW.$host3 failed `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host3 -k "$host3"_rwtest -o "1"
fi
}

#test rw 30.1.177.154
function RW_154()
{
$read $test_dir$r_dir4 >> $log 2>&1 && $write $test_dir$r_dir4/$host4.txt >> $log 2>&1
if [ $? -eq 0 ];then
echo "RW.$host4 successfully `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host4 -k "$host4"_rwtest -o "0"
else
echo "RW.$host4 failed `date +%Y-%m-%d-%H:%M:%S`" >> $log
$zabbix_sender $host4 -k "$host4"_rwtest -o "1"
fi
}

#running function
RW_151
RW_152
RW_153
RW_154