Syntax error: unexpected end of file. Bash - bash

I want to set up a teamspeak bot, and I have this script to start this.
#!/bin/bash
if [ $1 = 'stop' ]
then
echo stop >> /root/ts3bot/tmp/log.txt
date >>/root/ts3bot/tmp/log.txt
echo ======================
screen -S bot -X quit
fi
if [ $1 = 'start' ]
then
echo start >> /root/ts3bot/tmp/log.txt
date >> /root/ts3bot/tmp/log.txt
echo ======================
screen -dmS bot php core.php
ps ax | grep -v grep | grep -v -i SCREEN | grep links >> /root/ts3bot/tmp/log.txt
fi
<here is an extra blank line>
but when I type bash bot.sh it says syntax error: unexpected end of file
I don't know what I did wrong :/ the chmod is set on 755
Thanks!

I suspect you may have copied this shell script from a Microsoft Windows box over to a Linux or Unix server. If so, the problem might be that you have DOS/Windows line endings, which can cause unpredictable results in scripts.
To check the script for bad line endings on a Linux or Unix server, you can dump the file (sort of like a hex dump) by typing the following at the shell prompt:
$ od -c bot.sh | less
And look for \n or \r or \r\n. If lines appear to have a \r at the end, then you've found the problem.
To FIX this line-ending problem, you can use a tool like dos2unix if it's installed on your system. If you don't have dos2unix but you're on a Linux server, you may be able to do this instead:
$ sed -i 's/\r//' bot.sh
to convert the file.
Lastly ... see the first line of the script, #!/bin/bash? Because of that, you don't need to run this with bash bot.sh, you can just execute it directly with ./bot.sh.

Related

Invalid bash script when execute it in Ubuntu Server 18.04 through SSH terminal [duplicate]

I have a script that I'm trying to run but I just get the error ": command not found" whenever I try to run it. Here's what I've tried to do to fix it:
Made sure the hashbang is correct "#!/bin/bash"
Run dos2unix on the file
Run the script as scriptname.sh, ./scriptname.sh, and /bin/bash scriptname.sh
chmod 755 scriptname.sh
I still am unable to run the script. Any help is much appreciated!
This is caused by carriage returns. Here's the excerpt from the bash tag wiki:
Check whether your script or data has DOS style end-of-line characters
Use cat -v yourfile or echo "$yourvariable" | cat -v .
DOS carriage returns will show up as ^M after each line.
If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'
Make sure to check all your data, and not just the script itself.
You can use these to delete unnecessary characters:
tr -cd '[:alnum:][:blank:][:punct:]\n' < script.sh > new_script.sh
Or
tr -cd '[:graph:][:blank:]\n' < script.sh > new_script.sh
Then try new_script.sh.

Getting error ": command not found" when trying to run shell script

I have a script that I'm trying to run but I just get the error ": command not found" whenever I try to run it. Here's what I've tried to do to fix it:
Made sure the hashbang is correct "#!/bin/bash"
Run dos2unix on the file
Run the script as scriptname.sh, ./scriptname.sh, and /bin/bash scriptname.sh
chmod 755 scriptname.sh
I still am unable to run the script. Any help is much appreciated!
This is caused by carriage returns. Here's the excerpt from the bash tag wiki:
Check whether your script or data has DOS style end-of-line characters
Use cat -v yourfile or echo "$yourvariable" | cat -v .
DOS carriage returns will show up as ^M after each line.
If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'
Make sure to check all your data, and not just the script itself.
You can use these to delete unnecessary characters:
tr -cd '[:alnum:][:blank:][:punct:]\n' < script.sh > new_script.sh
Or
tr -cd '[:graph:][:blank:]\n' < script.sh > new_script.sh
Then try new_script.sh.

Bash script - Run commands that correspond to the lines of a file

