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
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
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'
a=( $line )is a dangerous practice for the reasons described in BashPitfalls #50 (while the pitfall refers to command substitutions, unquoted parameter expansions behave identically for purposes thereof) -- better to make itwhile read -r -a a; doand havereadsplit the words of your line into array elements itself.