![文件命令的有用替代方案](https://linux22.com/image/1254730/%E6%96%87%E4%BB%B6%E5%91%BD%E4%BB%A4%E7%9A%84%E6%9C%89%E7%94%A8%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88.png)
我使用以下 shell 语法(来自我之前的问题,由 - phemient 回答)来验证文件是 ASCII(文本)还是其他
if LC_ALL=C grep -q '[^[:print:][:space:]]' file; then
echo "file contains non-ascii characters"
else
echo "file contains ascii characters only"
fi
问题是,即使 test_file 是 ASCII 文件,我也会得到“文件包含非 ascii 字符”(来自 shell 脚本语法),为什么?
我还用文件命令测试了文件,这就是我得到的结果
file test_file
Non-ISO extended-ASCII English text" its also ASCII file
我的问题是:如何更改 shell 语法以便也支持“非 ISO 扩展 ASCII 英文文本”?
所以我将从 shell 脚本中得到打印“文件仅包含 ascii 字符”
remark the solution must be for Linux and solaris
谢谢
答案1
你好我会使用类似的东西:
#!/bin/bash
if [[ `file -b $1` == "ASCII text" ]] ; then
echo "file contains ascii characters only"
else
echo "file contains non-ascii characters"
fi
好像:
$ filetest.sh 1.txt
file contains ascii characters only
$ filetest.sh PHOTOS/HPIM0532.jpg
file contains non-ascii characters