我有一个文件名为 diff.txt。我想要检查它是否为空。
我写了一个类似下面的bash脚本,但是我无法让它工作。
if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm emtpy.txt
fi
我有一个文件名为 diff.txt。我想要检查它是否为空。
我写了一个类似下面的bash脚本,但是我无法让它工作。
if [ -s diff.txt ]
then
touch empty.txt
rm full.txt
else
touch full.txt
rm emtpy.txt
fi
可以试试这个:
#!/bin/bash -e
if [ -s diff.txt ]; then
# The file is not-empty.
rm -f empty.txt
touch full.txt
else
# The file is empty.
rm -f full.txt
touch empty.txt
fi
empty.txt
和full.txt
的角色。empty=empty.txt; full=full.txt; diff=diff.txt; 如果 [ -s ${diff?} ]; then r=${empty?} t=${full?}; else r=${full?} t=${empty?}; fi; rm ${r?}; touch ${t?}
- William Pursellset -u
。 - Jon McClung[ -s file.name ] || echo "file is empty"
[[ -s file.name ]] || { [[ -f file.name ]] && echo 'empty' || echo 'does not exist'; }
- smarber[ ... ]
而不是 [[ ... ]]
。后者是 bashism,仅应用于不兼容的 bash 检查(例如 regexp)。如果您有机会编写任何需要遵循 POSIX shell portable 的内容(例如 Debian 系统脚本),那么你自己也会感谢这个习惯的 :). - pevik[ -s file ] # 检查文件大小是否大于0
[ -s diff.txt ] && echo "file has something" || echo "file is empty"
如果需要,这将检查当前目录中所有的*.txt文件,并报告所有空文件:for file in *.txt; do if [ ! -s $file ]; then echo $file; fi; done
$(ls *.txt
,事实上也不应该这样做。有些人会将 ls
的默认设置用于长格式(比如我),而且 Shell 已经可以自己展开 *.txt
。所以只需要执行 for file in *.txt
就可以了。 - PrincessRTFMfor file in $(find . -name '*.txt'); do if [[ ! -s $file ]]; then echo $file; fi; done
- KlimczakM要检查文件是否为空或只包含空格,您可以使用grep:
if [[ -z $(grep '[^[:space:]]' $filename) ]] ; then
echo "Empty file"
...
fi
-s
选项也会在文件不存在的情况下显示文件为空。通过添加额外的检查 -f
来先检查该文件是否存在,我们可以确保结果的正确性。if [ -f diff.txt ]
then
if [ -s diff.txt ]
then
rm -f empty.txt
touch full.txt
else
rm -f full.txt
touch empty.txt
fi
else
echo "File diff.txt does not exist"
fi
检查文件是否为空的最简单方法:
if [ -s /path-to-file/filename.txt ]
then
echo "File is not empty"
else
echo "File is empty"
fi
您也可以在单行上编写:
[ -s /path-to-file/filename.txt ] && echo "File is not empty" || echo "File is empty"
if [[ -f diff.txt && -s diff.txt ]]
then
rm -f empty.txt
touch full.txt
elif [[ -f diff.txt && ! -s diff.txt ]]
then
rm -f full.txt
touch empty.txt
else
echo "File diff.txt does not exist"
fi
[[ -f filename && ! -s filename ]] && echo "filename exists and is empty"
# BASH4+ example on Linux :
typeset read_file="/tmp/some-file.txt"
if [ ! -s "${read_file}" ] || [ ! -f "${read_file}" ] ;then
echo "Error: file (${read_file}) not found.. "
exit 7
fi
如果$read_file为空或不存在,则使用exit停止展示。我曾多次错误地将这里的顶部答案误解为相反的意思。
例2:作为函数
# -- Check if file is missing /or empty --
# Globals: None
# Arguments: file name
# Returns: Bool
# --
is_file_empty_or_missing() {
[[ ! -f "${1}" || ! -s "${1}" ]] && return 0 || return 1
}
与@noam-manos基于grep
的答案类似,我使用cat
解决了这个问题。对于我来说,-s
无法工作,因为我的“空”文件大小大于0字节。
if [[ ! -z $(cat diff.txt) ]] ; then
echo "diff.txt is not empty"
else
echo "diff.txt is empty"
fi
'x']] && rm -rf /home;#
,那么这不会导致大问题吗? - Evi1M4chine'x' ]] && rm -rf /home; #
,这不会导致大问题吗? - anon
diff
调用,请检查返回值:if diff foo.txt bar.txt; then echo 'No difference'
。 - l0b0if [ ! -s diff.txt ]; then echo "为空"; else echo "有内容"; fi
- David Ramirez$ cat diff.txt | hexdump -C
命令检查文件。 - Alejandro Blasco[ … ]
与 bash 的本地[[ … ]]
不同,后者允许一些会破坏前者的事情。例如,在引用或比较运算符方面。 - anon