Hitul Mistry
Hitul Mistry

Reputation: 2275

Ubuntu shell script getting error

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

Upvotes: 0

Views: 502

Answers (3)

choroba
choroba

Reputation: 242383

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

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

Upvotes: 1

bidifx
bidifx

Reputation: 1650

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

Upvotes: 1

pitseeker
pitseeker

Reputation: 2563

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

Upvotes: 1

Related Questions