1

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

1 Answer 1

1

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)) ] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.