Home bash Bash error: syntax error: unexpected end of file

Bash error: syntax error: unexpected end of file

Author

Date

Category

I receive

syntax error: unexpected end of file

when trying to execute a script. What’s wrong with the script? (checked in https://www.shellcheck.net/ like everything is green)

#! / bin / sh
PROCESS_COUNT = $ (ps aux | grep my-program | grep -v grep | awk '{print $ 2}' | wc -l)
if [$ PROCESS_COUNT! = 0]; then
  ps aux | grep my-program | grep -v grep | awk '{print $ 2}' | xargs kill -9
fi
FINAL_RESULT = $ (ps aux | grep my-program | grep -v grep | awk '{print $ 2}' | wc -l)
if [$ FINAL_RESULT! = 0]; then
  echo "error thrown"
fi

Answer 1, authority 100%

Here is the solution, the problem was that the text editor assigned characters \ r \ n instead of just \ r as required by unix.
The solution is taken from here reply with stackofflow

Here is how to do this using notepad ++

how to remove \ n using notepad ++


Answer 2, authority 100%

Instead of counting the number of processes as insurance against calling kill without arguments, it is much more convenient to use the while read loop, because if there are no items in the process list, it simply will not start. For example:

#! / bin / bash
my_program = sublime_text # for example
while read -r pid
do
  kill -9 "$ {pid}"
done & lt; & lt; (awk '$ 11 ~ my_program {print $ 2}' my_program = "$ {my_program}" & lt; & lt; (ps aux))

Answer 3

in my case, replacing tabs with a double space saved me

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions