LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Going through files in a directory, bash scripting (https://www.linuxquestions.org/questions/linux-newbie-8/going-through-files-in-a-directory-bash-scripting-801434/)

Pewgs 04-11-2010 06:41 PM

Going through files in a directory, bash scripting
 
Hey guys I am brand new to linux and the class I'm taking for it has more of a use the resources online and man pages method, meaning not much teaching has been going on.

Anyway the project I'm working on involves comparing files in a given directory, using bash scripting methods. So my program is given an argument for the directory you want to search and then carry out the instructions. So I know how to pass the argument using $1, but given the directory I'm not quite sure how to loop through all the files.

I'm not new to programming so I things such as if statements and loops buts it's been the linux specific commands that have given me trouble and I couldn't find anything in the man pages.

Sorry for being vague about the project, I want to be able to figure it out on my own but I can't really test anything until I figure this out, if you need more information about what I'm asking feel free to ask.

P.S. Hi Linux community :)

AnanthaP 04-11-2010 06:55 PM

The shell will expand the * argument so effectively you have to enter a loop for each x in * and in the loop do all your commands and then finally when done, exit.

All the best.

End

fbsduser 04-11-2010 07:00 PM

Code:

#!/bin/bash
        for i in $( ls ); do
            echo item: $i
        done

This will cycle through your files/directories and show them. It's a small start for you to work from.

crts 04-11-2010 07:02 PM

Hi,

You can look here for bash scripting guides:
http://tldp.org/guides.html

Pewgs 04-11-2010 07:09 PM

I'm sorry I don't quite understand, when you say * do you mean the variable stored as the directory? And by x do you mean each file in stored in the directory?

So for my example if this was my code:
#!/bin/bash

DIRECTORY=$1
for x in DIRECTORY
do
#comparing lines here
done

Like I said I'm completely new to linux and bash scripting.

EDIT: Nevermind fbsduser's post was very helpful

DOUBLE EDIT:
Ok but I'm at a new problem so because I need to compare two files in created a for loop in another for loop:

#!/bin/bash

DIRECTORY=$1
for i in $( ls $DIRECTORY )
do
for x in $( ls $DIRECTORY )
do
COMPARE=cmp i x
echo $COMPARE
done
done

from my understanding cmp should have an exit value but Im getting this error whenever it runs that line:
./remdup: line 8: i: command not found
I have a feeling that my line COMPARE=cmp i x is not valid. Does anyone know how I would express this?

grail 04-11-2010 07:41 PM

The error is correct:

Quote:

./remdup: line 8: i: command not found
And you are correct about the offending line:

Quote:

COMPARE=cmp i x
If you look at your line carefully you have said the following:

1. COMPARE=cmp -> assign the value of cmp to COMPARE
2. i x -> execute the i command using x as a parameter

Check out : http://tldp.org/LDP/abs/html/command...#COMMANDSUBREF

Make sure you read through the examples, also look further down the page at $() as the alternative to `` (these are called back ticks which
are located under the ~ (tilde) key)

Pewgs 04-11-2010 09:20 PM

Ok so this result I think is currently working:

COMPARE=`cmp $i $x`

In my directory that I'm testing I have two files I'm using for testing called "a" and "one". So when I run the program I'm getting this as a result

cmp: a: No such file or directory
cmp: a: No such file or directory
cmp: One: No such file or directory
cmp: One: No such file or directory

which through debugging I have determined is from ths line
COMPARE=`cmp $i $x`. Also when I echo $COMPARE it's spitting out a blank line which means that nothing is being assigned to it. Any thoughts? It's obvious that a and One are in the directory because the program recognizes to files name a and One, so I'm not sure why I'm getting these messages... Thanks guys you have been a real help so far.

grail 04-11-2010 09:40 PM

Ok, so as this is a learning process the best thing to do is run the command at the command line and confirm the output
is as expected. So step by step would be:

1. perform "cmp a One" (without the quotes) on the command line and check that it does compare.
2. once the above has finished, issue the following: echo $? (this will show you what the return value would have been)
3. if all the above as expected, instead of using for loops, try passing 'a' and 'One' as arguments to the script instead of directory
so code to mimic command line above is: (this goes in the current script with the rest remarked out)
Code:

cmp $1 $2
echo $?

4. from there you should then be able to piece your program back together.

Let me know if you get stuck or if the above is confusing?

Pewgs 04-11-2010 10:02 PM

Quote:

Originally Posted by grail (Post 3932385)
Ok, so as this is a learning process the best thing to do is run the command at the command line and confirm the output
is as expected. So step by step would be:

1. perform "cmp a One" (without the quotes) on the command line and check that it does compare.
2. once the above has finished, issue the following: echo $? (this will show you what the return value would have been)
3. if all the above as expected, instead of using for loops, try passing 'a' and 'One' as arguments to the script instead of directory
so code to mimic command line above is: (this goes in the current script with the rest remarked out)
Code:

cmp $1 $2
echo $?

4. from there you should then be able to piece your program back together.

Let me know if you get stuck or if the above is confusing?

All of this makes sense and when I try those I am getting desirable results, but I still don't understand why I'm getting the no such file error. I was thinking maybe its because I'm in a different directory but then how would the program know the names of the files?

grail 04-11-2010 10:18 PM

You need to look carefully at your code and compare to how you would do it on the command line,
ie from the command line, what would you write to compare (cmp) two files in another directory???

Then look at how your code is written.

crts 04-11-2010 11:34 PM

Hi,

you should avoid using 'ls' in a script. This is problematic with filenames which contain spaces. Consider the following:
Code:

echo Test > 'filename with space'
Now run the following script:
Code:

for f in $( ls )
do
 echo $f
done

The output is:
Code:

filename
with
space

This is how you should normally script it
Code:

for f in "$PATH_TO_DIRECTORY"/*
do
 echo "$f"
done

This way you won't encounter the problem you are facing right now. However, you should try to understand why you get the 'no such file ...' error. grail already gave you the decisive hint.


All times are GMT -5. The time now is 11:19 PM.