需要一些帮助来简化bash中的脚本吗
这实际上是我的第一篇帖子。 感谢任何能帮忙的人需要一些帮助来简化bash中的脚本吗,bash,Bash,这实际上是我的第一篇帖子。 感谢任何能帮忙的人 #!/bin/bash for file in * do if [ -s $file ]; then continue else echo "$file is empty and will be removed" rm $file fi done for file in * do if [ -x $file -a -f $file ];then
#!/bin/bash
for file in *
do
if [ -s $file ]; then
continue
else
echo "$file is empty and will be removed"
rm $file
fi
done
for file in *
do
if [ -x $file -a -f $file ];then
echo "$file is an executable"
fi
done
有没有更简单的方法来编写此脚本,使其全部在1 for循环中?尝试以下方法:
#!/bin/bash
for file in *; do
if [ -s "$file" ]; then
if [ -x "$file" -a -f "$file" ]; then
echo "$file" "is an executable"
fi
else
echo "$file" "is empty and will be removed"
rm "$file"
fi
done
我投票结束这个问题,因为它属于@MarkSetchell;哎呀!谢谢现在更正。@haccks:最好引用变量,以避免文件名被分词。即使他们没有空间,推荐或使用扩展的
test
operator@Inian; 同意。我通常使用[[]]
而不是[]
,在这种情况下不引用变量,但引用总是好的。