请注意,我将要发布的这段代码故意不完美,如果目录中有文本文件,但完整代码确实有这样的检查,它将无法工作。但问题在于:
#!/bin/bash
for d in *; do
cd $d
done
产生:
./test: line 3: cd: 500: No such file or directory
./test: line 3: cd: 536: No such file or directory
./test: line 3: cd: 560: No such file or directory
./test: line 3: cd: 572: No such file or directory
./test: line 3: cd: out.txt: No such file or directory
./test: line 3: cd: prob1: No such file or directory
./test: line 3: cd: problem2: No such file or directory
./test: line 3: cd: README: No such file or directory
./test: line 3: cd: test: No such file or directory
其中 500、536、560 和 572 是应该能够打开的目录。如果我只做一个 cd 500 那么我可以毫无问题地进入目录。但是当我在 shell 脚本中使用自动化过程时,它就不起作用了。
好吧,在进一步进入程序后,我现在有:
#!/bin/bash
cd "$1"
for d in */; do
(
cd "$d"
for file in $*; do
(
bla="$(head -4 $file|tail -1)"
cd ~cfb43/cs265/a1
echo $bla > out.txt
cd
cd "$1"
cd "$d"
)
done
cd "$1"
)
done
我收到:
head: error reading `/home/cfb43/cs265/a1': Is a directory
head: error reading `/home/cfb43/cs265/a1': Is a directory
head: error reading `/home/cfb43/cs265/a1': Is a directory
head: error reading `/home/cfb43/cs265/a1': Is a directory
head: error reading `/home/cfb43/cs265/a1': Is a directory
我要做的就是进入一个目录并从文件中读取并获取第四行文本并将其打印到我文件夹中的 out.txt 中。我不确定为什么这段代码会卡在 a1 而不是至少 500 或类似的东西。任何进一步的帮助,我会非常感激