0

I have a .py code that gives me a number which I want to save to some shell variable because I need to use a if/fi loop on that variable. How can I do that? My a.py gives me 1.0 and my condition is sth like: if $x <= 1.5 do sth

I need the .py output to go to $x.

  • 1
    It sounds like you're asking [How can I assign the output of a command to a shell variable?](https://unix.stackexchange.com/questions/16024/how-can-i-assign-the-output-of-a-command-to-a-shell-variable) - however, be aware that `bash` itself can really only perform integer arithmetic – steeldriver Nov 11 '19 at 23:56
  • For comparing fractional numbers, you'll need a different tool, like bc: `x=$(python code.py); if (( $(echo "$x <= 1.5" | bc) )); then ...` – glenn jackman Nov 12 '19 at 01:58
  • I tried: x=$(python code.py) ; echo $x. I get error message: command not found. – RAJENDRA THAPA Nov 12 '19 at 02:45
  • Are you certain? I just tried the following and it works fine for me: `x=$(python3 -c 'import sys ; print(sys.version[0:3])'); echo $x` – user1404316 Nov 12 '19 at 03:38
  • @RAJENDRATHAPA The command substitution (the `$(...)` bit) in that comment should contain the shell code necessary to invoke your Python script. So use the command in there that you would normally use to run the Python script. – Kusalananda Nov 12 '19 at 07:43

0 Answers0