Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash语法错误_Bash_Shell_Sh_Raspbian_Debian Based - Fatal编程技术网

Bash语法错误

Bash语法错误,bash,shell,sh,raspbian,debian-based,Bash,Shell,Sh,Raspbian,Debian Based,只是学习bash并尝试在脚本中实现函数 下面的脚本可以通过ShellCheck正常运行,但在命令行运行bash时出现语法错误 这一定是我定义函数的方式,但我无法找到正确的方法来实现它——特别是如果它通过了ShellCheck error is on line 24 syntax error near unexpected token `${\r'' `autounload() { 脚本: 我尝试在调用函数的位置上下移动函数,但似乎没有什么区别。正如在注释中发现的,您有一个Windows(DOS

只是学习bash并尝试在脚本中实现函数

下面的脚本可以通过ShellCheck正常运行,但在命令行运行bash时出现语法错误

这一定是我定义函数的方式,但我无法找到正确的方法来实现它——特别是如果它通过了ShellCheck

error is on line 24
syntax error near unexpected token `${\r''
`autounload() {
脚本:


我尝试在调用函数的位置上下移动函数,但似乎没有什么区别。

正如在注释中发现的,您有一个Windows(DOS)文件,然后在UNIX机器上运行。这有一个问题,即DOS中的行尾是
\r\n
,而UNIX中的行尾是
\n
,因此每行都有多余的
\r

要清除这些,您可以使用以下任一方法:

  • 在UNIX中:
    dos2unix
  • 在Windows中:中描述的任何工具

您可能有一个在UNIX中运行的DOS文件。尝试运行
dos2unix
以“清除”我正在使用Textpad在Windows上编辑的itI,然后通过ftp进行编辑。所以,如果我只使用sudo nano,应该会把它清理干净吗?不完全是,因为窗口有不同的线条端点。只需使用工具
dos2unix
(或中所示的其他工具)来替换它们。谢谢该死,一小时前就该问了把它写在答案里,我会记下来的。这对我来说是一个陡峭的学习曲线。这在。
#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
#
# Called from {SCRIPT_DIR}/usb-initloader.sh
# make sure to chmod 0755 on file
#
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
#
# CONFIGURATION
#
LOG_FILE="$1"
MOUNT_DIR="$2"
DEVICE="$3"  # USB device name (from kernel parameter passed from rule)
#
# check for defined log file
if [ -z "$LOG_FILE" ]; then
    exit 1
fi
#
# autounload function to unmount USB device and remove mount folder
#
autounload() {
    if [ -z "$MOUNT_DIR" ]; then
        exit 1
    fi
    if [ -z "$DEVICE" ]; then
        exit 1
    fi

    dt=$(date '+%Y-%m-%d %H:%M:%S')
    echo "--- USB Auto UnLoader --- $dt"

    sudo umount "/dev/$DEVICE"
    sudo rmdir "$MOUNT_DIR/$DEVICE"

    # test that this device isn't already mounted
    device_mounted=$(grep "$DEVICE" /etc/mtab)

    if ! "$device_mounted"; then
        echo "/dev/$DEVICE successfully Un-Mounted"
        exit 1
    fi
}

autounload >> "$LOG_FILE" 2>&1
#
autounload() {
    if [ -z "$MOUNT_DIR" ]; then
        exit 1
    fi