bash 脚本中命令的多个输出检查
Multiple output check of a command in bash script
我有一个检查下面的命令 status.Now 我想写一个 bash 脚本来检查 3 件事
Master_Host == '10.0.0.1' 和 Connect_Retry==60 和 Replicate_Ignore_DB=''
有人可以帮忙吗?
MariaDB []> show slave status\G;
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.1
Master_User: abc123
Master_Port: 8080
Connect_Retry: 60
Replicate_Ignore_DB:
将输出存储在变量中,然后使用 grep -q
三次:
stat=$(mariadb authmaster <<< 'show slave status\G;')
if grep -qx ' *Master_Host: *10.0.0.1 *' <<< "$stat" &&
grep -qx ' *Connect_Retry: *60 *' <<< "$stat" &&
grep -qx ' *Replicate_Ignore_DB: *' <<< "$stat"
then
echo ok
else
echo not ok
fi
或者 for loop
也许?
#!/usr/bin/env bash
stat=$(mariadb authmaster <<< 'show slave status\G;')
for f in ' *Master_Host: *10.0.0.1 *' ' *Connect_Retry: *60 *' ' *Replicate_Ignore_DB: *'; do
grep -qx "$f" <<< "$stat" || {
value=$?
printf 'Error: %s not found!\n' "$f" >&2
exit "$value"
}
done
printf 'All is ok!\n'
如果要查找的字符串比较多,可以用数组存储。
to_find=(
' *Master_Host: *10.0.0.1 *'
' *Connect_Retry: *60 *'
' *Replicate_Ignore_DB: *'
)
for f in "${to_find[@]}"; do
# the rest of the script here ...
我有一个检查下面的命令 status.Now 我想写一个 bash 脚本来检查 3 件事 Master_Host == '10.0.0.1' 和 Connect_Retry==60 和 Replicate_Ignore_DB=''
有人可以帮忙吗?
MariaDB []> show slave status\G;
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.1
Master_User: abc123
Master_Port: 8080
Connect_Retry: 60
Replicate_Ignore_DB:
将输出存储在变量中,然后使用 grep -q
三次:
stat=$(mariadb authmaster <<< 'show slave status\G;')
if grep -qx ' *Master_Host: *10.0.0.1 *' <<< "$stat" &&
grep -qx ' *Connect_Retry: *60 *' <<< "$stat" &&
grep -qx ' *Replicate_Ignore_DB: *' <<< "$stat"
then
echo ok
else
echo not ok
fi
或者 for loop
也许?
#!/usr/bin/env bash
stat=$(mariadb authmaster <<< 'show slave status\G;')
for f in ' *Master_Host: *10.0.0.1 *' ' *Connect_Retry: *60 *' ' *Replicate_Ignore_DB: *'; do
grep -qx "$f" <<< "$stat" || {
value=$?
printf 'Error: %s not found!\n' "$f" >&2
exit "$value"
}
done
printf 'All is ok!\n'
如果要查找的字符串比较多,可以用数组存储。
to_find=(
' *Master_Host: *10.0.0.1 *'
' *Connect_Retry: *60 *'
' *Replicate_Ignore_DB: *'
)
for f in "${to_find[@]}"; do
# the rest of the script here ...