2

I have a String of command as below:

CMD_LAUNCH="launch.sh \
    -v $ABC_VERSION \
    -p something \
    -X $MAX_HEAP_SPACE_PARAM \
    -Dpersist false \
    -DanotherPasram Abc"

I will launch this command in ksh as below:

$CMD_LAUNCH

How can I make sure the command has -Dpersist false ?

I want to cover the cases where there can be any no of spaces between -Dpersist and false. but my attempt fails to accomplish this.

Try 1)

if [[ "$CMD_LAUNCH" = *"Dpersist\s+false"* ]]
then
        echo "It's there!"
else
        echo "It's not there!"
fi

I want to test if Dpersist false is present in command.

3
  • did this using if [[ $ALCYONE_LAUNCHER_CMD = Dpersist+(' ')false ]] is there a better way to represent space than ' ', also how to do I cover tabs as well Commented Mar 13, 2018 at 8:02
  • Thanks for input!. tried that it doesn't work.looks like it's not the regex, it's something like pattern in ksh Commented Mar 13, 2018 at 8:31
  • 1
    Why are you using a string instead of a function in the first place? Commented Mar 14, 2018 at 13:16

1 Answer 1

3

Solution 1:

if [[ "$CMD_LAUNCH" == *+(Dpersist+(\s)false)* ]]
then
        echo "It's there!"
else
        echo "It's not there!"
fi

Ksh's pattern matching is different to regex as it will always match the whole string - like regex starting with ^ and ending with $. Therefore you have to enclose the pattern, which itself is enclosed in parentheses, with asterisks. The * matches any sequence of characters. The + in front of each pattern means match 1 or more occurrences of the pattern.

Solution 2:

Another option is to use the =~ operator:

if [[ "$CMD_LAUNCH" =~ Dpersist\s+false ]]                                            
then
    echo "Its there!"
else
     echo "Its not there!"
fi

=~ uses regex syntax.

Resources

For more examples see

Sidenote

Also check out ShellCheck, it helps greatly finding errors in your shell scripts.

Sign up to request clarification or add additional context in comments.

1 Comment

Solution one fails for multiple cases: 1)ksh k.sh "a.sh -Dpersist true false" 2) ksh k.sh "a.sh -Dpersistfalse" 3) ksh k.sh "a.sh"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.