免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2218 | 回复: 4
打印 上一主题 下一主题

因该如何判断一个文件是否为shell脚本文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-23 22:10 |只看该作者 |倒序浏览
我的思路是这样的
1 先判断这个文件可以执行
在1  的基础上判断这个文件的file 为 文本文件
不知道思路是否正确
我用
#!/bin/sh
for AA in `find /tmp -name "*"`
do
echo '$AA'=$AA
if [ -x $AA ] && [ file $AA|grep "ASCII" >/dev/zero 2>/dev/zero ]
then
echo -n "the file is shell"
else
echo -n "the file isn't shell"
fi
done
但是 会报错
不知道为什么

论坛徽章:
0
2 [报告]
发表于 2008-05-23 22:12 |只看该作者
file命令不行吗?

论坛徽章:
0
3 [报告]
发表于 2008-05-23 23:25 |只看该作者
有个思路,用rksh,但没试通,不明白原因:
sco_200:root[/tmp/test] 228 $ ./ls -l
total 206
-r-xr-xr-x   1 root     sys        20748 May 23 23:14 ls
-rwx--x--x   1 root     sys        80932 May 23 23:09 more
sco_200:root[/tmp/test] 229 $ echo $PATH
/test/tmp
sco_200:root[/tmp/test] 230 $ echo $HOME
/tmp/test
sco_200:root[/tmp/test] 231 $ (./ls -l|/bin/rksh   more)   
/bin/rksh: more: cannot execute
sco_200:root[/tmp/test] 232 $ (./ls -l|/bin/rksh  -c  more)
/bin/rksh: more:  not found
sco_200:root[/tmp/test] 233 $ (./ls -l|/bin/rksh  -c  ./more)
/bin/rksh: ./more: restricted
sco_200:root[/tmp/test] 234 $

论坛徽章:
0
4 [报告]
发表于 2008-05-24 11:08 |只看该作者
谢谢楼上的咯~~~

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2008-05-24 13:50 |只看该作者
脚本文件为普通文本文件, 看执行权限未必管用, 如果首行有魔数的话用file是可以判断的, 如果没有, 就不好说啦,
/home/lee#echo hello >a
/home/lee#file a
a: ASCII text
/home/lee#sed -i '1i#!/bin/bash' a
/home/lee#file a
a: Bourne-Again shell script text executable
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP