Unix Script - Changing Variable Question

This is a problem with basic Unix scripting. Thanks for looking!

  1. The problem statement, all variables and given/known data:
    Make a script that will compare 2 given directories and output those filenames that are in Directory 1 and not 2

  2. Relevant commands, code, scripts, algorithms:
    Sed & Awk can't be used. The main problem with my code below is that the variable x, after being assigned the value 1 seemingly changes back 0 after leaving the if statement.

Directory 1 (d1) has the three files
A, B, C
Directory 2 (d2) has the three files
A, C, E

Therefore the following should be the output:

B
  1. The attempts at a solution (include all code and scripts):
    This is my script so far.
x=0

ls $1 | while read input
do
        x=0
                ls $2 | while read input1
                do
                        if [ "$input" = "$input1" ]
                        then
                                #echo "both have $input"
                                x=10
                                echo ""   #TRACE
                                echo "$x" #TRACE
                                # If File found in both d1 and d2, x value is changed to one
                        fi
                done

        echo "$x" #TRACE

                if  [ "$x" != "10" ]
                then
                        echo "$input"
                fi

done

With the trace included above, I get

matrix:~> test111 one two

10
0
total 0

10
0
A

0
B

10
0
C

Without Trace:

total 0
A
B
C

Meaning after the x is changed to 10 when a match is found, its later turned back to 0 for a reason I cant explain

If anyone could help be out I'd really appreciate it.

Thanks in advance!

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Toronto, Toronto, Canada, Prof. Michaels

First, look up how to use

if [ -r file ]

Then process the contents of directory one, against directory two.