bash脚本从.txt中提取变量,在尝试使用mount命令时不断给出语法错误

bash脚本从.txt中提取变量,在尝试使用mount命令时不断给出语法错误,bash,sh,Bash,Sh,上个星期我一直在努力让它起作用,但我不明白为什么它不起作用。我直接在终端中输入的结果好坏参半,但从.sh运行时,会不断收到语法错误消息。使用ubuntu 11.10 看起来mount命令的一部分被推到下一行,无法正常完成。。我不知道为什么会发生这种情况,也不知道如何防止它进入第二线 我在mounts.txt中定义了以下几行,它们从下面的mount-drives.sh读取 我已经调用它来使用sudo运行,所以它不应该是一个权限问题 感谢您的关注,如果需要更多信息,请告诉我 mounts.txt

上个星期我一直在努力让它起作用,但我不明白为什么它不起作用。我直接在终端中输入的结果好坏参半,但从.sh运行时,会不断收到语法错误消息。使用ubuntu 11.10

看起来mount命令的一部分被推到下一行,无法正常完成。。我不知道为什么会发生这种情况,也不知道如何防止它进入第二线

我在mounts.txt中定义了以下几行,它们从下面的mount-drives.sh读取

我已经调用它来使用sudo运行,所以它不应该是一个权限问题

感谢您的关注,如果需要更多信息,请告诉我

mounts.txt

    mountname,//server/share$,username,password,
mount-drives.sh--源代码,更新如下

    #!/bin/bash
    while read LINE;
    do 

    # split lines up using , to separate variables
    name=$(echo $LINE | cut -d ',' -f 1)
    path=$(echo $LINE | cut -d ',' -f 2)
    user=$(echo $LINE | cut -d ',' -f 3)
    pass=$(echo $LINE | cut -d ',' -f 4)

    echo $name
    echo $path
    echo $user
    echo $pass


    location="/mnt/test/$name/"

    if [ ! -d $location ]
    then
        mkdir $location
    fi

    otherstuff="-o rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=$user,password=$pass"

    mount -t cifs $otherstuff $path $location

    done < "/path/to/mounts.txt";
mount-drives.sh--已更新

    #!/bin/bash

    while read LINE
    do
        name=$(echo $LINE | cut -d ',' -f 1)
        path=$(echo $LINE | cut -d ',' -f 2)
        user=$(echo $LINE | cut -d ',' -f 3)
        pass=$(echo $LINE | cut -d ',' -f 4)
        empty=$(echo $LINE | cut -d ',' -f 5)
        location="/mount/test/$name/"
        if [ ! -d $location ]
        then
            mkdir $location
        fi
        mounting="mount -t cifs $path $location -o username=$user,password=$pass,rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777"
        $mounting
        echo $mounting >> test.txt
     done < "/var/www/MediaCenter/mounts.txt"

看完评论后直言不讳。$pass拾取了一个新行,因为mounts.txt是在windows中创建的,并且具有windows行结尾。尝试将echo$传递行更改为:


看看是否一切都正确无误。

这里有很多值得改进的地方。考虑下面更紧凑、更正确的方法:

while IFS=, read -u 3 -r name path user pass empty _; do
  mkdir -p "$location"
  cmd=( mount \
    -t cifs \
    -o "rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=$user,password=$pass" \
    "$path" "$location" \
  )
  printf -v cmd_str '%q ' "${cmd[@]}" # generate a string corresponding with the command
  echo "$cmd_str" >>test.txt          # append that string to our output file
  "${cmd[@]}"                         # run the command in the array
done 3<mounts.txt

与原始脚本不同的是,即使路径或位置值包含空格,也可以正常工作。

尝试使用“-x”选项运行脚本,使bash在执行时打印出每一行。在顶部附近添加'set-x',或者执行bash-x./mount-drives.sh。我猜当你回显$LINE时,.txt文件中的“$”会引起问题…感谢你的关注,它似乎不适合我,或者有一个错误阻止了它的显示。我尝试从终端启动,并在脚本中使用set-x命令来解决相同的问题。我是否应该以某种方式避开回音中的$LINE?在回音中的$LINE周围加上双引号可能是个好主意。脚本是否向终端回显正确的值?是否尝试将回显放在mount命令之前,以查看正在执行的内容?如果是,请张贴。您说命令的一部分被推到下一行-哪一部分?看起来$line和下面的echo$user等获得了正确的变量。当我将所有内容复制到终端时,它会通过,但mount命令有一个错误。这是挂载回显的输出,看起来它在输出文件的-o处被拆分,但是当我将它从输出文件复制到终端时,它在$path之前被拆分:mount-t cifs-o rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=username,password=password//server/share$/mnt/test/mount/Ah,windows。是的,可以。我想可能是这样,但我自己无法复制它,但我没有想到其他行结尾。尝试更改回音但没有效果,但它显示我需要删除mounts.txt tho中的双引号,方法是删除$otherstuff变量,并将该内容移动到mount语句的末尾,它现在以1行的形式写入我的输出文件,现在复制到终端时。现在,我必须处理脚本没有通过运行。当我运行脚本时,脚本在完成之前终止,当我尝试通过终端加载时出现此错误:第30行:意外标记附近的语法错误完成“第30行:完成”while IFS=, read -u 3 -r name path user pass empty _; do mkdir -p "$location" cmd=( mount \ -t cifs \ -o "rw,uid=1000,gid=1000,file_mode=0777,dir_mode=0777,username=$user,password=$pass" \ "$path" "$location" \ ) printf -v cmd_str '%q ' "${cmd[@]}" # generate a string corresponding with the command echo "$cmd_str" >>test.txt # append that string to our output file "${cmd[@]}" # run the command in the array done 3<mounts.txt