Bash IF statement: string comparison of command output is always false

Hi, I am trying to write a simple bash script to join a screen session. If the session is not already running, it will start it and try to connect again.

The problem I encountered is related to the if statement; it should compare the output of the screen command with the error message, and if they are equal, start a session and attach. But it always goes to the else clause, printing out the error message that I checked: s

Both lines contain the same thing:

"There is no associated screen matching sctest."

but bash thinks they are different ...

Here's a bash script, what am I missing?

#!/bin/bash

screenOutput=$(screen -aA -x sctest);
failString="There is no screen to be attached matching sctest.";

if [ "$screenOutput" = "$failString" ]; then
    echo "screen session not started. starting now...";

    # . ./init.sh

    # echo $(screen -aA -x sctest);
else
    echo "$screenOutput";
fi
+4
source share
1

set -x ( invoke bash -x yourscript), , .

, - :

[ 'There is no screen to be attached matching sctest' = $'There is no screen to be attached matching sctest\r' ]

... , a \r . .

, :

screenOutput=$(screen -aA -x sctest)
screenOutput=${screenOutput%$'\r'}
+3

Source: https://habr.com/ru/post/1627370/


All Articles