If loop query

Hi, its getting aborted with below IF loop, can plz guide me what im missing here

        if [ -z "${APPEND}" ] || [ $($APPEND+1) % 3 -eq 0 ]; then
                echo "print $APPEND"
        fi

What operating system are you using?

What shell are you using?

There is no loop in the above code. What loop are you talking about?

Please describe in English what you want the code:

[ $($APPEND+1) % 3 -eq 0 ]

to do and what values might be assigned to the variable APPEND . The code that you have here is extremely likely to give you a command not found error and a syntax error unless APPEND has not been assigned, has been assigned an empty string as a value, or has been assigned a string value such that that string value appended with the string +1 names an executable file on your command search path (in which case you'll get a syntax error from the % operator in your test command).

If is not loop. Its conditional statement.

I guess, APPEND is having string.

please try this

#!/bin/bash

echo -n "Enter the String : "
read APPEND
LENGTH=${#APPEND}
if [[ -z "${APPEND}" || $((LENGTH+1))%3 -eq "0" ]]
then
      echo "print $APPEND"
fi
1 Like

@itkamaraj: You may want to phrase the arithmetic expansion like $(( (LENGTH+1)%3 )) to become an integer value, and using the mod function on LENGTH will fold back if APPEND passes 1000...?
@JSKOBS: it is always helpful if you explain in detail what "getting aborted" means, like adding error messages and input conditions.

1 Like