0

I am developing simple shell script which copy all my present directory files to backup directory which will be exist in present working directory. now i'm getting error when i pass more then one condition in if.

#!/bin/bash
filename=nx.pdf
for i in *;
 do
 echo $i;
 if [ $i == backup || $i == $filename ] ; then
    echo "Found backup."
 else
 echo "Part 2"
 cp -rf $i backup
 fi
 done

I am getting error

asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2
deployee.sh
asd.sh: line 6: [: missing `]'
asd.sh: line 6: ==: command not found
Part 2

3 Answers 3

1

The compare operator is = (as defined in POSIX). But == works on some shells as well. Something like this should work:

if [ $i = backup ] || [ $i = $filename ] ; then
Sign up to request clarification or add additional context in comments.

Comments

1

You should quote $i in "". Otherwise you get syntax errors for filenames with blanks.

1 Comment

If i do { if [ $i == backup ] ; then echo "Found" then i'm not getting errors.
1

To be able to use || and && in conditions, you have to use the double square brackets:

if [[ $i == backup || $i == $filename ]] ; then

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.