【问题标题】:Bash: How to use functions with parameters with find and sshBash:如何通过 find 和 ssh 使用带参数的函数
【发布时间】:2021-01-23 09:02:35
【问题描述】:

我正在尝试在远程 ssh 客户端上搜索特定类型的文件,并希望使用作为函数参数传递的文件名来调用函数:

out=$(ssh operator@$IP << EOF
    check_cert_date () {
        echo "checking" $1
    }
    $(typeset -f)
    find /opt -iname *.der -o -iname *.pem  -exec bash -c 'for arg; do check_cert_date "$arg"; done' - {} \;
EOF
)

找到文件,但文件名本身没有传递给 check_cert_date(),即 $1 始终为空。

【问题讨论】:

    标签: bash shell ssh find


    【解决方案1】:

    注意引用“此处文档”。使用&lt;&lt; "EOF"

    此外,find 需要括号,以便您的操作同时应用于 *.der*.pem 文件:

    find /opt \( -iname *.der -o -iname *.pem \) -print | while read -r file; do check_cert_date "$file"; done
    

    【讨论】:

    • 谢谢,试过了,但仍然是同样的问题 - 只需为找到的所有文件打印“检查” - 即 1 美元一无所获。
    • 当您尝试不带| while…find 时,您会得到结果吗?
    • 哦,引用问题。将&lt;&lt; EOF 替换为&lt;&lt; "EOF"
    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多