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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
11-17-2005, 12:20 PM
|
#1
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Rep:
|
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!
|
|
|
|
11-17-2005, 12:23 PM
|
#2
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
if you need to go thru a list systematically that is what a for loop is used for
|
|
|
|
11-17-2005, 12:33 PM
|
#3
|
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,741
|
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...
|
|
|
|
11-17-2005, 12:55 PM
|
#4
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
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?
|
|
|
|
11-17-2005, 01:01 PM
|
#5
|
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep: 
|
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.
|
|
|
|
11-17-2005, 01:19 PM
|
#6
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
Oh.. I'm a retard... Thanks... lol
|
|
|
|
11-17-2005, 03:55 PM
|
#7
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
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.
|
|
|
|
11-17-2005, 04:29 PM
|
#8
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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..
|
|
|
|
11-17-2005, 08:03 PM
|
#9
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
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.
|
|
|
|
11-18-2005, 12:10 AM
|
#10
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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"
|
|
|
|
11-18-2005, 12:10 PM
|
#11
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
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!
|
|
|
|
11-18-2005, 12:43 PM
|
#12
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
your welcome. I am glad I was able to help :-)
|
|
|
|
11-19-2005, 12:19 PM
|
#13
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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.
|
|
|
|
11-19-2005, 03:15 PM
|
#14
|
|
Member
Registered: May 2005
Posts: 378
Rep:
|
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. 
|
|
|
|
11-19-2005, 05:45 PM
|
#15
|
|
Member
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419
Original Poster
Rep:
|
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 04:27 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|