added a line to an existing shell script and - bash

now I get a bizarre error as if I have changed/lost a closing argument such as fi, statement closure such as , or a hidden series of tabs.
This is the message I get in my err file:
./cron_run.sh: line 156: syntax error near unexpected token `else'
./cron_run.sh: line 156: ` else'
Again, I did not touch these lines. Not even close and what I added was another operation to dump a mongo collection to the backup directory. So I had:
...start of script
mongodump... # this was existing
mongodump... # this was the addition
...rest of script (about 70) lines unchanged
Key point:
the above lines worked
the code/server executed as expected
the error/crash occurred at the end of the process after execution (so the whole thing actually worked!)
I looked at the code with syntax highlighting (vim, nano) and I cannot see anything wrong with it (at least not an obvious thing such as a bracket, fi or missing back tick)!
Any suggestions?

Related

error in sudo config, need help to fix

My shell give me this error when trying to do a sudo
/etc/sudoers: syntax error near line 30 <<
I did not modify this file...
anyway the 30th line is the very end of the file.
I paste here the last 3 rows, from 27 to 30
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
please help me to understand the problem and fix it.
I am a newbie on linux.
If the 30th line is the last one it might indicate that you have problems in the file somewhere along the way.
The parser has reached the end of the file and is telling you there is an error (might be looking for unclosed brackets of any kind etc).
If you can parse the full file or try to read it and go line by line to figure out where is the error.

Here document gives EOF error in Ruby IO

The following code give two errors which I am not able to resolve. Any help would be appreciated:
random.rb:10: can't find string "TEMPLATE" anywhere before EOF
random.rb:3: syntax error, unexpected end-of-input
Code:
id = 2
File.open("#{id}.json","w") do |file|
file.write <<TEMPLATE
{
"submitter":"#{hash["submitter"]}",
"quote":"#{hash["quote"]}",
"attribution":"#{hash["attribution"]}"
}
TEMPLATE
end
From the documentation (emphasis mine):
The heredoc starts on the line following <<HEREDOC and ends with the next line that starts with HEREDOC
Your code doesn't contain a line starting with TEMPLATE. If your text editor (or IDE) supports regular expressions in searches, try ^TEMPLATE.
You can either remove the spaces or if you want to keep them, change <<TEMPLATE into <<-TEMPLATE. The addition of - instructs the Ruby parser to search for an (possibly) intended TEMPLATE like you have in your code.

'no such file or directory' on a file that isn't accessed

I'm writing a small Ruby script that does a statistical analysis on a list of names generated by another script of mine.
When I run it with this command:
ruby [first script] [args] | ruby -- [second script] _
it throws this error:
./name_gen_test.rb:15:in `gets': No such file or directory # rb_sysopen - _ (Errno:ENOENT)
from .name_gen_test.rb:15:in `gets'
from .name_gen_test.rb:15:in `<main>'
(Apologies for typos; Powershell wouldn't let me copy/paste)
This is line 15:
until (cur_line = gets).nil?
Then there's the body of a loop, the rest of the code, etc. However, if I put this line:
gets
as the very first line, I get the same error. In fact, if I totally empty the file and have nothing but a call to gets, I get the error that the file '_' cannot be found.
How can I make it understand that '_' is a command line argument and not a file to be... read from, I guess? Why doesn't gets work like I expect it to (i.e. reading from the standard input)?
I'm running it with Powershell, if that makes a difference.
Sorry if this is a duplicate; simply Googling the error message leads to a dozen different issues and a dozen different solutions, none of which apply, and I couldn't figure out how to put this problem into a Google query.
STDIN.gets will do what you want. By default, gets is (pretty much) equivalent to ARGF.gets. ARGF reads from standard input if there are no ARGS, and from files that correspond to ARGS if there are.

bash: syntax error near unexpected token `else' formatting issue?

I'm pretty new to bash and am having trouble with what I assume is formatting. I'm trying to edit the /etc/profile so it will display a login message for root and a different login message for anyone else. But I'm getting the error bash: syntax error near unexpected token else. I've tried all the different combinations of no semicolon, then on the next line etc but always get the same error. I've tried the lines separately and they display fine (except $HOSTNAME, can't get that to work). When run like this and login with root it will just jump to "Welcome $USER...".
Anyone suggestions would be appreciated!
if [ "$UID" -ne 0 ]; then
echo -e "\033[40;37;7m Danger! Root is doing stuff in `pwd`\033[0m"
else
echo "Welcome $USER! You are working on `$HOSTNAME` in `pwd`."
fi
As written, above works for me - but, as pointed out, you should change your test to -eq 0.
For the syntax error near unexpected token problem - I will guess that your file contains embedded 'control codes', i.e. most likely a carriage return \r.
Try:
cat -e ~/your_profile
see any non-printable characters? if so, remove them (cat options may vary - check you manpage) or
od -c ~/your_profile

syntax error near unexpected token `

The following script is intended to run the program "senna" on all files in a directory and write the output for each file (preserving the input file name) into another directory
for file in ./Data/in/*;
do
./senna -iobtags -usrtokens -posvbs -srl < $file > ./Data/out/$file
done
On trying to execute the script, the following error arises.
-bash-4.0$ sh run.s
'un.s: line 1: syntax error near unexpected token `
'un.s: line 1: `for file in ./Data/in/*;
The script has the lines of code exactly as above and there is no `. Perhaps it implies something else. Help with error resolution would be appreciated.
The line endings in the script are wrong. Pass it through dos2unix to eliminate the CRs.

Resources