为什么这组 apt 命令逐行运行时有效,但作为脚本运行时无效?

为什么这组 apt 命令逐行运行时有效,但作为脚本运行时无效?

我可以在 Digital Ocean 中全新的 Ubuntu 16.04 实例上逐行运行此脚本,但是当我授予它可执行权限chmod +x并运行它时,我在运行脚本的不同部分时尝试失败。

sudo apt-get -y install nginx git libcurl4-gnutls-dev libxml2-dev libssl-dev 
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
sudo apt-get update
sudo apt-get install r-base r-base-dev gdebi-core
sudo su - -c "R -e \"install.packages('devtools', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('XML', repos='http://cran.us.r-project.org')\""
(crontab -l 2>/dev/null; echo "5 0 * * * Rscript /root/transit-tracker/scripts_in_testing/download_gtfs.R ") | crontab -
(crontab -l 2>/dev/null; echo "* * * * * Rscript /root/transit-tracker/scripts_in_testing/get_tidy_gps.R ") | crontab -
wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.5.3.838-amd64.deb
sudo gdebi -n shiny-server-1.5.3.838-amd64.deb 
sudo sed -i 's/  listen 3838;/  listen 80;/' /etc/shiny-server/shiny-server.conf
# then reboot 

作为回应,我得到了;

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package
Executing: /tmp/tmp.EeBdzAP95F/gpg.1.sh --keyserver
keyserver.ubuntu.com
--recv-keys
E298A3A825C0D65DFD57CBB651716619E084DAB9
" not a key ID: skipping57CBB651716619E084DAB9
E: Invalid operation update
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package r-base
E: Unable to locate package r-base-dev
E: Unable to locate package gdebi-core
-su: R: command not found
-su: R: command not found
-su: R: command not found
-su: R: command not found
-su: R: command not found
'rontab: invalid option -- '
crontab: usage error: unrecognized option
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)
'rontab: invalid option -- '
crontab: usage error: unrecognized option
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)
--2017-05-08 22:39:38--  https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.5.3.838-amd64.deb%0D
Resolving download3.rstudio.org (download3.rstudio.org)... 52.85.90.213, 52.85.90.39, 52.85.90.98, ...
Connecting to download3.rstudio.org (download3.rstudio.org)|52.85.90.213|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2017-05-08 22:39:38 ERROR 404: Not Found.

sudo: gdebi: command not found
: No such file or directoryserver/shiny-server.conf

答案1

正如您在评论中确认的那样,您的脚本是在 Windows 系统上编写的,并且包含 Windows 样式(CR+LF)行尾,而不是 Unix 样式(LF),这是必要的。

为了轻松地在 DOS/Windows 和 Unix 样式之间转换文本文件的行尾,请安装一个名为的包dos2unix

sudo apt install dos2unix

它带有两个命令,dos2unix将 Windows 文本文件转换为 Unix 格式,以及unix2dos将 Unix 文本文件转换为 Windows 格式。

对于你的情况,只需运行

dos2unix FILENAME

用您的实际脚本文件名替换FILENAME,之后它应该可以正常运行。

相关内容