我目前正在創建一個數據庫在bash中。我想添加一個「和」功能。這個函數將搜索兩件事,並且只返回匹配兩個搜索的東西。我將如何實現這一目標?grep多個東西出故障
這裏是我的代碼:
#!/bin/bash
clear
#return all
function all {
cat people.dat
}
#return some
function search {
grep -i "$SEARCH" people.dat || echo "search returned nothing"
}
#or search
function or {
egrep -i "$search1|$search2" people.dat
}
#and search
function and {
if [[ $(grep -i "$search1") = $(grep -i "$search2") ]]; then
echo yes
#that is temporary I want to see if it worked
fi
}
#return null
function null {
return
}
while [ true ]
do
#get the search
read SEARCH
#search
if [[ $SEARCH == *" OR "* ]]; then
search1=${SEARCH%" OR "*}
search2=${SEARCH##*" OR "}
or
elif [[ $SEARCH == *" AND "* ]]; then
search1=${SEARCH%" AND "*}
search2=${SEARCH##*" AND "}
and
elif [ "$SEARCH" = "all" }; then
all
elif [ "$SEARCH" = "exit" ]; then
exit
elif [ "$SEARCH" = "" ]; then
null
else
search
fi
done
這是功課?我真的希望你不打算在Bash中實現生產數據庫... – l0b0
你可以像這樣使用sed:'sed -n「/ $ a/{/ $ b/p}」'用於AND和'sed - n「/ $ a \ | $ b/p」'爲OR – andlrc
搜索字符串是否可以重疊?例如:'search1 =「a b」''search2 =「b c」'string =「a b c」。這裏'grep「$ {search1}。* $ {search2}」<<<「$ {string}」'將會失敗。 –