Bash script throws syntax error unless echo statements are commented & uncommented - linux

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.

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.

Related

Is there a way to instruct pylatex to not ignore extra spaces (whitespaces) on its own?

I have started using pylatex four days ago to automate report generation (I have no earlier experience in latex as well). The text that I want to enter in the latex report has been generated by an online server and stored in my file as a text file. In the text file, there exist places with more than one simultaneous space for proper formatting and aligning (Protein Sequence Alignment). Hence, while simply trying to use .append, pylatex seems to ignore all the extra spaces on its own. I searched the internet with various relevant terms, but could not find any answer for pylatex. I did find some latex answers, exploring which I tried to incorporate pylatex's NoEscape and explicitly replacing " " (double spaces) with " \space ", it seems to work for some lines as expected, but not for others (maybe some of the places have \ (or some other special characters for latex) which are expected to be meaningful but could not understand what it actually means since it does not even exist (for example \clo). Can someone please suggest a method where I can simply allow the simultaneous existence of the spaces, which is also visible in the final document without the need to think about the existence of a possible special character that needs to be escaped. The following code snippets that I used might be of use to answer/understand my query.
i = 0
with doc.create(Subsection('Details')):
with open(whatcheck_detail, 'r') as fh:
lines = fh.read().split("#")[1:]
for line in lines:
i += 1
line = line.replace(" ", " \space ").replace("#", "\#") + "\n"
if "Note:" in line:
print(i)
# doc.append(TextColor(line))
doc.append(NoEscape(line))
elif "Warning:" in line:
print(i)
# line = "\color{blue} " + line
doc.append(NoEscape(line))
# doc.append(TextColor("blue", line))
elif "Error:" in line:
print(i)
# line = "\color{red} " + line
doc.append(NoEscape(line))
# doc.append(TextColor("red", line))
And the following is the screenshot of the last part of the error report.
Error Description upon running the above script.
Following are the images of what is happening by using the simple pylatex code doc.append(TextColor("color", line)), and what is actually in the text file (which is how I want it to be on PDF generated by latex/pylatex).
What is happening to the text in the output file.
What is in the text file, or how do I want it.
Thank you!

date command is giving erroneous output while using inside rpm spec file

I have to perform some necessary steps before installing my package, such as taking back up of previous datastore snapshot.
For that purpose I'm using a %pre script as follows.
%pre
#!/bin/sh
--------
--------
stamp=`date +%Y%m%d%H%M%S`
echo ${stamp}
-------------
-------------
The output is as follows: 20161103123325OURCE
It is printing some random characters along with date. "OURCE" is not present anywhere in my spec file.
The same script works perfectly as standalone. The platform is CentOS7.
rpmbuild knows a whole set of macros. Apparently a certain macro is defined as:
%S = %SOURCE
I didn't manage to find something that tells rpmbuild not to expand that macro; but there is a way in tricking him not to do so. I know this is a little workaround, but it's the best I could come up with:
stamp=$(date '+%Y%m%d%H%M%''S')
note that I replaced the backticks with the recommanded $() invocation
I just inserted two '' to split the string in two parts; this avoids macro replacement.
If you escape the percent '%' in your date command with a second percent symbol '%%' as described at the following link, that should correct the behavior you're seeing with expanding %S to "OURCE" as you're seeing in your output.
stamp=`date +%%Y%%m%%d%%H%%M%%S`
See section "Writing a Macro" here
http://rpm.org/user_doc/macros.html

Stacked IF NOTs

dd = (re.findall(r'">County<(.*?)</td>', lmth2))
if not dd:
dd = (re.findall(r'<small>Location in <a(.*?)</td>',lmth2)
if not dd:
county.append("")
fin.append(name[o]+';'+address[o]+';'+city[o]+';'+stateorg[o]+';'+county[o]+';'+phone[o]+';'+website[o])
continue
else:
ee = (re.findall(r'title="(.*?) County', dd[0]))
county.append(ee[0])
fin.append(name[o]+';'+address[o]+';'+city[o]+';'+stateorg[o]+';'+county[o]+';'+phone[o]+';'+website[o])
I'm trying to stack IF NOTs together to find the find result. If
If dd does not come up as a match then I want to try the second scenario. If that doesn't come up with a match then I want, for right now, for it to show it as nothing comes up and to set up the line to be saved to the file. If it does come up with a match than I need it continue on find the second level of the search with ee = (re.findall...)
Until I found the second possible scenario to search for everything had been working find but then I found another possible thing I need to look for so I'm trying to add it into the program and I keep getting an Invalid Syntax coming back on the : on the second
if not dd:
This is one that is WAY beyond me. I'm not use to having this trouble with stacked if's when I use to use VB6. Python seems to be handling things a bit differently.
Indentation is syntactically significant in Python. Unlike languages where blocks are determined by tokens like begin and end or { and }, in Python blocks are determined by indents and dedents.
As such, you cannot arbitrarily indent Python code. Whenever Python encounters a line that is indented further than the line above it, it expects that to be the first line of a new block. The issue you have is that inside the first if statement you have already established the indentation level of that block with the line dd = ..., and then you've indented the next if statement even further, while it should be at the same indentation level.
If you remove the extra indent on the second if not dd: line, it should no longer have a syntax error.

Line Break in Python not working

kanye_quote = """My greatest pain\
in life is that I will never be able to see myself perform live"""
I am trying to create a line break but not having any luck. I am using the """ method to allow for multiple lines. It was my understanding that \ would create a new line but when I run the script, I am not getting a new line. Any Help would be terrific in solving this little problem.
If you remove the \ you got a line break. The \ is telling python to ignore the line break. The triple quote method lets you enter string on multiple lines. If you want to enter the string on one line you can use \n to get line breaks.

strings in fortran and filenames using them

here is the trouble ... i'm dynamically building (rather changing)a string which contains numerals(numbers) (like to have filename out01.txt ,out02.txt etc ..)
my program works fine (i'm using the last updated value string to name a file and edit that file) ... but in the same directory with "ls" command, i can see that file created and through file browser i can acess it but from command line using vim , gedit i can't open it new file of same name is opening... moreover i can't remove that file from command line (rm out010.txt'
no such file or directory) here is the code , i might not have been able to explain my problem but code will speak for itself ...
program strtest
implicit none
character(len=1024)::filen,format_str
integer::i
format_str="(a5,i0.3,'.txt')"
do i=1,10
write(filen,format_str)'out',i
end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest
note: i have the same problem even without using trim() function in file opening statement
please explain my situation!!
regards ...
Your filenames are coming out with 2 spaces in front of them, so if you put rm " out01.txt" (2 spaces,out01.txt) you will be able to delete them. It's the a5 that's throwing off the format string.
As #jonsca pointed out already, the problem is with the extra whitespace. The simplest way to get rid of it is to use adjustl, like this:
open(23,file=trim(adjustl(filen)))

Resources