Bash 多次检查字符串并执行不同的操作
我想根据结果进行多次检查,以执行不同的操作 在这种情况下,出现了一些问题:Bash 多次检查字符串并执行不同的操作,bash,grep,Bash,Grep,我想根据结果进行多次检查,以执行不同的操作 在这种情况下,出现了一些问题: ssh 10.1.1.1 'sapi=`ps -ef | grep weblogic | grep sapi | grep -v grep | wc -l`;if [ ! $sapi ]; then printf "unknown";elif [ "$sapi" == "0" ]; then printf "inactive";else printf "active";fi' 这个想法是当sapi字符串返回时: 0 -
ssh 10.1.1.1 'sapi=`ps -ef | grep weblogic | grep sapi | grep -v grep | wc -l`;if [ ! $sapi ]; then printf "unknown";elif [ "$sapi" == "0" ]; then printf "inactive";else printf "active";fi'
这个想法是当sapi字符串返回时:
0 - result to be inactive
1 or other kind of number - active
if is turn back nothing - to give unknown status
您可以使用
案例
。下面是我以前的一些帖子中的一个例子
#! /bin/bash
case "$(ps -ef | awk '/[w]eblogic/ && /sapi/ {a++} END {print a+0}')" in
0) echo "0 found"
;;
1) echo "1 found"
;;
*) echo "more than 1 found"
;;
esac
我也简化了你的测试。PS您不应该使用旧的和过时的后台技巧,使用括号
var=$(awk“some code”)
怎么了?问题是什么?使用ps
您可以测试服务是否正在运行(并计算它们的数量)。如果您尝试ssh
的服务器已关闭,则需要进行另一项测试。问题是,当“无结果”时,无法工作,这会导致我处于非活动状态。