嵌入Perl文件的Bash脚本'mv -f'

时间:2018-07-16 01:58:53

标签: bash perl

尝试使用perl将包含子目录的目录移动到/ opt。只是perl。

#!/usr/bin/perl -w

use strict;
use warning

system(

'echo "Copy the application directory here and paste it here"

read apple

mv -f $apple /opt

');

这将返回

mv: cannot stat "'/root/dump'": No such file or directory

我对read apple的输入是/root/dump,它是一个目录。当我直接在终端上执行此操作时,它起作用了,但是后来在perl文件中却无效。您能描述一下吗,因为我对bash脚本不太熟悉?谢谢你。

更新:

我尝试了我写的bash文件

#!/bin/bash

echo "Copy the application directory here and paste it here"

read apple

mv -f $apple /opt

这也将返回与perl相同的结果。

3 个答案:

答案 0 :(得分:4)

如果从字面上说"'/root/dump'",则意味着您在语法上还添加了一些字面quotes。确保在您的Bash脚本中引用$apple,并且引用您提供给read的内容。

答案 1 :(得分:3)

如果要使用 just perl ,而不是system()移出外壳,请使用File::Copy::Recursive中的dirmove()移动目录树。 / p>

答案 2 :(得分:-1)

如果我理解您的问题,那么您只想将目录重新移动到/opt。该脚本就是这样做的。

#!/usr/bin/perl -w

use strict;

print "Copy the application directory here and paste it here: ";
chomp (my $apple = <STDIN>);
`mv -f $apple /opt`;
  

使用perl。只是perl。

我不明白。您在上面提供的代码中使用系统命令。如果您只想使用perl,请使用Shawn已经提到的dirmove() from File::Copy::Recursive。希望这会有所帮助。