【问题标题】:find: bad option -printf find: path-list predicate-list shell scripting查找:错误选项 -printf 查找:路径列表谓词列表 shell 脚本
【发布时间】:2013-12-04 14:21:49
【问题描述】:

我刚刚制作了一个非常基本的 shell 脚本,它接受一个输入路径并显示该路径中文件的属性。

问题:该脚本正在我的 PC 上运行,但是当我尝试在我的大学 UNIX 服务器上运行它时,我收到了一个错误:

find: bad option -printf
             find: path-list predicate-list
  • 我的电脑:Ubuntu
  • 我的大学服务器:SunOS nyx 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V210

代码:

#!/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

【问题讨论】:

  • Sun 上的旧查找版本不支持-printf 选项
  • 谢谢。有没有办法我可以用另一种方式实现它?
  • 尝试find /opt/sfw -name find -print 看看管理员是否在那里安装了 GNU find。
  • 它也可以在 /usr/sfw 或 /usr/local/bin 中,blastware.com 及其后继 optcsw.com 让您确定安装位置。

标签: bash shell solaris


【解决方案1】:

你在 Solaris -printf 和你的一般语法上有两个问题,

尝试:

find $ARGS -name '*.txt' -exec ls -l {} \; | nawk '{print $5, $(NR) }'

其中 ARGS 是目录而不是文件名。使用名称。接下来 printf - 你将不得不使用我给你的东西 - 将每个文件的 ls -l 管道传输到 nawk - 在 Solaris 上不是 awk - 并打印你想要的字段。字节是字段#5,所以'{print $5}' 可以做到这一点。最后一个字段$(NR) 是文件名

solaris 上的 awk 是一个非常古老的实现,您不能移植 unbuntu GNU awk 语法,并且通常让它在 solaris awk 上按照您想要的方式工作。

【讨论】:

  • 您应该留下find $ARG"/"*.txt,这实际上是正确的,并且可能提供与find $ARGS -name '*.txt'不同的输出
  • 除非在 Solaris 10 上 find 将其解释为目录,而不是 .txt 文件。因此,虽然它在语法上可能是合法的,但根据他对 ls 的结果,看起来 OP 想要 -name '*.txt' 或一些类似的变体。
  • Solaris find 不要求路径参数是目录。它肯定会在此处接受初始命令发送的文件列表(除非存在带有可疑.txt 后缀的目录)。如果 OP 想要报告位于子目录中的 .txt 文件,您的回复是正确的,但没有证据表明他想要(或不……)
【解决方案2】:

find -printf 的替代方法是在一组文件上使用stat --printf 以打印有关它们的有用信息。这是一个代码 sn-p,它排除了对 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

【讨论】:

  • Solaris 9 没有附带 stat。它可能存在也可能不存在。
  • @jimmcnamara 嗯,似乎有一个适用于 Solaris SPARC 9 系统的软件包 coreutils。不确定它是否包含stat
  • opencsw.org/use-it/sharing-optcsw 这确实有一个 findutils 包。如果你想要一个。 stat 被埋在那里的其他地方。 sunfreeware 也有 GNU coreutils,它有 find。但该网站现已被锁定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 2020-10-01
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多