LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-23-2012, 02:55 PM   #1
eamoss
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
Out of ls into array


I am trying to put the output of an ls into a variable which I am able to do with the below command.
Code:
TEST=$(ls)
which will give me
Code:
file1.txt file2.txt file3.txt f1.txt f2.txt a.z b.z c.x
but I only want the files that start with file or f so I changed it to
Code:
TEST=$(ls file* -o f*)
but now I am getting unwanted information like
Code:
drwxr--r--   1 fred  editors   4096  file1 -rw-r--r--   1 fred  editors  30405  file2 -r-xr-xr-x   1 fred  fred      8460  f1
How do I just get the file names and put them into array?
 
Old 06-23-2012, 03:00 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
The -o flag is what's causing the problem, that switches output to a long listing, which is why you're seeing permissions, owner, etc.

Just remove the -o, and also any file that begins with "file" will also begin with "f", so you only need to search on files that begin with "f" in order to grab both.

Code:
TEST=$(ls f*)
Also, it's generally common practice to reserve all caps for environment variables. Local variables in a script should generally be lower case or mixed upper/lower case so as to prevent confusion.
 
Old 06-23-2012, 03:24 PM   #3
eamoss
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I should have used better examples but the two files I want are not the same format lets say file1.txt and doc1.f instead of f1 I want to search for. How do I do this?

Also how would I sort the output based on the number at the end of the file?
 
Old 06-23-2012, 04:28 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
See Why you shouldn't parse the output of ls(1) and while you're there anyway also see http://mywiki.wooledge.org/BashFAQ and http://mywiki.wooledge.org/BashPitfalls.
 
1 members found this post helpful.
Old 06-24-2012, 10:56 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The OP example also sets the values to a single scalar variable. Array syntax is "array=( <wordlist> )"

Notice too that the globbing patterns in the OP example are expanded and passed to ls before it's executed, so you're really just using it as a superfluous "print" command (and which would be subject to wordsplitting if used inside array brackets).

You can simply use the globs directly to set the array instead:

Code:
filearr=( file* doc* )

for f in "${filearr[@]}"; do

	echo "This is file: $f"

done
Finally, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

Last edited by David the H.; 06-24-2012 at 11:16 AM. Reason: major rewording after re-reading the OP
 
  


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
[SOLVED] mdadm: only give one device per ARRAY line: /dev/md/:raid and array laughing_man77 Linux - Hardware 4 03-23-2012 04:05 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:49 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
Open Source Consulting | Domain Registration