linux Bash查找命令:无此文件或目录

busg9geu  于 2023-01-16  发布在  Linux
关注(0)|答案(1)|浏览(222)

这一条把我难住了!决议的要点!
我需要遍历一个目录及其下的所有级别,以发现任何不匹配模式的文件或目录。
我在用查找命令-

find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls

错误-

find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls: No such file or directory

是的,find命令里的每个目录都存在-

ls -ld /home/user/inbox /home/user/inbox/imaging/
drwxr-xr-x. 5 user grp-ftp-admin 8192 Jan 13 12:45 /home/user/inbox
drwxr-xr-x. 3 user grp-ftp-admin  152 Jan 13 12:21 /home/user/inbox/imaging/

整个脚本源代码-

#!/usr/bin/env bash

function findInvalidArtifacts() {
    unset   inbox sshCommand exitCode cmdOutput
    s ilocal    -A VENDOR=(
                ['FTPBASE']="/home/user"
                ['FTPSOURCEDIR']="/home/user/inbox/imaging"
                ['FRIENDLYNAME']="user"
                ['ENVIRONMENT']="dev"
            )

    inbox="${VENDOR['FTPBASE']}/inbox"
    printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" \
        "${inbox}" "${inbox}" "${inbox}" "${VENDOR['FTPSOURCEDIR']}" "${VENDOR['FTPSOURCEDIR']}"
    #cmdOutput=$(ssh ${SSHOPTIONS} ${SSHUSERHOST} "${sshCommand}" 2>&1)
    cmdOutput=$("${sshCommand}")
    exitCode="${?}"
    if (( 0 != $exitCode )); then
        printf "FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n" "${sshCommand}" "${exitCode}" "${cmdOutput}"
        exit 1
    fi
    printf "SUCCEEDED - sshCommand=[%s] exitCode=[%d]\n" "${sshCommand}" "${exitCode}"
    printf "%s\n\n" "${cmdOutput}"
} # end findInvalidArtifacts()

最后,使用bash -vx运行脚本-

#!/usr/bin/env bash

function findInvalidArtifacts() {
        unset   inbox sshCommand exitCode cmdOutput
        local   -A VENDOR=(
                                ['FTPBASE']="/home/user"
                                ['FTPSOURCEDIR']="/home/user/inbox/imaging"
                                ['FRIENDLYNAME']="user"
                                ['ENVIRONMENT']="dev"
                        )

        inbox="${VENDOR['FTPBASE']}/inbox"
        printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" \
                "${inbox}" "${inbox}" "${inbox}" "${VENDOR['FTPSOURCEDIR']}" "${VENDOR['FTPSOURCEDIR']}"
        #cmdOutput=$(ssh ${SSHOPTIONS} ${SSHUSERHOST} "${sshCommand}" 2>&1)
        cmdOutput=$("${sshCommand}")
        exitCode="${?}"
        if (( 0 != $exitCode )); then
                printf "FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n" "${sshCommand}" "${exitCode}" "${cmdOutput}"
                exit 1
        fi
        printf "SUCCEEDED - sshCommand=[%s] exitCode=[%d]\n" "${sshCommand}" "${exitCode}"
        printf "%s\n\n" "${cmdOutput}"
} # end findInvalidArtifacts()

findInvalidArtifacts
+ findInvalidArtifacts
+ unset inbox sshCommand exitCode cmdOutput
+ VENDOR=(['FTPBASE']="/home/user" ['FTPSOURCEDIR']="/home/user/inbox/imaging" ['FRIENDLYNAME']="user" ['ENVIRONMENT']="dev")
+ local -A VENDOR
+ inbox=/home/user/inbox
+ printf -v sshCommand 'find -L %s \( ! -path %s -a ! -path '\''%s/*'\'' -a ! -path %s -a ! -path '\''%s/*'\'' \) -ls' /home/user/inbox /home/user/inbox /home/user/inbox /home/user/inbox/imaging /home/user/inbox/imaging
++ 'find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '\''/home/user/inbox/*'\'' -a ! -path /home/user/inbox/imaging -a ! -path '\''/home/user/inbox/imaging/*'\'' \) -ls'
./try.sh: line 16: find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls: No such file or directory
+ cmdOutput=
+ exitCode=127
+ ((  0 != 127  ))
+ printf 'FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n' 'find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '\''/home/user/inbox/*'\'' -a ! -path /home/user/inbox/imaging -a ! -path '\''/home/user/inbox/imaging/*'\'' \) -ls' 127 ''
FAILED - sshCommand=[find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls], exitCode=[127], cmdOutput=[]
+ exit 1

这是一个难题。我不明白为什么find会抱怨“没有这样的文件或目录”。

yzuktlbb

yzuktlbb1#

相关片段:

printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" [more args]
cmdOutput=$("${sshCommand}")

这是trying to shove a command into a variable and run it的一个经典陷阱,基本的解决方法是使用函数,而不是变量。
出现错误的原因是Bash在完成所有解析步骤后通过选择命令行的第一个单词来确定要运行的命令的名称。您希望使用带有一些参数的命令find,但是由于"${sshCommand}"用引号括起来了,整个命令行中只有一个单词,所以Bash认为您需要的命令名为find -L /home/dir ( ! -path etc etc... )。我敢打赌,您的系统中没有任何具有该名称的可执行文件。
与您尝试执行的操作等效的函数可能如下所示:

_findCmd() {
  searchDir=$1 # first arg is the directory to search from
  shift
  
  # each extra argument is a directory to exclude from the search
  # we can loop over the list and build the arguments that find needs
  # in an array
  path_args=()
  if (( $# > 0 )) ; then
    path_args+=( "(" "!" "-path" "$1" )
    shift
    while (( $# > 0 )) ; do 
      path_args+=( "-a" "!" "-path" "$1" )
      shift
    done
    path_args+=( ")" )
  fi
  find -L "$searchDir" "${path_args[@]}" -ls
} 

cmdOutput=$( _findCmd /home/user/inbox /home/user/inbox "/home/user/inbox/*" /home/user/inbox/imaging "/home/user/inbox/imaging/*" )

相关问题