So, I have an exercise in which I need to calculate a specific string.
For example, the string "|| x ||| + ||". The vertical lines are 1. So the solution to this line should be 8. So I made this script:
#!/bin/bash
result=$(echo $1 | tr -d ' ' | sed 's/|/1+/g' | sed 's/++/+/g' | sed 's/+x/x/g' | sed 's/ sed 's/x/*/g' | sed 's/+$//' | sed 's/$/\n/' | bc)
But when I ran this script in the sample line, I got 6. Then I realized, because the script is doing this: 1 + 1 * 1 + 1 + 1 + 1 + 1. Therefore, I need to make the brackets between (1 + 1) * (1 + 1 + 1) + (1 + 1), but I can’t figure it out.
Can someone help me? Thank you in advance!
source
share