0

I am getting errors when running this bash script. I have checked in all manuals, it looks okay to me?

Help appreciated.

  #!/bin/sh
  HOME=/var/www
  sleep 20
  echo 'running' > $HOME/script-1-output.txt
  if  (-f $HOME/script-2-output.txt )
    echo 'script-2 has run' >> $HOME/script-1-output.txt
  else
    echo 'script-2 has not run' >> $HOME/script-1-output.txt
  fi
  if  (-f $HOME/script-3-output.txt)
    echo 'script-3 has run' >> $HOME/script-1-output.txt
  else
    echo 'script-3 has not run' >> $HOME/script-1-output.txt
  fi
1
  • 2
    What errors? Where? EDIT: I believe you need if [ -f $HOME/script-2-output.txt ], same with the other if - note the square brackets, and spaces. Still, for next time, remember to include all the relevant info. Commented Sep 10, 2013 at 23:33

1 Answer 1

2

In bash, if conditions are wrapped between [] not (). There should always be space between condition and square brackets. And you need then in if-then-else-fi syntax.

if [ -f file.txt ]; then
    echo "yes"
else
    echo "no"
fi
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, the [ and ] are special syntax for the test command, so [ -f file.txt ] is equivalent to test -f file.txt. It is more correct to say that in bash, you don't use any braces at all around the condition of an if statement. if test -f file.txt; then ... is equally correct.
It is more correct to say that in bash you use [[ ]] around the conditional expressions. if [[ -f file.txt ]]; then

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.