I am trying to compare a string with the $ character, followed by either s or t and a range of numbers. but the range is different for each letter. for example if it is an s it can be followed by numbers between 0-7 and if it is a t it can be followed by numbers between 0-9.
What I managed to write a part of the if statement to compare it, but I couldn't figure out how to include different ranges for each letter
script:
#!/bin/bash
input="test1.txt"
check(){
while read -r line; do
a=( $line )
for i in "${a[@]:1}"; do
if [[ "$i" == \$[st]* ]]; then
echo "$i"
fi
done
done < "$input"
}
check
Run Code Online (Sandbox Code Playgroud)
Instead of using * I want to specify for s that it can only be followed by numbers between 0-7 and t can only be followed by numbers 0-9. I tried using this:
if [[ "$i" == \$(s[0-7]*|t[0-9]*) ]]; then
Run Code Online (Sandbox Code Playgroud)
but I got this error:
./test.sh: line 9: syntax error in conditional expression: unexpected token `(' ./test.sh: line 9: syntax error near `\$(s' ./test.sh: line 9: `if [[ "$i" == \$(s[0-7]*|t[0-9]*) ]]; then'
Run Code Online (Sandbox Code Playgroud)
=~
用于正则表达式匹配,不用于==
。更正该错误并|
在正则表达式中使用竖线(),即OR。
if [[ $i =~ \$(s[0-7]*|t[0-9]*) ]]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
150 次 |
最近记录: |