字符串比较是shell脚本

1 bash shell

我有一个方案将文件从一个服务器复制到另一个服务器,因为我需要检查任何现有的scp正在进行中,编写了一个示例shell脚本但是条件不满足,即使语法正确,这里的主要问题是ps命令的输出将存储在变量中scpstat,并且相同的if语句中的匹配字符串进行比较,这里我得到的变量输出不同于在脚本之外执行.可以看到它在执行时的脚本执行格式不同sh -x scpsamp.sh,为什么有" sh"附加到输出,但在没有ps和分配时,因为scpstat='scp'我能够得到条件正确,我做错了什么时候输出到变量.请帮忙

#!/bin/sh
scpstat=`ps -ef | grep scp | egrep -v 'grep|ssh' | awk '{print $8}')`  
if [ "$scpstat" = "scp" ];  
then  
echo "SCP is in progress"  
else  
echo "No SCP in progress"  
fi  
Run Code Online (Sandbox Code Playgroud)

sh -x输出

pjh*_*pjh 5

从输出中提取信息是非常困难的ps.如果你的系统有pgrep,那就容易多了:

if pgrep scp >/dev/null
then
    echo "SCP is in progress"
else
    echo "No SCP in progress"
fi
Run Code Online (Sandbox Code Playgroud)