How to compare strings in ksh

28,532

Solution 1

You should always double quote variables. And you need = for string equals. So:

if [ "$check1" = "[FAILED]" ]; then

Solution 2

You are doing an arithmetic comparison by using -eq leading to the error, you need to do string comparison by using = (or == inside [[), and for that using quotes is enough:

[ "$check1" = "[[FAILED]" ]
[[ "$check1" = "[[FAILED]" ]]
Share:
28,532

Related videos on Youtube

Vince
Author by

Vince

Updated on September 18, 2022

Comments

  • Vince
    Vince over 1 year

    I want to check the result of a job and execute an action on FAILED.

    First: I grep the last word of the line in my application logfile (for the recent processed file ($processedfilename)):

    check1=$(grep "$processedfilename" "$logfile" | grep "anotherword" | \
        grep "FAILED" | tail -1 | awk '{print $NF}')
    

    This results in [FAILED].

    Now I want to check on the result

    if [ $check1 -eq "[[FAILED]" ] 
    then
    

    or

    if [ $check1 -eq "\[FAILED]" ] 
    then
    

    There's always arithmetic syntax error.

    What's the correct syntax to check on [FAILED]?

  • heemayl
    heemayl over 7 years
    @AlexejMagura That is mentioned in the answer. From answer: == (inside [[)