【问题标题】:Error moving files in shell for-loop在 shell for 循环中移动文件时出错
【发布时间】:2017-05-05 11:13:52
【问题描述】:

我正在使用以下 shell 代码将文件从一个位置移动到另一个位置:

for i in [ test1 test2 test3]
do
  mv /home/usr/enabler/$i/*  /home/bin/enabler/consolidate
  echo "moved successfully!!!"
done                                                                               

在 bash shell 中运行我收到错误 "Illegal file pattern: Unclosed character class near index 1"。我希望循环转到每个文件夹 test1test2test3 并移动目标文件夹中 test1test2test3 中存在的所有文件。

【问题讨论】:

  • 方括号在这里似乎一点用都没有,你为什么要放那些?还要注意正确引用您的变量。另见stackoverflow.com/questions/10067266/…
  • 单行:mv /home/usr/enabler/{test1,test2,test3}/* /home/bin/enabler/consolidate

标签: bash shell sh


【解决方案1】:

以这种方式修复您的脚本:

for i in test1 test2 test3
do
  mv /home/usr/enabler/"$i"/*  /home/bin/enabler/consolidate
  echo "moved successfully!!!"
done

这正是你想要的!!

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 2013-01-08
    • 2012-02-26
    • 2017-08-20
    • 2017-02-24
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多