linux - shell 脚本 : appending directory path and filename

标签 linux shell

我想使用 shell 脚本从一个目录复制一个文件

假设我将目录和文件名分别保存为

dir=/home/user/directory/
file=file_1

在我的脚本中使用此命令复制文件 Im

cp $dir$file .

但是我得到这个错误

/bin/cp omitting directory '/home/user/directory'

我已经尝试了所有组合,例如。从变量目录等中省略了跟踪反斜杠,但没有任何工作。我不明白这段代码有什么问题。请帮助

也许命令 $dir$file 没有在 shell 中解包(即只有目录变量被解包,而不是文件变量)!!!!!!

最佳答案

看起来您在 cp $dir$file 中遇到扩展问题。 为了防止可能出现的问题,最好用大括号保护您的变量并用双引号完整路径/文件为确保您不会被文件名或天堂中的空格捕获,请禁止用户的目录名:

cp "${dir}${file}" .

这将防止错过第二个 $ 的可能性。还要确保您对其他用户/home 具有读取权限(如果您是 root 用户或使用 sudo 应该没问题)

关于linux - shell 脚本 : appending directory path and filename,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24928459/

相关文章:

linux - 大小差异 : Copy command doesn't copy all contents

linux - Bash oneliner with pipes 和 if condition giving error

linux - 每次文件达到特定大小时使文件无效的 Shell 脚本

linux - 如何从文件中以特定单词开头的 bash 输出中保存文本?

linux - 使用 rsync 备份

c++ - 树莓派上的 openGL,索引顶点

linux - 删除包含下一行子字符串的几乎重复项

linux - linux将文件从usb复制到桌面脚本

regex - 如何获取括号中指定的内容?

linux - 如何在没有 root 权限的情况下安装 Bioconductor 软件包?