关于您的“但是执行此操作会给出[: 1: !=: unexpected operator
”问题,这是一个解决方案:
使用bash -c
而不是sh -c
. bash 程序在处理方括号方面似乎比 sh 更好。例如:
ubuntu@ubuntu:/$ echo "antipetalous" | if [[ "antipetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi
match
ubuntu@ubuntu:/$ echo "antepetalous" | if [[ "antepetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi
no
ubuntu@ubuntu:/$ sh -c 'echo "antipetalous" | if [[ "antipetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi'
sh: 1: [[: not found
no
ubuntu@ubuntu:/$ bash -c 'echo "antipetalous" | if [[ "antipetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi'
match
ubuntu@ubuntu:/$ bash -c 'echo "antepetalous" | if [[ "antepetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi'
no
ubuntu@ubuntu:/$ sh -c 'echo "antepetalous" | if [[ "antepetalous" =~ "ti" ]]; then echo "match"; else echo "no"; fi'
sh: 1: [[: not found
no
ubuntu@ubuntu:/$
所以ssh -t $HOST -p 22 -l deehem "sh -c 'if [ "" != ...
会变成ssh -t $HOST -p 22 -l deehem "bash -c 'if [ "" != ...
.
感谢 pLumo 在https://askubuntu.com/questions/1310106/sh-c-sh-not-working-when-using-if-statement-and-having-in-the-filena(“命令行 - sh - c '...' sh {} 在使用 if 语句并且文件名中有 ' 时不起作用 - Ask Ubuntu")。