I am writing a bash script to get some information from a remote host, but I got a problem, here is my code:
#!/bin/bash -e
NODE_IP="192.168.136.1"
SSH_NODE="ssh -q -t -t -o StrictHostKeyChecking=no root@${NODE_IP}"
# check stack exists or not
stack=`${SSH_NODE} "openstack stack list |grep test | wc -l"`
echo -e "${stack}"
if [ "${stack}" == "1" ]; then
echo -e "delete existed stack";
fi
echo -e "end"
Output:
1
end
The variable stack
outputs 1 but it doesn't equals to "1", I guess there are some unknown characters behind it, so I tried to trim them by using | xargs
and | tr -d '\n'
, but both of them didn't work.
stack=`${SSH_NODE} "openstack stack list |grep test | wc -l | tr -d '\n' | xargs"`
# or trim after return
stack=`${SSH_NODE} "openstack stack list |grep test | wc -l" | tr -d '\n' | xargs`
If I change the code to "contains" instead of "equals", it will work.
if [[ "${stack}" == *"1"* ]]; then
But I wonder what is the unknown character behind my variable.
I would appreciate any help.