HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?

Hello All,

Bash Version: 4.1.10(1)

I'm trying to "verify" some user input. The User input will contain the "Absolute Path" a "Command" and any "Options"
of that Command.

For Example, say the user's input is:

user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

I can't get my REGEX Pattern below to print an error when, say for example the user accidentally inputs 2 "/" consecutively by accident...

My REGEX Pattern/Code is: *Notice the double "//" after "/usr"...

user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

MY_PATTERN="((/{1,1}[[:alnum:]_-]*)+).*$"

if [[ "$user_input" =~ $MY_PATTERN ]]
 then
        echo "TRUE"
else
        echo "FALSE"
fi

So my question is how can I get this to print FALSE when the user enters a double "//"?
I thought the part of my REGEX that has "/{1,1}" would mean match one and ONLY one consecutive "/"...?

Any thoughts would be great..!

Thanks in Advance,
Matt

user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"

fixed_input=`echo ${user_input} | sed 's/\/*\//\//g'`

if [ "${user_input}" = "${fixed_input}" ]
then
        print "TRUE"
else
        print "FALSE"
fi
1 Like

Dont think bash has a "likes" operator "=~" which exists in perl so the basis of the if test is wrong...

Hey bipinajith, thanks for the reply...

Ah ha, that's a good idea!

Just so I'm clear what that does...
Will that remove any consecutive "/" and replace it with only one "/" and save the input into a NEW variable, then compare
the NEW var with the original...?

Sounds like a plan to me... Cool, Thanks!

Thanks Again,
Matt

---------- Post updated at 03:49 PM ---------- Previous update was at 03:44 PM ----------

Hey Shamrock, thanks for the reply...

Bash does have a "=~" Operator... Isn't that the REGEX Operator?

From the Bash Manpage:

Thanks,
Matt

---------- Post updated at 04:32 PM ---------- Previous update was at 03:49 PM ----------

Hey bipinajith,

Will I get the same result with your sed command and this one below..?

### the "\/{2,}" will 2 or more consecutive forward slashes "/"
tmp_input=$(echo "$user_input" | sed 's/\/\{2,\}/\//g' )

Is there any real difference between yours and mine..?

Thanks,
Matt

Yes, you will get the same result. Your command looks good.

1 Like

Ok my bad...so bash has that operator in any case if you want to look for the presence of two consecutive slashes then your regexp must be formed accordingly since that is what you want to find. Your regexp is looking for a single slash and $user_input contains it so it wont report false. I modified the code a bit to suit what you need to find...

user_input="/usr/local//myExample/check_process -p 'java' -w 10 -c 20"

MY_PATTERN="[/][/]+"

if [[ ! "$user_input" =~ "$MY_PATTERN" ]]
then
        echo "TRUE:  contains >= 2 consecutive slashes"
else
        echo "FALSE:  doesn't contain >= 2 consecutive slashes"
fi
1 Like

Ok Cool, thanks bipinajith!

Hey Shamrock, thanks for the reply!
Cool thanks, I'll give that a shot...

Thanks Again,
Matt