【问题标题】:Trouble Calling Program with Arguments Inside Bash Script在 Bash 脚本中使用参数调用程序时遇到问题
【发布时间】:2019-12-24 21:26:45
【问题描述】:

问题

我的代码首先遍历一个文件,每行都有一个文件夹路径,确保它们是正确的(它确实通过了,我也知道这些是有效路径),然后尝试将它传递给程序 @987654322 @ 但错误。

问题似乎完全取决于我如何创建或使用folderList(您可以将其视为第一个回声),但我不确定如何解决。

输入

/media/sf_D_DRIVE/VirtualMachines Backups/
/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/

错误输出

"/media/sf_D_DRIVE/VirtualMachines Backups/" "/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/"

"/media/sf_D_DRIVE/VirtualMachines: [Errno 2] No such file or directory: '"/media/sf_D_DRIVE/VirtualMachines'
Backups/": [Errno 2] No such file or directory: 'Backups/"'
"/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/": [Errno 2] No such file or directory: '"/media/sf_C_DRIVE/Websites/47/sln/site/App_Data/"'

代码

#this snippet reads in folder paths from a file
while read line
do
    exists=false
    if [ -f "$line" ]; then
        exists=true
    fi
    if [ -d "$line" ]; then
        exists=true
    fi
    if [ $exists = false ]; then
        exit 1
    fi
    folderList+=" $line"
done < "$2"

echo $folderList #gets past here successfully

borg create -s --progress $1::${dateString} $folderList

【问题讨论】:

标签: bash shell whitespace quotes


【解决方案1】:

通过使用数组来传递参数并简化条件测试,这样的事情应该会更好:

while read -r line
do
    if [ -f "$line" ] || [ -d "$line" ]; then
      folderList+=( "$line" )
    else
      exit 1
    fi
done < "$2"

echo "${folderList[@]}" #gets past here successfully

borg create -s --progress "$1::${dateString}" "${folderList[@]}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多