Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script not understood by Ubuntu Bash

Tags:

bash

ubuntu

I am new to Bash Script. I run the following simple script in Ubuntu like this:

sudo bash -x Script.sh

Output:

+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ adminEmail=$'[email protected]\r'
+ $'\r'
: command not found
' echo 'database name:
database name:
+ read $'dbname\r'

The actual script:

#!/bin/bash

# Installation script for latest Wordpress website on Ubuntu
# 
# Kave
# December 27, 2011


adminEmail="[email protected]"

echo "database name:"
read dbname

What are all these '\r' error messages coming up? Even the comments seem not to be understood...

like image 850
Houman Avatar asked Dec 27 '11 18:12

Houman


2 Answers

Simply do

dos2unix scriptname

and execute your script.

If you don't have the tool you can do the following to install it.

sudo apt-get install dos2unix

OR

Do the following command line hacks to convert the file.

tr -d '\015' < scriptname > scriptname.new

OR

sed -i 's/\x0D$//' scriptname
like image 160
jaypal singh Avatar answered Oct 03 '22 23:10

jaypal singh


You downloaded the file via a Windows machine, and you have CRLF line endings (that's \r\n in C and related languages).

Remove the DOS line endings. For example, edit the file in vim and change the format with :set fileformat=unix (plus Return) and then write the file back out. Alternative techniques could use the tr command, or dos2unix or dtou, depending on what's available.

like image 43
Jonathan Leffler Avatar answered Oct 04 '22 01:10

Jonathan Leffler