我需要检查它是否有正常工作的数据,如果它是空的则失败。
#!/bin/bash
while read _mem
do
if [[ -s $_mem ]] ; then
echo "$_mem"
else
echo "The file is empty"
fi
done
我像./filechk.sh < stuff
如果文件内容有数据,则打印文件的每一行。如果它是空的,它什么都不打印。我认为它与读取失败的事实有关,所以循环失败但是当我将其更改为此时。
#!/bin/bash
while read _mem
do
if [[ -s $_mem ]] ; then
echo "$_mem"
fi
done || echo "The file is empty"
即使读取失败,它也不起作用,它以0退出。
答案 0 :(得分:1)
如果文件存在但是为空,那么在第一种情况下,while
在读取最终失败时不会执行正文。在第二种情况下,read
失败,但while
没有。
因此,您可能需要单独检查空案例:
#!/bin/bash
if read _mem ; then
echo $_mem
while read _mem ; do
echo "$_mem"
done
else
echo "File is empty"
fi