Unix Script - Changing Variable Question

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Unix Script - Changing Variable Question
# 1  
Old 10-25-2010
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:

Code:
B


3. The attempts at a solution (include all code and scripts):
This is my script so far.
Code:
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

Code:
matrix:~> test111 one two

10
0
total 0

10
0
A

0
B

10
0
C

Without Trace:
Code:
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!




4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Toronto, Toronto, Canada, Prof. Michaels
# 2  
Old 10-25-2010
First, look up how to use
Code:
if [ -r file ]

Then process the contents of directory one, against directory two.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getting input and changing variable?

Hi I am new to scripting and have a function in my .sh script file that outputs a html radio button form weather_forecast_config() { echo "" echo "<html><head><title>Welcome</title></head>" echo "<body>" echo "<h2>Weather Forecast - Change City</h2>" echo "<form name="input"... (5 Replies)
Discussion started by: scriptnewbie
5 Replies

2. Shell Programming and Scripting

Changing the variable using awk?

Dear all, I have kind of used both the awk/sed command and found them really useful. But at the necessity I am having right now, I need help. Actually, I would like to do the following in file script.sh PATH535="/eos/uscms/store/user/pooja04//analysis2012/535/mc/summer12/002/tt/" ... (2 Replies)
Discussion started by: emily
2 Replies

3. Shell Programming and Scripting

Question on tweaking the PATH variable to allow the world to run my executable script

All, I am pretty new to Unix and still in the learning curve :) I have a simple requirement for which I did not get an answer yet (Atleast I do not know how to keyword the search for my requirement!!!). I have an executable script my.script1 in a folder /data/misc/scripts/dev, which when... (5 Replies)
Discussion started by: bharath.gct
5 Replies

4. Shell Programming and Scripting

Changing a variable Question

I have a variable: $FILENAME = /XXXX/XXXX/XXXX/file.dat I want to set another variable that will give me this: $FILENAME2=filea.dat So basically i'm chopping up variable $FILENAME. Not sure cut will do this as i'm looking at different directories so the characther length may be... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

5. Shell Programming and Scripting

Changing value of a variable inside a shell script

I have a continous polling happening inside a shell script on AIX. This actually calls a PL/SQL block. Is there a way I can set up a variable or pass an interrupt to end the script gracefully. I cant read from the config once the job starts running. Ideally I should change value of a variable and... (1 Reply)
Discussion started by: kshyju
1 Replies

6. Shell Programming and Scripting

Help with awk script, changing the FS for a single variable

Hi all, im new to awk and would apreciate if you could tell me how to do this, i have a file with several entries like this: 2008-09-09 21:57:45 44 403 CUSTOM_EVENT Upgrade - end1 2008-09-09 21:57:46 45 403 CUSTOM_EVENT Component Check - start... (3 Replies)
Discussion started by: sx3v1l_1n51de
3 Replies

7. UNIX for Dummies Questions & Answers

Unix Script Question

Hi to all in forum and I hope someone will be able to help. It is likely that over the next couple of months I have to get a hands on knowledge of Unix due to incoming work in company and I hope to be able to get some knowledge from the general Unix community. In the meantime I have one... (2 Replies)
Discussion started by: kencheck
2 Replies

8. Shell Programming and Scripting

Newbie Question: passing a variable into java from script?

I'm currently working on my second ever ksh script! So I apologize if this is a stupid question - I've searched the forum and on google and haven't seen anything :confused: I'm running my script with an input at startup that variable determines a couple of other values(int) that I store into... (1 Reply)
Discussion started by: Cailet
1 Replies

9. AIX

changing unix user password using script

Hi sir, i need help in scripting.. i have 30 users like below eda01 eda02 eda03 eda04 ..... ...... eda30 I want to reset all users password start with eda01 until eda30 to default password 1234 how do i do this using script, i dunt want system prompt me for password.. i am... (5 Replies)
Discussion started by: mani_um
5 Replies

10. UNIX for Dummies Questions & Answers

Very simple question about changing PS1 variable at startup!

Hello there ! I am new in this Unix world and just start learning Unix. I have very simple question about changing PS1 variable (Shell Prompt) i have local.profile file in my working directory, i open in vi edit mode and add this line PS1="Hello:>" and i save that file. I disconnected from... (2 Replies)
Discussion started by: abidmalik
2 Replies
Login or Register to Ask a Question