[python] Comparing a variable with a string python not working when redirecting from bash script

First I'll show you both the bash and python script (both are in the /bin directory on my mac):

The bash script (esh_1):

#! /bin/bash  echo -n "Enter bash or natural-language command: " read INPUT echo $INPUT > ~/USER_INPUT.txt $INPUT if (( $? )); then echo Redirected to Python Script; esh_2; cat ~/USER_INPUT.txt; else echo Did not redirect to Python Script;  fi esh_1 

The python script (esh_2):

#! /usr/bin/python2.7  with open('/Users/bendowling/USER_INPUT.txt', 'r') as UserInputFile:     UserInput = UserInputFile.read()  UserInputFile = open('/Users/bendowling/USER_INPUT.txt', 'w+')  if UserInput == 'List contents':     UserInputFile.write("ls") else:     print "Didn't work"  UserInputFile.close() 

The bash script takes the user's input, stores it in a temporary file called USER_INPUT.txt, and checks if it runs correctly. If it doesn't, it calls esh_2 (the python script) which reads the USER_INPUT.txt file, taking the user's input. It then checks if it's equal to the string "List contents". If it is, then it writes "ls" to the text file. It then closes the file. The bash file then cats the command stored in the text file (in the future I will make it run it as a command). The script then starts again.

The problem is that when I enter "List contents" into the shell, it doesn't work, thus printing "Didn't work". However, if I go into the text file myself and write "List contents", the python script works and writes "ls" to the text file. I have no clue why this is happening. I would gladly appreciate any help on this matter.

Thanks, b3n

This question is related to python bash shell

The answer is


When you read() the file, you may get a newline character '\n' in your string. Try either

if UserInput.strip() == 'List contents': 

or

if 'List contents' in UserInput: 

Also note that your second file open could also use with:

with open('/Users/.../USER_INPUT.txt', 'w+') as UserInputFile:     if UserInput.strip() == 'List contents': # or if s in f:         UserInputFile.write("ls")     else:         print "Didn't work" 

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"