LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-05-2005, 01:49 AM   #1
thanhVic
LQ Newbie
 
Registered: Jan 2005
Location: Canada
Distribution: Fedora3
Posts: 26

Rep: Reputation: 15
a bash question


I have a question like this:

When I use "ls *.jpg", it's gonna print out every files jpg in my folder. the question is : How can I actually use each file as a variable ?

For example,

$ ls *jpg > new
$ cat new
face.jpg
owl-face.jpg
man-face.jpg
$

So I have 3 jpg files in "new".

I want to make :
face1 = "face.jpg"
face2 = "owl-face.jpg"
face3 = "man-face.jpg"
to use:
echo $face1, $face2, $face3


Thanks a lot

Last edited by thanhVic; 02-05-2005 at 02:40 AM.
 
Old 02-05-2005, 03:05 AM   #2
beaucoup
Member
 
Registered: Jun 2003
Location: Florence, SC
Distribution: Gentoo i386
Posts: 180

Rep: Reputation: 30
I'm not sure I understand exactly what you're asking, but if you want to run a command for every item in a glob, you can use the following format (a trick I probably use 10 times a day)

Code:
for FILE in *.jpg; do echo ${FILE}; done
You can put any number of commands between "do" and "done". Just terminate each with a semicolon.

It's also often useful to use a command substition in the above format.

Code:
for FILE in `cat new.txt`; do echo ${FILE]; done
Or, if your files are scattered about, you can have the find command run commands on files.

Code:
find . -iname *.jpg -exec echo \{\} \;
Note that \{\} gets replaced with the filename, and the \; is manditory
(This particular example would do the exact same thing that that find normally does, though)
 
Old 02-05-2005, 04:39 AM   #3
thanhVic
LQ Newbie
 
Registered: Jan 2005
Location: Canada
Distribution: Fedora3
Posts: 26

Original Poster
Rep: Reputation: 15
exactly what I need, thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about BASH operand Linux - Newbie 2 11-27-2005 09:49 PM
BASH question robscott Linux - Software 3 11-23-2005 09:39 AM
Bash question lnxduck Programming 3 09-30-2005 08:50 AM
bash question? shanenin Linux - Software 1 11-26-2004 12:22 PM
little bash question gplacek Linux - General 2 09-01-2004 12:10 PM


All times are GMT -5. The time now is 11:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration