1

Following an example I have been provided, I'm reading a result from another script to determine course of action.

isOK=$(somepythonscript.py --someParametersHere)

if [ "$isOK" = true ]; then

When I echo value of isOK (echo "isOK=$isOK"), I see isOK=true and isOK=false, depending on arguments I pass.

Would it be possible to collapse this into one statement to this effect?

if [ $(somepythonscript.py) = true]; then

I have been reading about shell scripting and my understanding is that = true isn't a test of a boolean value and more of a misunderstood effect that people assume is working like a boolean. If this is safe, I can bet on this, as well. But if there is a better way to test response of this other Python script, I am open to it. Please note, that I cannot change the other Python script.

5
  • Does somepythonscript.py actually print out the string true or are you thinking of Python' True? I mean, are you talking about matching a specific string or do you want to look at the script's exit status and if it succeeded? Are you just looking for if somepythonscript.py; then "echo worked"; else echo "failed"; fi?
    – terdon
    Mar 17, 2023 at 19:52
  • I don't have the source code for the Python script. I test it with different arguments and echo what is assigned to isOK. Sometimes I get isOK=true, sometimes isOK=false. So I'm wondering if I can remove this isOK variable and still test the result. What I have shown is a working example but I'm hoping to remove the isOK, as it appears redundant.
    – user565728
    Mar 17, 2023 at 20:19
  • And what happens when you try it? I mean, yes, var=$(command); [ $var = whatever ]; is exactly equivalent to [ $(command) = whatever ], but since simply trying it would demonstrate that, I am guessing you are asking for something more.
    – terdon
    Mar 17, 2023 at 21:02
  • I have made this change and want to test it but still don't have my pull request approved. So in the meantime, I wanted to ask experienced admins if this is looking good or if I'm wasting time waiting for deployment. I think you're telling me there's a good chance this will work.
    – user565728
    Mar 17, 2023 at 21:42
  • You'll have to be careful since true is a Unix command so depending on the context in which you use true in your code it'll be that command or it'll be a literal string. Ditto for false.
    – Ed Morton
    Mar 18, 2023 at 11:20

1 Answer 1

3

Yes, you can do this. The construct $(command) is a way of treating the output of command as a variable, so it is essentially equivalent to var=$(command) and then using $var. So if you don't need to keep the output, you don't need a variable and this is enough:

if [ "$(command)" = "true" ]; then
  # do your thing
fi

Just remember that true and false are just strings as far as the shell (bash, in your case) is concerned. They don't have any special meaning, unlike True and False in Python.

Finally, the more common idiom for this sort of thing is to use a command's exit status which is 0 on success and non-0 on failure. So, if your Python script is written well and exits with a non-0 status on failure, it might even be enough to simply do:

if somepythonscript.py --someParametersHere
then
  echo "The script worked!"
else
  echo "the script failed!"
fi
3
  • Regarding true and false not having any special meaning - there are Unix commands with those names so they do have a special meaning and the OP will have to be careful of the context in which they use them to avoid potentially cryptic errors.
    – Ed Morton
    Mar 18, 2023 at 11:23
  • @EdMorton well yes, but that isn't really relevant here. That's like saying that the string cd has a special meaning in the shell because it is also the name of a builtin. The point is that the shell uses 0 and 1 in the way that Python uses True and False, so the strings true and false have no special meaning. Yes, there are the builtin true and false commands in bash, and other shells, as well as the binaries /sbin/true and /sbin/false but those are completely different things.
    – terdon
    Mar 18, 2023 at 19:34
  • It's relevant if/when the OP decides to use true or false in some other context not knowing it's a command and gets a cryptic result. If they didn't have a special meaning then you could just type "true" or true at the command prompt or in any other context and get a similar result to just typing "foo" or foo but that's not the case.
    – Ed Morton
    Mar 19, 2023 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .