0

In this example, I mention that I have 2 lists.

list1
x;00:26:82:50:00:00;192.168.1.195;COMPUTER1
x;00:26:82:50:11:11;192.168.1.195;COMPUTER2
x;00:26:82:50:22:22;192.168.1.196;COMPUTER3

list2
x;00:26:82:50:11:11;192.168.1.197;COMPUTER3

And when I execute the command (thanks to ccs242):

for field in 2 3 4; do cut -d\; -f${field} list* | sort | uniq -d; done

I get duplicates

00:26:82:50:11:11
192.168.1.195
COMPUTER3

Now, I want to automate this process using a bash script, so if it does not find duplicates, run a certain command, but if the script detects duplicate, aborts and leaves the duplicates in in /var/log/syslog

This is my script:

 pathlist=/etc/doc

 function duplicate(){
     acls=`for field in 2 3 4; do cut -d\; -f${field} $pathlist/list* | sort | uniq -d; done`
     if [ ${acls} == '' ]; then
     echo OK
     #run a command foo
 else
     echo "Duplicate Data. Abort. Check /var/log/syslog"
     echo $acls >> /var/log/syslog
     exit
  fi
 }
 duplicate

The problem is here:

  [ ${acls} == '' ]

How can i fix it?

2 Answers 2

1

If there is more than one duplicate, then $acls will contain a space for each new-line and the command is then syntactically incorrect. What you need is:-

[ "${acls}" == "" ]

This forces the comparison to be between two strings. As in your question, you could use '' instead of "", but I prefer to use the same type of quotes on both sides of a comparison.

0
0

Try it this way:

if [ -z ${acls} ]
2
  • Line 5: [: too many arguments
    – acgbox
    Apr 14, 2017 at 21:17
  • Please edit your answer to explain a bit more about how this command works and what it should accomplish. Apr 14, 2017 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .