Bash error while running script

1

1

I have a CentOS 6.5 64-bit dedicated server. The only thing I done on it is yum install java7, so I have not installed any other stuff.

So in the directory /root I made this file (test.sh)

#!/bin/bash
while true
do
    echo "Hey"
        echo "You have five seconds to do 'Ctrl+C' or the while loop will continue."
    sleep 5
done

I know theres nothing wrong with the code, because I have tried some other (From official websites) and I get the same errors.

So If I do:

cd /root
bash test.sh

I get this error

test.sh: line 7: syntax error near unexpected token `done'
test.sh: line 7: `done'

If I do

cd /root
./test.sh

I get this error

-bash: ./test.sh: Permission denied

I have also tried doing this in the directory /home and I get the same errors.

PS. I'm logged in as root via SSH.

user3524823

Posted 2014-06-22T14:10:10.583

Reputation: 185

What does echo $USER say? What does stat -c '%a' "./test.sh" say? Your code above works for me. Did you forget to chmod +x /root/test.sh ? – polym – 2014-06-22T14:12:07.543

$USER says "root" stat -c '%a' "./test.sh" says "644" – user3524823 – 2014-06-22T14:15:16.717

You forgot to add the executable flag. Look at @slhck's answer below. Is the code above the same as it is displayed here? I don't get why you get the line 7 error. – polym – 2014-06-22T14:19:11.273

Its exact the same code. Just copied and pasted it to double check. – user3524823 – 2014-06-22T14:20:58.537

Answers

3

Fixing Permissions

Fairly sure that the script is not executable. For that, you need to set the executable flag for the current user by running:

chmod u+x /root/test.sh

Then you should be able to run it as:

cd /root
./test.sh

That is, if you're currently running as root (check with whoami).

If you want the script to be executable by another user on the system, it needs to be chmod og+x ("others and group exetuable"), however that won't work if the script itself is saved under /root, which isn't readable by other users than root itself.

Fixing Copy-Paste Errors

It seems from your /bin/bash^M error message that you have a Windows CRLF line ending there (\r\n), which should just be a Linux newline (\n).

To remove this, you can run the following on the file:

sed -i 's/\r//' test.sh

Or this:

dos2unix test.sh

slhck

Posted 2014-06-22T14:10:10.583

Reputation: 182 472

When I do ./test.sh I get this error. -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory I did "ls", and the file was there. – user3524823 – 2014-06-22T14:18:29.423

You seem to have a copy-paste error in the hashbang line (the first line in your script). Some unknown character at the end of the line. Erase it completely, including the beginning of the next line, and write #!/bin/bash again. – slhck – 2014-06-22T14:20:00.080

Could it be the line break or something? – user3524823 – 2014-06-22T14:23:54.543

Pretty sure, yeah. I updated my answer with some instructions on how to get rid of it. – slhck – 2014-06-22T14:24:41.307

Thanks! It works now. ´sed -i 's/\r//' test.sh´ maked it! – user3524823 – 2014-06-22T14:26:24.713