如何检查文件是否为ASCII文本文件-BASH SCRIPT

时间:2018-09-25 01:33:23

标签: bash scripting ascii

我有一个运行3个命令的Bash脚本。我担心的最后一个文件称为“ junk1.txt”。我想对if语句执行的检查做出补充。如果“ junk1.txt”是ASCII文本文件。我将如何去做?

./command1 > command1.txt

while read l;

do
    command2 > command2.txt

    while read m;

        do

            command3 junk.txt > junk1.txt 2>/tmp/err
            if [ -s /tmp/err ] #I WANT TO ADD ANOTHER CONDITION HERE
            then
                echo "not cracked"
            else
                echo "cracked"
                exit
            fi
        done < command2.txt

done < command1.txt

2 个答案:

答案 0 :(得分:2)

您可以使用file对此进行测试:

对于您的用例,将if [ -s /tmp/err ]行替换为:

if [ -s /tmp/err ] && [ "$(file junk1.txt)" = "junk1.txt: ASCII text" ]

答案 1 :(得分:1)

使用strings命令:

if diff <(strings ascii.txt) ascii.txt; then ...