首页 > 解决方案 > 如何将 \ 与 read 命令一起使用?

问题描述

我是这个主题的完整初学者,我通常会寻找解决方案,而不是仅仅提出问题,因为我确信有人遇到了类似的问题。但这一次我找不到解决问题的方法。
所以我写了一个小 shell 脚本,它使用 rclone 将文件从变量文件夹复制到我的 Google Drive。
我的问题是如果包含多个单词,我无法输入文件夹名称。我已经尝试过使用read -r folder但它返回

Command copy needs 2 arguments maximum: you provided 3 non flag arguments: ["One" "Folder/" "Drive:SomeFolder/File"]

如果我使用\它返回

Command copy needs 2 arguments maximum: you provided 3 non flag arguments: ["One\\" "Folder/" "Drive:SomeFolder/File"]

我希望最终命令看起来像这样

rclone copy One\Folder/ Drive:SomeFolder/File
#!bin/sh
#upload

echo "Folder Name:"
read -r folder
echo "File Name:"
read -r name
rclone copy ${folder}/ Drive:SomeFolder/${name} 

标签: linuxshellrclone

解决方案


问题在于您调用 rclone 的方式,而不是您读取变量的方式。您需要引用变量扩展。ShellCheck 会自动检测此问题和其他常见问题。——那个人


推荐阅读