Issue
I have a simple bash script template file that I use to generate new bash scripts from python. Each time I change some values in a variable and then create a new copy of the template from python. After saving the file, I give it executable permission. Let me mention it now that I'm using pycharm as the editor. But when I run this newly generated bash script from the terminal it always gives me the following syntax error:
./job_load_8.sh: line 4: syntax error near unexpected token `('
When I open this newly created bash script file in pycharm and comment all echo statements and save the changes, then the script doesn't throw any errors. And when I go back and uncomment all my previously commented statements, then the script works exactly as expected. Clearly, pycharm is doing some auto-formatting that I'm missing. What is happening here ?
Edit: After many people pointed out that I haven't delved sufficient information, I'm giving more details now. Although, I have solved my issue, I'm mentioning more details in case someone else faces this issue and wants an answer.
Here is the python code that inserts certain lines in the bash script:
# at line number 11, insert a variable GPU_FLAG
lines.insert(11, "gpuFlag = " + str(gpu_flag) + "\r")
# at line number 12, insert a variable NUM_ITER
lines.insert(12, "gpuFlag = " + str(gpu_flag) + "\r")
# get the last set of spot file names from the cohort and save them in a list called spot_file_names separated by commas and add a newline
lines.insert(13, "spotFileNames = [" + ", ".join(["'" + spotFileNames[spot_cohort*(i//spot_cohort)+k] + "'" for k in range(i%spot_cohort+1)]) + "]\r")
And shown below is the result of diff between the two scripts, i.e., before and after commenting and uncommenting.
Solution
Answering here even though I have solved my issue in case someone in the future may be in need of a quick fix. As you can see that there are these extra ^M characters in the original which was not there in the fixed bash script. I have no idea where they come from. But once I remove them, my issue is solved. I don't know how to remove them from the python script so now I just insert after every two lines and that solves my issue. Somehow pycharm (practically every other editor) does this automatically when a file is saved after commenting and uncommenting. At least that's what I'm guessing is happening. Nevertheless, my issue is now solved and I've told why. Hope this helps.
Answered By - Pratik Dash Answer Checked By - Robin (WPSolving Admin)