rebuild
By:
rebuild

How to run the bash scripts file?

October 24, 2014 4.6k views

The following bash scripts can be executed one by one in my debian7 console.

apt-get update
apt-get -y upgrade
apt-get install -y build-essential  gcc  libtool  libxml2-dev libxslt1-dev  openssl  libssl-dev
wget https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz
tar xf Python-3.4.0.tgz -C /usr/local/src/
cd /usr/local/src/Python-3.4.0/
./configure --prefix=/usr/local/python3.4  --with-ensurepip=install   
make 
make  install
ln -s /usr/local/python3.4/bin/python3.4   /usr/bin/python3.4

Why can't be executed by the command bash -x /root/initial.sh when i save all the command in the file /root/initial.sh?

root@online:~# . /root/initial.sh

E: Invalid operation update  
E: Invalid operation upgrade  
Reading package lists... Done  
Building dependency tree  
Reading state information... Done  
E: Unable to locate package libssl-dev  
-bash: $'\r': command not found  
--2014-10-24 12:02:20--  https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz%0D  
Resolving www.python.org (www.python.org)... 103.245.222.223  
Connecting to www.python.org (www.python.org)|103.245.222.223|:443... connected.  
HTTP request sent, awaiting response... 404 Not Found  
2014-10-24 12:02:20 ERROR 404: Not Found.  

tar: /usr/local/src/\r: Cannot open: No such file or directory  
tar: Error is not recoverable: exiting now  
: No such file or directoryython-3.4.0/  
-bash: ./configure: No such file or directory  
ln: failed to create symbolic link `/usr/bin/python3\r': File exists  
-bash: $'\r': command not found  
-bash: $'\r': command not found  
-bash: $'\r': command not found  
-bash: $'\r': command not found  
-bash: $'\r': command not found 

Somebody think that Windows line endings are different from Unix/Linux line endings. bash doesn't know how to handle Windows line-endings. Create the file with an editor on Linux.

  1. i rewrite it in putty console with nano ,same problem remains. 2.It is strange that i nano the /root/ini.sh in putty console,there is just one line in it:
apt-get update 

When i input :

sh /root/ini.sh 
E: Invalid operation update

There is no relationship betwwen my ini.sh and '\r' ,what is the matter?

4 comments
  • It's the line endings as someone else said. The errors you get about \r are definitely due to Windows line endings.

    If you could use "pastebinit" to paste the exact content of those files to a pastebin maybe we can spot the mistakes.

  • Maybe.....

    #!/bin/bash
     (apt-get update && apt-get -y upgrade && apt-get install -y build-essential  gcc  libtool  libxml2-dev libxslt1-dev  openssl  libssl-dev) > /dev/null
    wget "https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz"
    tar "xf Python-3.4.0.tgz -C /usr/local/src/"
    cd "/usr/local/src/Python-3.4.0/"
    ./configure --prefix=/usr/local/python3.4  --with-ensurepip=install   
    make 
    make  install
    ln -s "/usr/local/python3.4/bin/python3.4"   "/usr/bin/python3.4"
    
  • run this, and try it again:

    dos2unix /root/initial.sh
    
  • You might need to install the dos2unix package first:

    sudo apt-get install dos2unix
    
1 Answer

think to kamaln7 ,
sudo apt-get install dos2unix
dos2unix /root/initial.sh
sh /root/initial.sh
it is ok.
But problem remains.
1.In gvim for windows
i have set configuration in ~vimrc .
set fileformat=unix
when i save the file in gvim and sent it into my serverip, sh /root/initial.sh can not make it run.
E: Invalid operation update and -bash: $'\r': command not found

  1. In notepad++ Edit -> EOL Conversion--unix format. same problem remain.

Is the transmission process cause the problem ?
i change the window format with gvim or notepad++,but the transmission process change it
from unix format into window format again?

My client system is win7, i have to work in win7 environment , it is inconvenient for me to change the file format with dos2unix mytarget.sh and then sh mytarget.sh .
How can i solve the problem in gvim for windows and notepad++ ?

Have another answer? Share your knowledge.