![]() |
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! |
if you need to go thru a list systematically that is what a for loop is used for
|
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... |
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? |
Quote:
|
Oh.. I'm a retard... Thanks... lol
|
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? |
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.. |
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 ################################### |
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"Code:
if [ ${file:0:1} == A ] ; then [c0de] enter code more code you got the picture [/c0de] when you do it replace the zero with an "o" |
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! |
your welcome. I am glad I was able to help :-)
|
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`Code:
LIST=(`ls /tmp/*.tgz`) |
Quote:
Code:
case $file in |
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. |