Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 为什么这会给我一个错误?伯爵是_Shell - Fatal编程技术网

Shell 为什么这会给我一个错误?伯爵是

Shell 为什么这会给我一个错误?伯爵是,shell,Shell,我真的不明白为什么不工作 ls>out count="$(ls|wc -l)" for i in {1..$count-2} do if[ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then mv "$1$count$2" | cat out | head -$count | tail -1 fi done 在if和'[”,脚本将起作用。欢迎使用。也许你可以解释一下你想要实现什么?请检查:另外,无论如何,你不应该以编程方式

我真的不明白为什么不工作

ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if[ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done

在if和'[”,脚本将起作用。

欢迎使用。也许你可以解释一下你想要实现什么?请检查:另外,无论如何,你不应该以编程方式使用
ls
,当然不应该计算文件数。请参阅BashFAQ#4,at,还有,你为什么要用管道将
mv
连接到
cat
mv
不会将任何内容写入stdout(因此,写入该管道),因此它只会使该命令和其他管道元素之间的操作顺序不明确(因为管道的所有组件都是同时执行的)。此外,这是一种极为低效的文件迭代方式。使用shell内置的全局搜索功能:
用于*中的文件;do[“$file”=rename.sh]&&continue;:“这里还有什么”;done
。如果要迭代除最后两个以外的所有文件,也很容易:
文件=(*);用于“${files[@]:0:${files[@]}-2}”中的文件;do…;done
您不能将大括号扩展与变量索引一起使用,例如,
{1..$count-2}
将不起作用(如果没有一些hacks)。您可以更好地使用上面的建议。不,它不会——还有其他错误。例如,
对于{1..$count-2}中的i
语法无效,无法在数值范围内进行迭代。我的意思是语法正确,shell将执行脚本,不会输出错误。
    ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if [ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done