bash 查找:错误选项 -printf 查找:路径列表谓词列表 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20377804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find: bad option -printf find: path-list predicate-list shell scripting
提问by rbk
I've just made a very basic shell script which takes a input path and displays the attributes of the files in that path.
我刚刚制作了一个非常基本的 shell 脚本,它采用输入路径并显示该路径中文件的属性。
Problem: the script is running on my PC, but when I try to run it on my college UNIX server I'm getting an error:
问题:脚本正在我的 PC 上运行,但是当我尝试在我的大学 UNIX 服务器上运行它时,我收到一个错误:
find: bad option -printf
find: path-list predicate-list
- My PC: Ubuntu
- My college server: SunOS nyx 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V210
- 我的电脑:Ubuntu
- 我的大学服务器:SunOS nyx 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V210
The code:
编码:
#!/bin/bash
echo " enter address in form : /home/rohan/../.."
read ARG
if [ -n "$ARG" ]; then
echo "input path taken : $ARG"
# ls -lsh $ARG"/"*.txt
else
ARG=$(pwd)
fi
echo " enter option "
echo " 1. file size, 2. permission, 3. owner/group, 4. all , 5. exit"
read OPTION
while [ $OPTION != "5" ]
do
if [ $OPTION = "1" ]; then
find $ARG"/"*.txt -printf " %p %s bytes \n"
elif [ $OPTION = "2" ]; then
find $ARG"/"*.txt -printf " %p %M \n"
elif [ $OPTION = "3" ]; then
find $ARG"/"*.txt -printf " %p %g \n"
elif [ $OPTION = "4" ]; then
find $ARG"/"*.txt -printf "%p %s bytes %M %g \n"
fi
echo "enter option again"
echo " 1. file size, 2. permission, 3. owner/group, 4. all , 5. exit"
read OPTION
done
回答by jim mcnamara
You have two problems on Solaris -printf and your general syntax,
你在 Solaris -printf 和你的一般语法上有两个问题,
try:
尝试:
find $ARGS -name '*.txt' -exec ls -l {} \; | nawk '{print , $(NR) }'
Where ARGS is a directory not a filename. Use -name. Next printf - you will have to use something like what I gave you - pipe ls -l for each file into nawk - NOT awk on Solaris - and print the fields you want. bytes are field #5, so '{print $5}'
works for that. the last field $(NR)
is the filename
其中 ARGS 是目录而不是文件名。使用名称。接下来 printf - 你将不得不使用类似我给你的东西 - 将每个文件的 ls -l 管道传输到 nawk - 而不是 Solaris 上的 awk - 并打印你想要的字段。字节是字段 #5,因此'{print $5}'
适用。最后一个字段$(NR)
是文件名
awk on solaris is a very very old implementation and you cannot port unbuntu GNU awk syntax and generally have it work the way you want on solaris awk.
awk 在solaris 上是一个非常非常古老的实现,你不能移植unbuntu GNU awk 语法,并且通常让它在solaris awk 上按照你想要的方式工作。
回答by jim mcnamara
An alternative to find -printf
is to use stat --printf
on a set of files to print useful info about them. This is a code snippet that factors out calls to find
and uses shell glob to match the set of files:
另一种方法find -printf
是stat --printf
在一组文件上使用以打印有关它们的有用信息。这是一个代码片段,它分析出调用find
并使用 shell glob 来匹配文件集:
# ...
usage() {
echo "enter option again"
# ...
}
# read options
while read OPTION; do
format=""
case $OPTION in
1) format="%n %s bytes\n" ;;
2) format="%n %A\n" ;;
3) format="%n %U/%G\n" ;;
4) format="%n %s bytes %A %U/%G\n" ;;
5) exit ;;
*) usage ;;
esac
# print file info
shopt -s nullglob
for file in "$ARG"/*.txt; do
stat --printf "$format" "$file"
done
done