【发布时间】:2015-02-06 08:55:31
【问题描述】:
我编写了这段代码来将一个目录移动到另一个目录。 我想要的这段代码如下:
- 用户将给出文件名或目录名。
- 用户可以提供一个目标文件夹。如果用户不想要目标文件夹,他/她只需按 Enter。
- 然后源目录将被复制到目标目录(如果给定)或默认目录(如果没有给定目标)
这里是代码。
$source_folder
$destination_folder
$destination
read -r directory_source
read -r destination_folder
if [ "$destination_folder" = ""]
then
$destination = "/var/www/html/"
else
$destination = "$destination_folder"
fi
cp -a "$source_folder" "$destination"
这是该程序的输入:
Myfiles.sh (called the shell script in terminal)
Max Sum (This is the source directory)
(Pressed enter i.e. No destination given)
它给出以下错误:
/usr/local/myfiles/copyDirToHTML.sh: line 6: [: : unary operator expected
/usr/local/myfiles/copyDirToHTML.sh: line 10: =: command not found
cp: cannot stat ‘’: No such file or directory
【问题讨论】:
-
需要交互式输入的脚本不太有用,因为它们不能作为构建块合并到更大的脚本中,并且您放弃了 shell 的有用交互式功能,例如选项卡扩展等。明智的
cpd(对于默认复制)就像cpd () { cp "$1" "${2-/path/to/default}"; }一样简单 -
谢谢,我只是学习。将尝试您的解决方案。 @tripleee
标签: linux shell if-statement ubuntu-14.04 logical-operators