是否可以将参数扩展与bash中的算术表达式结合起来?例如,我可以做一个单行来评估lineNum
还是numChar
在这里?
echo "Some lines here
Here is another
Oh look! Yet another" > $1
lineNum=$( grep -n -m1 'Oh look!' $1 | cut -d : -f 1 ) #Get line number of "Oh look!"
(( lineNum-- )) # Correct for array indexing
readarray -t lines < $1
substr=${lines[lineNum]%%Y*} # Get the substring "Oh look! "
numChar=${#substr} # Get the number of characters in the substring
(( numChar -= 2 )) # Get the position of "!" based on the position of "Y"
echo $lineNum
echo $numChar
> 2
8
换句话说,我可以根据单行表达式中另一个字符的位置来获取字符串中一个字符的位置吗?