如何在bash脚本中使用for循环匹配正则表达式?

时间:2017-10-12 11:19:30

标签: regex bash shell

我试图从命令输出中匹配多个字符串,并为每个字符串做一些事情。

#!/usr/bin/env bash
echo 'Howdy, can you please give me the domain (without www)?'
read domain
routes=$(flynn -a shop-app route | grep $domain)
# echo $routes | egrep "http\/\S+"

pattern="http\/[^ ]+"
for word in $routes
do
    [[ $word =~ $pattern ]]
if ${BASH_REMATCH[0]}
then
    match="${BASH_REMATCH[0]}"
    sed -i s/DOMAIN/$domain/g $domain.sh
    sed -i s:ROUTE1:$match:g $domain.sh

fi

if ${BASH_REMATCH[1]}
then
    match2="${BASH_REMATCH[1]}"
    sed -i s:ROUTE2:$match2:g $domain.sh

fi

done

echo $match

更新:正则表达式部分现在可以工作,但循环不起作用。我知道循环会找到两个匹配,并希望对每个匹配

示例文字:

http:www.lipi.ir            shop-app-web  http/d49ced12-c6ca-46a0-b919-6d97b6580ad3  false   false   / 
http:lipi.ir                shop-app-web  http/ff919e9d-9bf7-4342-a4b3-ea184c698959  false   false   /

0 个答案:

没有答案