prathapa reddy
prathapa reddy

Reputation: 331

Bash Script error using grep

I have the following script

var1=$(pwd)
echo $var1
if [ -e $var1 ]  
 then
  echo present directory exists
   if [ grep ^d\* | $(ls -al) ] 
    then
      echo these are the directories in $var1
   fi
else 
  echo failed
fi

Running this script gives the following output with errors

/home/user1/Desktop/workspace
present directory exists
ifelse.sh: 6: [: missing ]
ifelse.sh: 6: ifelse.sh: total: not found

Please explain the errors. If possible give a resource where I can learn about about Bash script errors completely. Thanq

Upvotes: 1

Views: 652

Answers (1)

Marc Young
Marc Young

Reputation: 4012

You have a syntax error in your grep.

You must interpolate (execute) the grep and pipe then evaluate the exit code of that command overall:

if [ $(grep ^d\* | $(ls -al)) ] 

Upvotes: 1

Related Questions