我想创建简单的shell脚本,将确定的两个文本文件的输出之间的差别。我能够成功运行脚本时,我重定向到文件(即>和> B)。
我试图下面是重定向输出变量,而不是文件,以避免不必要的文件创建,但我得到的错误ambiguous redirect
。
谁能教我如何解决这个问题?
sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" | awk '{print $1, $2}' > $a
sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/last/msmscgatewaylast |grep "CIMD2:" |awk '{print $1, $2}' > $b
echo="diff $a $b"
#echo "$DIFF"
test2.sh: line 2: $b: ambiguous redirect
我也尝试过其他方法,但得到不同的错误
$a=`sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" |awk '{print $1, $2}'`
$b=`sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/last/msmscgatewaylast |grep "CIMD2:"|awk '{print $1, $2}'`
echo="diff <$a <$b"
test1.sh: line 1: =CSMSH3: command not found
test1.sh: line 2: =CSMSH3: command not found
但是,从shell提示符下执行时各个命令工作正常
"sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" |awk '{print $1, $2}'"
下面还试图从命令提示符但在脚本失败
DIFF <(SED -n '/ eTopUp MU(55)网关状态1:/,/ eTopUp MU(60)网关状态:/ P'/根/脚本/ msmscgateway | grep的 “CIMD2:” | AWK“{打印$ 1,$ 2 } ')<(SED -n '/ eTopUp MU(55)网关状态1:/,/ eTopUp MU(60)网关状态:/ p'/根/脚本/最后/ msmscgatewaylast | grep的 “CIMD2:” | AWK'{打印$ 1,$ 2} ')得到如下错误test4.sh:第1行:附近意外的标记语法错误'('
您可以使用command substitution做
foo=$(sed 'bar' baz.txt)
read foo < <(sed 'bar' baz.txt)
或者用猛砸4.2可以使用lastpipe
shopt -s lastpipe
sed 'bar' baz.txt | read foo
你的:
sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" | awk {print $1, $2}' > $a
sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/last/msmscgatewaylast |grep "CIMD2:" |awk '{print $1, $2}' > $b
真的应该是:
a=$(sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" | awk '{print $1, $2}')
b=$(sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/last/msmscgatewaylast |grep "CIMD2:" |awk '{print $1, $2}')
为了把输出放到变量。现在它可能抱怨,因为$a
和$b
你的情况转换为没有当它膨胀。
然而有人提到,这可能不会与diff $a $b
工作。
在所有的,如果你不想(Bash中至少)无需变量:
diff <(echo -e "a\nb\nc") <(echo -e "a\nc\nc")
基本语法
diff <(CMD1) <(CMD2)