LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2009, 04:01 PM   #1
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Assign file name to a variable w/out knowing file name...


I'm writing a shell script and am a little stuck. I know the name of the directory but not the file inside the directory. How can I assign the file name to a variable? I hope that makes sense. If I need to clarify this let me know. Thanks for your help!
 
Old 07-02-2009, 04:09 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
Something like this?

filenames=($(ls))
for $file in ${filenames[@]}; do echo $file; done
 
Old 07-02-2009, 04:16 PM   #3
dots
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Your reply made it click! Gees, I feel like an idiot! This is how I got it to work: FILE=`ls` Thank you very much!
 
Old 07-02-2009, 04:19 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,420

Rep: Reputation: 198Reputation: 198
you don't need ls

Code:
for file in *
do
   echo $file
done

F="*" # quote spaces
or:

Code:
set *
echo $1  # first file
echo ${n}  #nth file
the set is space resilient.

Last edited by bigearsbilly; 07-02-2009 at 04:24 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 02:15 PM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 11:19 AM
how to select a particular line from a file and assign it to a variable? pdklinux79 Linux - Newbie 4 06-18-2008 08:21 PM
Knowing if the File is already in use or open. sameergk Programming 1 10-10-2006 05:33 AM
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 01:12 AM


All times are GMT -5. The time now is 05:31 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration