2

I want to do an if then statement: If variable i is equal to the regex for 0 through 9 AND if grep does not return any results, then do something. Is this the correct syntax?

if [[ $i =~ [0-9] ]] && if ! grep $i /opt/tftpboot/*; then

or is it

if [[ $i =~ [0-9] ]] && ! [[ grep $i /opt/tftpboot/* ]]; then
  • Note that if in a shell is somewhat different to other languages: what follows if are commands (see gnu.org/software/bash/manual/…), not any special syntax. [[ is a bash/ksh/zsh built-in command. The exit status of the commands is used by if. – glenn jackman Oct 5 '15 at 15:16
4

It's:

if [[ $i =~ [0-9] ]] && ! grep $i /opt/tftpboot/*; then

You probably don't need the output of grep, in which case you can do:

if [[ $i =~ [0-9] ]] && ! grep -q $i /opt/tftpboot/*; then
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.