1

I am having errors where I am putting in code that should be working fine into a shell file and when I run it, it fails to run correctly. The error I am receiving from this is confusing me as well somewhat. Here is what I have:

echo "Please enter your student ID: "
read username
echo "Please enter your MySQL password: "
read -s password
db="db$username"
echo "DB username is $db"
$(mysql -D$db -u$username -p$password -se "CREATE TABLE...")
echo "That has been completed"

The SQL command has been truncated as this appears to have no relevance for the error.

If I run this code through putty I get this:

*******@csl-*******:~/tasks/SQL$ ./generate.sh
Please enter your student ID:
': not a valid identifierad: `username
Please enter your MySQL password:
': not a valid identifierad: `password
DB username is db
ERROR 1045 (28000): Access denied for user '-p'@'localhost' (using password: NO)
./generate.sh: line 7: $'\r': command not found
That has been completed
*******@csl-*******:~/tasks/SQL$

(The * represent hidden values)

I am not sure what the error is with this code here. I have tried under different circumstances such as using a Mac to create the file with exactly the same file on and run it through terminal and it works fine (off the same Linux server). However I also went back to the same Mac to try and re-run the code and got the same error as shown here - even though the code hasn't changed.

Some background information: Using Putty for SSH access, Filezilla for FTP, Sublime Text for code editor. Connecting to a remote Linux server to run from.

1 Answers1

0

I think this is due to having the wrong line ending character, which would be the case if you are running sublime in windows.

Try running od -xcb to find the carriage return characters. Unix expects just \n whereas you will see \r\n with windows carriage returns.

You can fix your file by running dos2unix generate.sh.

As per Dannys comment below you can change the settings in sublime by going to view > line endings > unix.


Examples:

I copied your script and created a file using vim with unix carriage returns called script.sh. I then used unix2dos to create a file with windows carriage returns for testing and was able to recreate your error.

Unix carriage returns:

od -xcb script.sh
0000000    2123    622f    6e69    622f    7361    0a68    6365    6f68
      #   !   /   b   i   n   /   b   a   s   h  \n   e   c   h   o

Windows carriage returns:

od -xcb windows.sh
0000000    2123    622f    6e69    622f    7361    0d68    650a    6863
      #   !   /   b   i   n   /   b   a   s   h  \r  \n   e   c   h

And the error from the windows script:

> . windows.sh

Please enter your student ID:
': not a valid identifier
Please enter your MySQL password:
': not a valid identifier
DB username is db12
That has been completed
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
  • Thank you and everyone else for your help. This has now solved the issue. Unfortunately dos2unix isn't installed on the system. However you can, on sublime, go to view > line endings > unix and then it will save correctly. – Danny Franklin Jan 05 '18 at 19:27
  • I've added that information above in case anyone else encounters the same issue with sublime. – Thomas Smyth - Treliant Jan 05 '18 at 19:32