bash 脚本语法错误:字意外(期望“做”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22849884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:07:03  来源:igfitidea点击:

bash script Syntax error: word unexpected (expecting "do")

bashnginx

提问by Undermine2k

I'm trying to run this dropbox script on my nginx server , but im getting:

我正在尝试在我的 nginx 服务器上运行这个 dropbox 脚本,但我得到:

 Syntax error: word unexpected (expecting "do")

I copy pasted the script for a website, and I tried removing special characters, but im still getting the same error.

我复制粘贴了网站的脚本,并尝试删除特殊字符,但我仍然遇到相同的错误。

script:

脚本:

#!/bin/sh
# /etc/init.d/dropbox
### BEGIN INIT INFO
# Provides:          dropbox
# Required-Start:    $network $syslog $remote_fs
# Required-Stop:     $network $syslog $remote_fs
# Should-Start:      $named $time
# Should-Stop:       $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the dropbox daemon for debian/ubuntu
# Description:       Dropbox daemon for linux
### END INIT INFO

DROPBOX_USERS="root"
start() {
    echo "Starting dropbox..."
    for dbuser in $DROPBOX_USERS; do
        start-stop-daemon -b -o -c $dbuser -S -x /home/$dbuser/.dropbox-dist/dropboxd
    done
}

stop() {
    echo "Stopping dropbox..."
    for dbuser in $DROPBOX_USERS; do
        start-stop-daemon -o -c $dbuser -K -x /home/$dbuser/.dropbox-dist/dropboxd
    done
}

status() {
    for dbuser in $DROPBOX_USERS; do
        dbpid=`pgrep -u $dbuser dropbox`
        if [ -z $dbpid ] ; then
            echo "dropboxd for USER $dbuser: not running."
        else
            echo "dropboxd for USER $dbuser: running."
        fi
    done
}


case "" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  restart|reload|force-reload)
    stop
    start
    ;;

  status)
    status
    ;;

  *)
    echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}"
    exit 1

esac

exit 0

回答by rici

Probably you managed to insert windows line endings when you copy and pasted. If you have dos2unix, use it. (dos2unix scriptfile) Otherwise, there are a number of similar utilities.

可能您在复制和粘贴时设法插入了 Windows 行尾。如果您有 dos2unix,请使用它。(dos2unix 脚本文件) 否则,还有许多类似的实用程序。

回答by Michael Tomkins

Why do you have

为什么你有

/home/$dbuser/.dropbox-dist/dropboxd

or replacing variables.

或替换变量。

/home/root/.dropbox-dist/dropboxd

Shouldn't it be

不应该是

/root/.dropbox-dist/dropboxd

eg

例如

/$dbuser/.dropbox-dist/dropboxd

Are you seriously allowing dropboxes in /root?

你真的允许 Dropbox 进入/root吗?

Also why a for dbuser in DROPBOX_USERS; doreassign? rather than

还有为什么要for dbuser in DROPBOX_USERS; do重新分配?而不是

dbuser=$DROPBOX_USERS