0

I have a shell script that calls a python script. The python script returns a variable "Found" back to the main shell. While I am able to display the result variable in main shell, the variable is not being recognized in conditions.

#!/bin/bash
PythonScript="C:\Users\arun\Test.py"
Pythonresult="$(py "${PythonScript}")"
Varuableresult="Found"

echo "Display both the results here..."

echo "Variable result is..." $Varuableresult
if [ "$Varuableresult" = 'Found' ]; then
    echo "Variable result in condition working"
fi

echo "Python result is..." $Pythonresult
if [ "$Pythonresult" = 'Found' ]; then
    echo "Python result in condition is working"
fi

The python script (Yes, it's just one line):

print("Found")

The output:

$ sh 'C:\Users\myusername\Documents\Value_Adds\FTP_Filecheck\Test.sh'
Display both the results here...
Variable result is... Found
Variable result in condition working
Python result is... Found

Why does the variable is not recognized in the condition?

I'm executing the shell script in Cygwin

Gerhard
  • 6,850
  • 8
  • 51
  • 81
Arun
  • 65
  • 1
  • 9
  • 1
    try `echo "Python result is... '$Pythonresult'"` to see what's actually returning. May be there are whitespaces or new lines in the value. – LMC Jun 04 '22 at 18:39
  • 2
    Running `set -x` in your shell script to enable trace-level logging will show you the specific `[` command being run, so you can compare the actual and intended outputs. – Charles Duffy Jun 04 '22 at 19:23
  • 1
    (and to reiterate what wjandrea was saying above about pictures of output... if you pasted your actual output _as text_ we might be able to check whether there's a space after the `Found` before the newline, but with a screenshot there's no possible way to tell; of course, a [mre] is better). – Charles Duffy Jun 04 '22 at 19:23
  • 2
    Please [edit] in the logs of a `set -x` trace (aka `bash -x yourscript`). – Charles Duffy Jun 04 '22 at 19:59
  • 2
    It's _likely_, since you're on Windows, that your Python script's output has DOS newlines, so your string is really `$'Found\r'` instead of `'Found'`, but they look the exact same to `echo`. The trace logs will make it obvious. – Charles Duffy Jun 04 '22 at 20:00
  • 1
    @CharlesDuffy, I used the set -x and looks like you're right. $ sh 'C:\Users\username\Documents\Value_Adds\FTP_Filecheck\Test.sh' Display both the results here... + echo 'Variable result is...' Found Variable result is... Found + '[' Found = Found ']' + echo 'Variable result in condition working' Variable result in condition working + echo 'Python result is...' $'Found\r' Python result is... Found + '[' $'Found\r' = Found ']' – Arun Jun 04 '22 at 20:04
  • 1
    You can use `Pythonresult=${Pythonresult%$'\r'}` to strip that character off -- though it's not guaranteed to work reliably unless you change your script from being a sh script to a bash script (`$'...'` is originally a ksh extension picked up by bash and zsh, but is not a feature found in the POSIX sh standard, so `/bin/sh` doesn't guarantee support for it). – Charles Duffy Jun 04 '22 at 20:07
  • 1
    (similarly, if your script starts with `#!/bin/bash`, you should run `bash scriptname`, not `sh scriptname`, to be doing the same thing the OS does when invoking it) – Charles Duffy Jun 04 '22 at 20:08
  • 1
    as another option, if you use a Python _installed with cygwin_ instead of using a native Windows Python interpreter, that might stop those unwanted characters from showing up in the first place. – Charles Duffy Jun 04 '22 at 20:08
  • Thank you @CharlesDuffy, Please add your last four comments as a separate answer so I can mark it as relevant answer – Arun Jun 04 '22 at 20:10
  • If the question were open I would be closing it as a duplicate of [Are bash scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings); and as it's not open, I can't presently add an answer. – Charles Duffy Jun 04 '22 at 20:11

1 Answers1

1

It's likely, since you're on Windows, that your Python script's output has DOS newlines (CRLF instead of UNIX-standard LF), so your string is really $'Found\r' instead of 'Found', but they look the exact same to echo. Trace logs with set -x will make this obvious. ($'\r' is how bash writes the carriage return character, which when printed sends the cursor back to the far left side of the current line but has no other visible effect).

You can use Pythonresult=${Pythonresult%$'\r'} to strip that character off -- though it's not guaranteed to work reliably unless you change your script from being a sh script to a bash script ($'...' is originally a ksh extension picked up by bash and zsh, but is not a feature found in the POSIX sh standard, so /bin/sh doesn't guarantee support for it).

As another option, if you use a Python installed with cygwin instead of using a native Windows Python interpreter, that might stop those unwanted characters from showing up in the first place.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441