LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2005, 12:20 PM   #1
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Rep: Reputation: 31
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!
 
Old 11-17-2005, 12:23 PM   #2
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
if you need to go thru a list systematically that is what a for loop is used for
 
Old 11-17-2005, 12:33 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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...
 
Old 11-17-2005, 12:55 PM   #4
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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?
 
Old 11-17-2005, 01:01 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 11-17-2005, 01:19 PM   #6
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Oh.. I'm a retard... Thanks... lol
 
Old 11-17-2005, 03:55 PM   #7
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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?

Last edited by mijohnst; 11-17-2005 at 04:00 PM.
 
Old 11-17-2005, 04:29 PM   #8
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
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..
 
Old 11-17-2005, 08:03 PM   #9
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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
###################################


Last edited by mijohnst; 11-17-2005 at 08:27 PM.
 
Old 11-18-2005, 12:10 AM   #10
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
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"
 
Old 11-18-2005, 12:10 PM   #11
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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!
 
Old 11-18-2005, 12:43 PM   #12
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
your welcome. I am glad I was able to help :-)
 
Old 11-19-2005, 12:19 PM   #13
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
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.
 
Old 11-19-2005, 03:15 PM   #14
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 11-19-2005, 05:45 PM   #15
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No loop# SeaSharp Linux - Newbie 3 09-06-2005 08:14 PM
WHILE LOOP in 'C' ']['HeBroken Programming 4 10-29-2004 01:42 AM
while-loop Thomas23 Programming 4 05-24-2004 03:35 PM
for loop klfreese Programming 16 08-11-2003 11:09 AM
help with the following C loop ..... purpleburple Programming 12 08-06-2002 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration