Kal*_*sov 2 bash perl awk if-statement
我有两个文件,其中一个有数据文件1:
content
Run Code Online (Sandbox Code Playgroud)
file2是用vi创建的,我只是放了一些进入并有两三行,但仍然没有数据只是内容.
这对我来说不起作用,当添加行但没有其他东西.
if [ ! -s file2 ]
print "file2 is empty"
else
print "file2 has content"
fi
Run Code Online (Sandbox Code Playgroud)
在这种情况下是转:file2有内容
想法是在该文件中捕获任何字母或数字,如空格或输入为空.
if perl -ne'exit 1 if /\S/' file ; then
echo 'Only contains blank lines'
fi
Run Code Online (Sandbox Code Playgroud)
来想一想,grep也会有所作为.
if ! grep -q '[^[:space:]]' file ; then
echo 'Only contains blank lines'
fi
Run Code Online (Sandbox Code Playgroud)
这些比anubhava的解决方案更好,因为他们认为只包含空格和制表符的行是空白行.为尾随空格分配重要性绝不是一个好主意.