Devansh Agarwal
Devansh Agarwal

Reputation: 126

If statement syntax error (shell)

if [[ ! -z grep echo "${prof}" | cut -d. -f1 dm_smear.dat ]]

This gives me the following error. I am trying to find a string ${prof} in a file dm_smear.dat and if that exists in the file I will do certain operations

: syntax error in conditional expression   
: syntax error near `1`'   
: `         if [[ ! -z grep `echo "${prof}" | cut -d . -f 1` dm_smear.dat ]]'

Upvotes: 1

Views: 113

Answers (2)

Prakash
Prakash

Reputation: 183

You can try following code

prof="KEY1"

if ! [ -z cut -d. -f1 dm_smear.dat | grep ${prof} ] ; then

     echo "FOUND"

else

     echo "NOT FOUND"

fi

In above code, we are searching KEY1 in dm_smear.dat file at first column

the output will be FOUND if file contains KEY1 in first column of any line, otherwise it prints NOT FOUND

where dm_smear.dat contains

KEY1.VALUE1

KEY2.VALUE2

KEY3.VALUE2

Upvotes: 0

devnull
devnull

Reputation: 123518

I am trying to find a string ${prof} in a file dm_smear.dat and if that exists in the file I will do certain operations

You can simply use the -q option for grep. Say:

if grep -q "${prof}" dm_smear.dat; then
   echo "Found the string"
   # Do something here
fi

You could fix your original code by using process substitution correctly:

if [[ ! -z $(grep $(echo "${prof}" | cut -d . -f 1) dm_smear.dat) ]];
   echo "Found the string"
   # Do something here
fi

Upvotes: 1

Related Questions