Q:

The terminal is freaking out with my .sh character encoding. What can I do?

I have this SH file:
echo "KILLING OLD SERVER SESSION"
kill $(cat lastPID.txt)
sleep 10
echo "STARTING SERVER..."
java -jar my.cute.little.server.jar </dev/null>/dev/null 2>&1 &
echo $! > lastPID.txt
echo "SERVER STARTED! PID:" $(cat lastPID.txt)

This creates a new process, stores it's PID in a txt file, so that later it can close it before launching other process. The code works, that is not the problem.
The issue is that when I copy it from my PC to the server (using filezilla) and I run it I get the following:
KILLING OLD SERVER SESSION
restart_server.sh: 2: kill: Illegal number:
sleep: invalid time interval `10\r'
Try `sleep --help' for more information.
STARTING SERVER...
: not foundver.sh: 5: restart_server.sh:
SERVER STARTED! PID:

Notepad++ states I am using UTF-8 w/o BOM:

$ file restart_server.sh
restart_server.sh: ASCII text, with CRLF line terminators
$ file --mime-encoding restart_server.sh
restart_server.sh: us-ascii

So, for which sin am I being punished here? What am I missing?
Thanks!

A:

Backticks are not string quotes. They run their contents as a command and then replace themselves with the output. You want single quotes or double quotes for strings.
So
sleep `10`

is erroring because 10 is not a command and then sleep gets no argument, etc.
Similarly
echo `$! > lastPID.txt`

is trying to run $! > lastPID.txt as a command which will create an empty lastPID.txt file and then error because the pid is not a command. Then echo will echo the empty string.
You want any of
sleep 10
sleep '10'
sleep "10"

and
echo "$!" > lastPID.txt

Among other similar changes.
You also need to save the file with Unix newlines (and not DOS line endings). The encoding itself doesn't matter as much (though UTF-8 is generally a good choice) but you do not want a BOM.

