LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   while loop or for loop? (https://www.linuxquestions.org/questions/programming-9/while-loop-or-for-loop-384041/)

mijohnst 11-17-2005 12:20 PM

while loop or for loop?
 
I'm trying to build a script that will filter a list of files by name and then do something to them if they begin with the certain letters.

For example, I have five files in /tmp: A1.txt, A2.txt, A3.txt, B1.txt, and B2.txt. I want to run one command on any file that startes with "A" and ends with "txt" and run another command on any other file that begins with "B" and ends with "txt."

I'm just not sure which one I should use. Thanks for the help!

shanenin 11-17-2005 12:23 PM

if you need to go thru a list systematically that is what a for loop is used for

sundialsvcs 11-17-2005 12:33 PM

If the output of the ls or find command is used as input, it can provide you with only the filenames (if any exist) that match your criteria, and present them to you in ascending alphabetic order. Your task is then reduced to describing what you need to do to just one file.

Look at some of the examples in "Common Tasks" in info find...

mijohnst 11-17-2005 12:55 PM

Thanks for the advice. :) I'm starting to work on a for loop to finish my problem.

Where is info find at? Is that a site?

Nylex 11-17-2005 01:01 PM

Quote:

Originally posted by mijohnst
Where is info find at? Is that a site?
No, info is a command. Open up a terminal window and type "info find", but without the quotes.

mijohnst 11-17-2005 01:19 PM

Oh.. I'm a retard... Thanks... lol

mijohnst 11-17-2005 03:55 PM

I'm having some problems doing this. I have another question... If I use this:



###############################
if file da*.dat; then
source /home/mijohnst/compiled_matlab_setup && /home/mijohnst/batchdownsamp "da*.dat"
mv -f *des.dat *.dat
tar -czvf $name.tgz *a*.dat --remove-files
done
################################


Will it run my source /home/mijohnst/compiled_matlab_setup && /home/mijohnst/batchdownsamp "da*.dat" command will it open and close it over and over through each file that starts with da*.dat or will it do them all while it's open?

shanenin 11-17-2005 04:29 PM

If I am understanding your first question. you need to use a "for loop" in conjuntion with an "if then" statment. If I was you I would read up on those two constructs.

the for loop will go thru the whole directory, then the if then statment will decide what command to run on the file..

mijohnst 11-17-2005 08:03 PM

Sheesh... I'm stumped here! I've be looking at this all day and my mind hurts. Any someone help me out? This is what I've got... The red indicates where I'm lost.


#######################################
#
#/tmp/A-Files.tgz = A1.txt A2.txt and A3.txt
#/tmp/B-Files.tgz = B1.txt B2.txt and B3.txt
#
#######################################
# Global Verables
LIST=/tmp/tarlist.txt

# Start change
clear
echo "Creating List of Files"
ls /tmp/*.tgz >/tmp/tarlist.txt

echo ""
echo "Reading from $LIST for jobs to be desimated."
sleep 3
echo ""
for name in `cat ${LIST}` ; do
echo "Renaming $name..."
tar -xzvf $name
rm -f $name
if (files that start with A); then
run this command
fi
if (files that start with B) then
run this command
fi

done
echo "Repacking A Files..."
tar -czvf /tmp/A-Files-new.tgz ./A*.txt --remove-files
echo "Repacking B Files..."
tar -czvf /tmp/B-Files-new.tgz ./B*.txt --remove-files
echo ""
echo "Done!"
echo ""
exit 0
###################################


shanenin 11-18-2005 12:10 AM

looks like you are making progress :-)

this is the page I reference the following method from
http://www.tldp.org/LDP/abs/html/refcards.html#AEN17078

you can use a string method to test for the first letter.
Code:

shane@mainbox ~ $ D="abcdefr"
shane@mainbox ~ $ echo  ${D:0:1}
a

for your code something like this should work

Code:

if [ ${file:0:1} == A ] ; then
    command
elif  [ ${file:0:1} == B ] ; then
    command
fi

if you put your code inbetween these tags it look nicer
[c0de]
enter code
more code
you got the picture
[/c0de]

when you do it replace the zero with an "o"

mijohnst 11-18-2005 12:10 PM

Thanks a ton shanenin... :) You got me over the hump!

Geez, there is just too much to know! It's been fun though... Thanks all for the help!

shanenin 11-18-2005 12:43 PM

your welcome. I am glad I was able to help :-)

shanenin 11-19-2005 12:19 PM

a cleaner option would be to remove the temp file, it is not needed. You can set the output of ls /tmp/*.tgz to either an simple or an array varibale

using a simple variable this should work. the for command seems to use spaces as a delimeter.
Code:

LIST=`ls /tmp/*.tgz`
# now you can reiterate thru the varaible LIST
for name in  ${LIST} ; do

or if you want to uise array variable, this should work
Code:

LIST=(`ls /tmp/*.tgz`)
for name in ${LIST[*]} ; do

I am not sure if one method is better then the other, they seem to do the same thing. Maybe someone else has an opinion.

eddiebaby1023 11-19-2005 03:15 PM

Quote:

Originally posted by shanenin

Code:

if [ ${file:0:1} == A ] ; then
    command
elif  [ ${file:0:1} == B ] ; then
    command
fi


I'd use a case statement for this:
Code:

case $file in
    A*) some command ;;
    B*) other command ;;
esac

Looks neater to me.;)

mijohnst 11-19-2005 05:45 PM

Thanks for the input shanenin! I've removed my LIST= file and added the 'ls' command instead and it's running like a champ!

Also, thanks for the input eddiebaby1023... I might try your suggestion on another task!


All times are GMT -5. The time now is 06:05 PM.