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

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.

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.

Looping through output of docker bash command

I am trying to loop through a docker bash command like this:
sudo docker exec -it my-container bash -c "cd sites && ls" > output.txt
which gives me expected output, when I loop through it as: cat output.txt | while read line do echo "abcd${line}def"; done
it gives me output as:
defdfile1
defdfile2
in short it is overlapping after file name
If I do all this without the docker command like only ls on my host it works fine, how to resolve this issue?
When using the -t flag docker outputs to standard out with added carriage return character.
It shows as ^M with cat -vte output.txt. (-v, --show-nonprinting shows non printing characters using ^ or M- notation)
This carriage return breaks the formatting and the lines are not processed correctly. Remove the -t flag from the command to get the correct result.

Syntax error: unexpected end of file. 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.

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

Executing Shell Script by adding env variables

#!/bin/bash
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
export HIVE_AUX_JARS_PATH=/home/hadoop/lib/HiveUDF.jar
hive -S -e 'set mapred.job.queue.name=hdmi-technology'
hive -S -e 'SELECT count(*) from testingtable2' > attachment.txt
Whenever I try to run the above shell script(count.sh) like below, I always get errors, I have no idea what wrong I am doing as I am new to shell script and I am not sure how I can add environment variables in shell script.
bash-3.00$ sh count.sh
count.sh: HIVE_OPTS= -hiveconf mapred.job.queue.name=hdmi-technology^M: is not an
identifier
Is there anything wrong I am doing in my shell script, by the way I am adding environment variables in first two lines? Any help will be appreciated.
After all the changes I did as per the below comments,
mv count.sh count,
chmod +x count,
./count
whenever I try to do this in my prompt directly export HIVE_AUX_JARS_PATH=/home/hadoop/lib/HiveUDF.jar it works fine, but whenever I try to add this in my shell script as mentioned in my question, I always get 'java.io.FileNotFoundException(File file:/home/hadoop/lib/HiveUDF.jar does not exist. Why is it so?
You're running a bash script with a sh command that is not bash. Run it with ./count.sh so the shebang line will take effect, or just say bash count.sh.
You edited your shell script on a DOS or Windows machine, which uses the CRLF pair (\r\n) instead of the Unix-style LF ('\n') line endings.
The ^M is the tell-tale -- it is a \r carriage return character.
This should fix it:
tr -d '\r' < count.sh > count.sh.fixed
mv count.sh.fixed count.sh
Another option:
sed -i 's/\r//g' count.sh

Resources