bashshell

if +/- plus number as a necessary condition


As a condition for the program I have to provide the value -number or +number. I would like to verify it and make sure it was entered correctly. How can I create a proper check rule?

Example:

if [ "$1" is -number ] || [ "$1" is +number ];then
    subsync -o "$1" "subtitle.srt"
fi

Solution

  • With bash and a regex:

    if [[ "$1" =~ ^[-+][0-9]+$ ]]; then
    

    See: 4.1. Regular expressions and The Stack Overflow Regular Expressions FAQ