gpt4 book ai didi

Bash:检查条目是否为一行的 native 方法?

转载 作者:行者123 更新时间:2023-11-29 09:16:37 25 4
gpt4 key购买 nike

我有一个查找脚本,如果只找到一个文件,它会自动打开一个文件。我目前处理它的方式是对搜​​索结果的行数进行字数统计。有更简单的方法吗?

if [ "$( cat "$temp" | wc -l | xargs echo )" == "1" ]; then
edit `cat "$temp"`
fi

已编辑 - 这是整个脚本的上下文。

term="$1"
temp=".aafind.txt"

find src sql common -iname "*$term*" | grep -v 'src/.*lib' >> "$temp"

if [ ! -s "$temp" ]; then
echo "ø - including lib..." 1>&2
find src sql common -iname "*$term*" >> "$temp"
fi


if [ "$( cat "$temp" | wc -l | xargs echo )" == "1" ]; then
# just open it in an editor
edit `cat "$temp"`
else
# format output
term_regex=`echo "$term" | sed "s%\*%[^/]*%g" | sed "s%\?%[^/]%g" `
cat "$temp" | sed -E 's%//+%/%' | grep --color -E -i "$term_regex|$"
fi

rm "$temp"

最佳答案

除非我误解了,变量 $temp 包含一个或多个文件名,每行一个,如果只有一个文件名,是否应该对其进行编辑?

[ $(wc -l <<< "$temp") = "1" ] && edit "$temp"

如果 $temp 是一个包含文件名的文件:

[ $(wc -l < "$temp") = "1" ] && edit "$(cat "$temp")"

关于Bash:检查条目是否为一行的 native 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21444268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com