分页: 1 / 1

如何写这个命令?

发表于 : 2010-10-29 22:46
zhusongd
例如我查找一个名字为x的文件,然后用file命令判断文件x的文件类型
我用这样的管道命令好像不行,
find / -name x | file
如何写这段管道命令??

Re: 如何写这个命令?

发表于 : 2010-10-29 22:50
zhusongd
我知道这样的命令可以这样写
find / -name x | xargs file

还有没有其他的方法??

Re: 如何写这个命令?

发表于 : 2010-10-29 23:02
daniel.supremacy
find / -name x -exec file {} \;

Re: 如何写这个命令?

发表于 : 2010-10-29 23:14
zhusongd
这个命令参数也知道,能不能有一种方法,通过变量传递参数

Re: 如何写这个命令?

发表于 : 2010-10-30 2:37
bzhao
zhusongd 写了:这个命令参数也知道,能不能有一种方法,通过变量传递参数
你的意思是这个?

#!/bin/sh

find -name xxx > tmpfile

for i in `cat tmpfile`
do
file $i
done
rm tmpfile

Re: 如何写这个命令?

发表于 : 2010-10-30 3:21
znotft
zhusongd 写了:这个命令参数也知道,能不能有一种方法,通过变量传递参数
可以这样。
先定义一个变量 a, 再用a传递参数。
a=`find -name x`; file $a

Re: 如何写这个命令?

发表于 : 2010-10-30 8:49
daniel.supremacy
上面两楼都不能处理有空格的文件吧 :em06
高人指点迷津

Re: 如何写这个命令?

发表于 : 2010-10-30 10:26
bzhao
daniel.supremacy 写了:上面两楼都不能处理有空格的文件吧 :em06
高人指点迷津
你试试这个!

代码: 全选

#!/bin/sh
find -name "xxx yyy"> tmpfile
IFS="\n"
for i in `cat tmpfile`
do
file $i
done
rm tmpfile

Re: 如何写这个命令?

发表于 : 2010-10-30 10:50
daniel.supremacy
bzhao 写了: 你试试这个!

代码: 全选

#!/bin/sh
find -name "xxx yyy"> tmpfile
IFS="\n"
for i in `cat tmpfile`
do
file $i
done
rm tmpfile
麻烦先test再发上来,我把处理结果放这里:

代码: 全选

$ ls -lh test*
-rw-r--r-- 1 daniel daniel   0 2010-10-30 10:46 test file
-rw-r--r-- 1 daniel daniel   0 2010-10-30 10:46 test saf
-rwxr-xr-x 1 daniel daniel 102 2010-10-30 10:46 test.sh

代码: 全选

$ cat test.sh 
#!/bin/sh

find -name "test*" > tmpfile
IFS="\n"
for i in `cat tmpfile`
do
	file $i
done
rm tmpfile

代码: 全选

$ ./test.sh 
./test saf
./test file
./test.sh: ERROR: cannot open `./test saf
./test file
./test.sh' (No such file or directory)
============编辑的封个先==============
这个可以运行

代码: 全选

$ cat test.sh 
#!/bin/sh

find -name "test*" > tmpfile

IFS="
"

for i in `cat tmpfile`
do
	file $i
done
rm tmpfile

代码: 全选

$ ./test.sh 
./test saf: empty
./test file: empty
./test.sh: POSIX shell script text executable

Re: 如何写这个命令?

发表于 : 2010-10-30 12:19
bzhao
daniel.supremacy 写了:
bzhao 写了: 你试试这个!

代码: 全选

#!/bin/sh
find -name "xxx yyy"> tmpfile
IFS="\n"
for i in `cat tmpfile`
do
file $i
done
rm tmpfile
麻烦先test再发上来,我把处理结果放这里:

代码: 全选

$ ls -lh test*
-rw-r--r-- 1 daniel daniel   0 2010-10-30 10:46 test file
-rw-r--r-- 1 daniel daniel   0 2010-10-30 10:46 test saf
-rwxr-xr-x 1 daniel daniel 102 2010-10-30 10:46 test.sh

代码: 全选

$ cat test.sh 
#!/bin/sh

find -name "test*" > tmpfile
IFS="\n"
for i in `cat tmpfile`
do
	file $i
done
rm tmpfile

代码: 全选

$ ./test.sh 
./test saf
./test file
./test.sh: ERROR: cannot open `./test saf
./test file
./test.sh' (No such file or directory)
============编辑的封个先==============
这个可以运行

代码: 全选

$ cat test.sh 
#!/bin/sh

find -name "test*" > tmpfile

IFS="
"

for i in `cat tmpfile`
do
	file $i
done
rm tmpfile

代码: 全选


$ ./test.sh 
./test saf: empty
./test file: empty
./test.sh: POSIX shell script text executable

我的IFS="\n"是work的, 这个可能是sh的版本引起的:
我的系统是 Ubuntu10.04
#ls /bin/sh
lrwxrwxrwx 1 root root 4 2010-10-30 12:12 /bin/sh -> bash
$sh --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

你用的是什么系统? 什么shell?

Re: 如何写这个命令?

发表于 : 2010-10-30 13:37
tusooa

代码: 全选

find | while read line ; do file "$line" ; done

Re: 如何写这个命令?

发表于 : 2010-11-03 14:21
szl1997
file $(find / -name x)
/目录可能还要sudo