I was trying to write myself some handy scripts in order to
legitimately slacking off
work more efficiently, and this question suddenly popped up:
我试图给自己写一些方便的脚本,以便更有效地合法地放松工作,这个问题突然出现了:
Given a very long string $LONGEST_EVER_STRING
and several keywords strings like $A='foo bar'
, $B='omg bbq'
and $C='stack overflow'
给定一个非常长的字符串$ LONGEST_EVER_STRING和几个关键字字符串,如$ A ='foo bar',$ B ='omg bbq'和$ C ='堆栈溢出'
How should I use exact keyword matching as a condition in the case
statement?
我应该如何在case语句中使用完全关键字匹配作为条件?
for word in $LONGEST_EVER_STRING; do
case $word in
any exact match in $A) do something ;;
any exact match in $B) do something ;;
any exact match in $C) do something ;;
*) do something else;;
esac
done
I know I can write in this way but it looks really ugly:
我知道我可以这样写,但看起来很难看:
for word in $LONGEST_EVER_STRING; do
if [[ -n $(echo $A | fgrep -w $word) ]]; then
do something;
elif [[ -n $(echo $B | fgrep -w $word) ]]; then
do something;
elif [[ -n $(echo $C | fgrep -w $word) ]]; then
do something;
else
do something else;
fi
done
Does anyone have an elegant solution? Many thanks!
有人有优雅的解决方案吗?非常感谢!
3 个解决方案
#1
3
You could use a function to do a little transform in your A, B, C variables and then:
您可以使用函数在A,B,C变量中进行一些变换,然后:
shopt -s extglob
Ax="+(foo|bar)"
Bx="+(omg|bbq)"
Cx="+(stack|overflow)"
for word in $LONGEST_EVER_STRING; do
case $word in
$Ax) do something ;;
$Bx) do something ;;
$Cx) do something ;;
*) do something else;;
esac
done
#2
0
I would just define a function for this. It'll be slower than grep for large wordlists, but faster than starting up grep many times.
我只想为此定义一个函数。对于大型单词列表,它会比grep慢,但比启动grep多次快。
##
# Success if the first arg is one of the later args.
has() {
[[ $1 = $2 ]] || {
[[ $3 ]] && has "$1" "${@:3}"
}
}
$ has a b c && echo t || echo f
f
$ has a b c a d e f && echo t || echo f
t
#3
0
A variation on /etc/bashrc's "pathmunge"
/ etc / bashrc的“pathmunge”的变体
for word in $LONGEST_EVER_STRING; do
found_word=false
for list in " $A " " $B " " $C "; do
if [[ $list == *" $word "* ]]; then
found_word=true
stuff with $list and $word
break
fi
done
$found_word || stuff when not found
done
#1
3
You could use a function to do a little transform in your A, B, C variables and then:
您可以使用函数在A,B,C变量中进行一些变换,然后:
shopt -s extglob
Ax="+(foo|bar)"
Bx="+(omg|bbq)"
Cx="+(stack|overflow)"
for word in $LONGEST_EVER_STRING; do
case $word in
$Ax) do something ;;
$Bx) do something ;;
$Cx) do something ;;
*) do something else;;
esac
done
#2
0
I would just define a function for this. It'll be slower than grep for large wordlists, but faster than starting up grep many times.
我只想为此定义一个函数。对于大型单词列表,它会比grep慢,但比启动grep多次快。
##
# Success if the first arg is one of the later args.
has() {
[[ $1 = $2 ]] || {
[[ $3 ]] && has "$1" "${@:3}"
}
}
$ has a b c && echo t || echo f
f
$ has a b c a d e f && echo t || echo f
t
#3
0
A variation on /etc/bashrc's "pathmunge"
/ etc / bashrc的“pathmunge”的变体
for word in $LONGEST_EVER_STRING; do
found_word=false
for list in " $A " " $B " " $C "; do
if [[ $list == *" $word "* ]]; then
found_word=true
stuff with $list and $word
break
fi
done
$found_word || stuff when not found
done