I have a file like this (text.txt):
ls -al
ps -au
export COP=5
clear
Each line corresponds at a command. In my script, I need to read each line and launch each command.
ps: I tried all these options and with all of them I have the same problem with the command "export". In the file there is "export COP=5", but after running the script, if I do echo $COP in the same terminal, no value is displayed
while IFS= read line; do eval $line; done < text.txt
Be careful about it, it's generally not advised to use eval as it's quite powerful and as easy to be abused.
However, if there is no risk of influence from unprivileged users on text.txt it should be ok.
cat test.txt | xargs -l1 bash -c '"$#"' echo
In order to avoid confusion I would simply rename the file from text.txt to text and add a shebang (e.g. #!/bin/bash) as the first line of the file. Make sure it is executable by calling chmod +x text. Afterwards you can execute it as expected.
$ cat text
#!/bin/bash
ls -al
ps -au
clear
$ chmod +x text
$ ./text

Issue with scheduling in Linux

I scheduled a script using at scheduler in linux.
The job ran fine but the echo statements which I had redirected to a file are no where to be found.
The at scheduling command is as follows:
at -f /app/data/scripts/func_test.sh >> /app/data/log/log.txt 2>&1 -v 09:50
Can anyone point out what is the issue with the above command.
I cannot see any echo statements from the script in the log.txt file
To include shell syntax like I/O redirection, you'll need to either fold it into your script, or pass the input to at via standard input, like so:
at -v 09:50 <<EOF
sh /app/data/scripts/func_test.sh >> /app/data/log/log.txt 2>&1
EOF
If func_test.sh is already executable, you can omit the sh from the beginning of the command; it's there to ensure that you are passing a valid command line to at.
You can also simply ensure that your script itself redirects all its output to a specific log file. As an example,
#!/bin/bash
echo foo
echo bar
becomes
#!/bin/bash
{
echo foo
echo bar
} >> /app/data/log/log.txt 2>&1
Then you can simply run your script with at using
at -f /app/data/scripts/func_test.sh -v 09:50
with no output redirection, because the script itself already redirects all its output to that file.

how to log all the command output to one single file in bash scripting [duplicate]

This question already has an answer here:
Output bash script into file (without >> )
(1 answer)
Closed 9 years ago.
In gnu/Linux i want to log all the command output to one particular file.
Say in terminal,i am typing
echo "Hi this is a dude"
It should print in the file name specified earlier without using the redirection in every command.
$ script x1
Script started, file is x1
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit
Script done, file is x1
Then, the contents of file x1 are:
Script started on Thu Jun 13 14:51:29 2013
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit
Script done on Thu Jun 13 14:51:52 2013
You can easily edit out your own commands and start/end lines using basic shell scripting (grep -v, especially if your Unix prompt has a distinctive substring pattern)
Commands launched from the shell inherit the file descriptor to use for standard output from the shell. In your typical interactive shell, standard output is the terminal. You can change that by using the exec command:
exec > output.txt
Following that command, the shell itself will write its standard output to a file called output.txt, and any command it spawns will do likewise, unless otherwise redirected. You can always "restore" output to the terminal using
exec > /dev/tty
Note that your shell prompt and text you type at the prompt continue to be displayed on the screen (since the shell writes both of those to standard error, not standard output).
{ command1 ; command2 ; command3 ; } > outfile.txt
Output redirection can be achieved in bash with >: See this link for more info on bash redirection.
You can run any program with ported output and all its output will go to a file, for example:
$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
So, if you want to open a new shell session with ported output, just do so!:
$ bash > outfile
will start a new bash session porting all of stdout to that file.
$ bash &> outfile
will port all of stdout AND stderr to that file (meaning you will no longer see prompts show up in your terminal)
For example:
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$
$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$
If you want to see the output and have it written to a file (say for later analysis) then you can use the tee command.
$ echo "hi this is a dude" | tee hello
hi this is a dude
$ ls
hello
$ cat hello
hi this is a dude
tee is a useful command because it allows you to store everything that goes into it as well as displaying it on the screen. Particularly useful for logging the output of scripts.

Resources