#1
The =~ Regular Expression match operator no longer requires quoting
of the pattern within [[ … ]].
把测试条件换成下面这样就成功了。
if [[ “$newip” =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]];then
….
fi
使用变量的时候,是可以使用引号的:
newip=’192.168.1.100’
reg=’^([0-9]{1,3}.){3}[0-9]{1,3}$’
if [[ \$newip =~ \$reg ]]
then
echo ‘找到了ip地址’
fi
note: above “\” does not need, but if remove them, “$” will disappeared.
但是,对于 $reg 这个变量,在 [[]] 操作符中,就不能加上双引号,否则也会不匹配。
#2
item=”11”
contain =”11,22,33,44”
if [[ $contain =~ $item ]]
then
echo “item is included in contain”
fi
# above code which is used to check if contain inlaced item
note: pass parameters to scripts of shell
while
IFS= read line
do
echo $line
done < data.file
note: remove blank row below
tr -s ‘\n’
# if contain space in the row, need blow code:
IFS=$’\n’
for line in cat test.log
do
echo $line
done