I am trying to do multi condition check i.e., the input should not be empty and it should contain only numbers in while loop in Shell Script but i am facing multiple errors while trying to perform this operation. I have tried multiple options and i see multiple questions on this posted in Unix Stackexchange and Stack overflow but none of them helping that's why i am posting this question here Option 1:
while [ \( -z "$account_number" \) && \( "$account_number" =~ "^[0-9]+$" \) ];
do printf 'Input account Number RWDEx: '
read -r account_number
[ -z "$account_number" ] && echo 'Account Number cannot be empty; try again.'
done
This is throwing error [: missing `]' many suggested this error because of the space issue between condition and brackets but it didn't work
Option 2:
while [[ -z "$account_number" && "$account_number" =~ "^[0-9]+$" ]]
do printf 'Input account Number RWDEx: '
read -r account_number
[ -z "$account_number" ] && echo 'Account Number cannot be empty; try again.'
done
If i giving condition as above it not even asking for the input
Please help in solving this issue and i am using #!/bin/bash i tried even changing the script to /bin/bash instead of #!/bin/bash
$account_number
can be both empty (-z
) and (&&
) match^[0-9]+$
. Your logic is flawed. Note that only the[
builtin ofyash
andzsh
support a=~
operator.