-1

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.

enter image description here

enter image description here

Pratik Dash
  • 81
  • 1
  • 10
  • 1
    It's impossible to say without more information. What exactly is line 4? If you save the original vs fixed versions of the script and compare them with `diff`, what does it show? Can you create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) showing the problem? – Gordon Davisson Jan 14 '23 at 03:50
  • Do you really expect us to be magically able to somehow guess the code you are talking about? – arkascha Jan 14 '23 at 09:44
  • Replace `echo Guess (something like this)` with `echo "Guess (something like this)"`. – Walter A Jan 14 '23 at 11:21
  • OK. I see that I have irritated many people by my question. But be assured, there is nothing more that I could have mentioned, i.e., the code that could have provided more helpful information. I mean I can write the code but every editor formats it the same way and I'm not able to note any error in the code. @WalterA I was already aware of that and no that didn't solve my issue. – Pratik Dash Jan 14 '23 at 14:17
  • But @GordonDavisson, a huge thanks for the tip. It was stupid of me to not think of diff first. Turns out there were extra ^M (Ctrl + M) characters in the bash script files that I was generating from my python code. This was added when I was adding a new line after writing a line in the bash script. Somehow pycharm fixes it automatically while saving it after uncommenting. And it had nothing to do with the echo statements. It's just that the ^M characters are removed by pycharm. They were not visible in any other text editor. Let me know if this question was too stupid and I will remove it. – Pratik Dash Jan 14 '23 at 14:22

1 Answers1

-1

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.

Pratik Dash
  • 81
  • 1
  • 10