在 Bash 脚本中使用参数调用程序时出现问题
Trouble Calling Program with Arguments Inside Bash Script
问题
我有代码首先遍历一个文件,每行一个文件夹路径确保它们是正确的(它确实通过了,我也知道这些是有效路径),然后尝试将它传递给程序borg
但错误。
问题似乎完全取决于我如何创建或使用 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 < ""
echo $folderList #gets past here successfully
borg create -s --progress ::${dateString} $folderList
通过使用数组传递参数并简化您的条件测试,类似的东西应该会更好地工作:
while read -r line
do
if [ -f "$line" ] || [ -d "$line" ]; then
folderList+=( "$line" )
else
exit 1
fi
done < ""
echo "${folderList[@]}" #gets past here successfully
borg create -s --progress "::${dateString}" "${folderList[@]}"
问题
我有代码首先遍历一个文件,每行一个文件夹路径确保它们是正确的(它确实通过了,我也知道这些是有效路径),然后尝试将它传递给程序borg
但错误。
问题似乎完全取决于我如何创建或使用 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 < ""
echo $folderList #gets past here successfully
borg create -s --progress ::${dateString} $folderList
通过使用数组传递参数并简化您的条件测试,类似的东西应该会更好地工作:
while read -r line
do
if [ -f "$line" ] || [ -d "$line" ]; then
folderList+=( "$line" )
else
exit 1
fi
done < ""
echo "${folderList[@]}" #gets past here successfully
borg create -s --progress "::${dateString}" "${folderList[@]}"