.sh 循环遍历文件夹递归-

.sh 循环遍历文件夹递归-

我的文件路径是/server/2TB_HDD/TV Shows/(其中有大约 100 个节目,每个节目都有季节)

我似乎遇到的问题是文件路径。“电视节目”部分。

如果我将其作为 .sh 运行,则会出现类似的问题

(我对此还不太熟悉)

任何帮助都非常感谢。谢谢

代码:

#!/bin/bash
for f in /server/'2TB_HDD'/TV\ Shows/*;
  do
     [ -d $f ] && cd "$f" && echo Entering into $f and installing packages
  done;

bash test.sh

test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: /server/2TB_HDD/TV: binary operator expected
test.sh: line 4: [: too many arguments
test.sh: line 4: [: too many arguments

答案1

在方括号中引用$f。养成引用变量的习惯。

顺便提一下,可以使用替代方法,即find,空分隔符和while ... read VAR ; do ...done结构。例如,我进入每个目录并pwd在每个目录中执行操作。

find /home/xieerqi -maxdepth 1 -type d -print0 | while IFS= read -d $'\0' DIR ; do
    cd "$DIR"
    pwd
    cd ..
done

在你的情况下,你可以做类似的事情,

find "/server/2TB_HDD/TV Shows/" -maxdepth 1 -type d -print0 | while IFS= read -d $'\0' DIR ; do
cd "$DIR" && printf "\nEntering into %s and installing packages\n " "$DIR"
done

相关内容