如何运行bash脚本文件?

时间:2014-10-24 11:48:10

标签: bash shell

以下bash脚本可以在我的debian7控制台中逐个执行。

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

当我将所有命令保存在文件bash -x /root/initial.sh中时,为什么命令/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 

奇怪的是,我在putty控制台中将/root/ini.sh纳入其中,其中只有一行:

apt-get update 

当我输入时:

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

我的ini.sh和' \ r'之间没有任何关系。什么事?

在digitalocean论坛上向kamaln7思考 https://www.digitalocean.com/community/questions/how-to-run-the-bash-scripts-file

sudo apt-get install dos2unix
dos2unix /root/initial.sh
sh  /root/initial.sh 

没关系。
但问题仍然存在。

1.In gvim for windows

我在~vimrc中设置了配置。

set fileformat=unix 

当我将文件保存在gvim中并将其发送到我的serverip时,sh /root/initial.sh无法使其运行。
E:无效的操作更新和-bash:$' \ r':找不到命令

2. In notepad++

Edit -> EOL Conversion--unix format.  

同样的问题依然存在。

传输过程是否会导致问题? 我使用gvim或notepad ++更改窗口格式,但传输过程会更改它 从unix格式再次转换为窗口格式?

我的客户端系统是win7,我必须在win7环境下工作,用dos2unix mytarget.sh然后sh mytarget.sh更改文件格式是不方便的。
如何解决gvim for windows和notepad ++中的问题?

1 个答案:

答案 0 :(得分:-2)

删除-x参数。如果这不起作用,请尝试

. /root/initial.sh

注意空间。