'shell script ambiguous redirect [duplicate]
I am trying to create simple shell script which will identify difference between output of two text files. I am able to successfully run script when I redirect to file (i.e. > a and > b).
What I am trying below is to redirect output to variable instead of file to avoid unnecessary file creation but I am getting error ambiguous redirect.
Can someone educate me how to fix this error?
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
I have also tried other way but get different error
$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
But individual commands are working fine when executed from shell prompt
"sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" |awk '{print $1, $2}'"
also tried below from command prompt but fails in script
diff <(sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/msmscgateway |grep "CIMD2:" |awk '{print $1, $2}') <(sed -n '/eTopUp MU(55) Gateway Status1:/,/eTopUp MU(60) Gateway Status:/p' /root/scripts/last/msmscgatewaylast |grep "CIMD2:" |awk '{print $1, $2}') get below error test4.sh: line 1: syntax error near unexpected token `('
Solution 1:[1]
You can do it using command substitution
foo=$(sed 'bar' baz.txt)
read foo < <(sed 'bar' baz.txt)
or with Bash 4.2 you can use
lastpipe
shopt -s lastpipe
sed 'bar' baz.txt | read foo
Solution 2:[2]
Your:
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
should really be:
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}')
To put the output into variables. Right now it probably complains because $a and $b in your case translates to nothing when it expands.
However as someone mentions, this probably won't work with diff $a $b.
Solution 3:[3]
No need for variables at all if you don't want to (in Bash at least):
diff <(echo -e "a\nb\nc") <(echo -e "a\nc\nc")
Basic syntax is
diff <(CMD1) <(CMD2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zombo |
| Solution 2 | Jite |
| Solution 3 | Kjell Andreassen |
