如何写这个命令?

sh/bash/dash/ksh/zsh等Shell脚本
回复
zhusongd
帖子: 8
注册时间: 2010-10-21 19:54

如何写这个命令?

#1

帖子 zhusongd » 2010-10-29 22:46

例如我查找一个名字为x的文件,然后用file命令判断文件x的文件类型
我用这样的管道命令好像不行,
find / -name x | file
如何写这段管道命令??
zhusongd
帖子: 8
注册时间: 2010-10-21 19:54

Re: 如何写这个命令?

#2

帖子 zhusongd » 2010-10-29 22:50

我知道这样的命令可以这样写
find / -name x | xargs file

还有没有其他的方法??
daniel.supremacy
帖子: 133
注册时间: 2008-12-12 15:35

Re: 如何写这个命令?

#3

帖子 daniel.supremacy » 2010-10-29 23:02

find / -name x -exec file {} \;
zhusongd
帖子: 8
注册时间: 2010-10-21 19:54

Re: 如何写这个命令?

#4

帖子 zhusongd » 2010-10-29 23:14

这个命令参数也知道,能不能有一种方法,通过变量传递参数
头像
bzhao
帖子: 250
注册时间: 2008-07-05 2:15
系统: XUbuntu

Re: 如何写这个命令?

#5

帖子 bzhao » 2010-10-30 2:37

zhusongd 写了:这个命令参数也知道,能不能有一种方法,通过变量传递参数
你的意思是这个?

#!/bin/sh

find -name xxx > tmpfile

for i in `cat tmpfile`
do
file $i
done
rm tmpfile
znotft
帖子: 5
注册时间: 2010-08-31 1:42

Re: 如何写这个命令?

#6

帖子 znotft » 2010-10-30 3:21

zhusongd 写了:这个命令参数也知道,能不能有一种方法,通过变量传递参数
可以这样。
先定义一个变量 a, 再用a传递参数。
a=`find -name x`; file $a
daniel.supremacy
帖子: 133
注册时间: 2008-12-12 15:35

Re: 如何写这个命令?

#7

帖子 daniel.supremacy » 2010-10-30 8:49

上面两楼都不能处理有空格的文件吧 :em06
高人指点迷津
头像
bzhao
帖子: 250
注册时间: 2008-07-05 2:15
系统: XUbuntu

Re: 如何写这个命令?

#8

帖子 bzhao » 2010-10-30 10:26

daniel.supremacy 写了:上面两楼都不能处理有空格的文件吧 :em06
高人指点迷津
你试试这个!

代码: 全选

#!/bin/sh
find -name "xxx yyy"> tmpfile
IFS="\n"
for i in `cat tmpfile`
do
file $i
done
rm tmpfile
daniel.supremacy
帖子: 133
注册时间: 2008-12-12 15:35

Re: 如何写这个命令?

#9

帖子 daniel.supremacy » 2010-10-30 10:50

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
头像
bzhao
帖子: 250
注册时间: 2008-07-05 2:15
系统: XUbuntu

Re: 如何写这个命令?

#10

帖子 bzhao » 2010-10-30 12:19

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?
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 如何写这个命令?

#11

帖子 tusooa » 2010-10-30 13:37

代码: 全选

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

代码: 全选

] ls -ld //
头像
szl1997
帖子: 292
注册时间: 2009-11-24 14:12

Re: 如何写这个命令?

#12

帖子 szl1997 » 2010-11-03 14:21

file $(find / -name x)
/目录可能还要sudo
男儿心比铁,纵死亦千钧!
回复