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 :) |
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 |
Code:
#!/bin/bash |
|
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? |
The error is correct:
Quote:
Quote:
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) |
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. |
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 Let me know if you get stuck or if the above is confusing? |
Quote:
|
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. |
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' Code:
for f in $( ls ) Code:
filename Code:
for f in "$PATH_TO_DIRECTORY"/* |
All times are GMT -5. The time now is 11:19 PM. |