LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-02-2013, 03:02 PM   #1
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Rep: Reputation: Disabled
Simple bash array question


Have found myself in the position of needing to write short simple bash script to create an array of directories.
Code:
#! /bin/bash
drives=(/run/media/david/*)
	
for i in "${drives[@]}";
	do 
		echo $i
	done

exit
This script returns
/run/media/david/External_Drive_1
/run/media/david/Misc-Files
on seperate lines as expected.

What I need to do, (and have not been able to figure out) is to have echo $i return the directory names only, on seperate lines.

External_Drive_1
Misc-Files

Done my searching, but when I get far into the land of nested brackets and single vs double quotes I am wandering in the dark without a lantern.

TIA for any suggestions
 
Old 05-02-2013, 03:07 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
'echo "${i//*\//}";' or 'echo "$(basename "$i")";'
 
1 members found this post helpful.
Old 05-02-2013, 03:36 PM   #3
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks... If you have a moment can you explain ${i//*\//} for my further edification? I almost get why it works, but so simple (like the jitterbug) it has plumb evaded me.


Tks again
 
Old 05-02-2013, 05:12 PM   #4
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
The ${i//*\//} first takes the value of i, the first double forward slash says relplace matching strings with another string. The *\/ is the string to match, the * matches any value, the \/ escapes to a literal /, so the string matches anything up to and including the last /. Everything after the last / is the string to replace, in this case it's blank. So the command finds anything that matches */ (that is anything and up to including the final slash) and replaces it with nothing.
 
3 members found this post helpful.
Old 05-02-2013, 05:26 PM   #5
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks for the explanation ... It becomes clear
 
Old 05-02-2013, 07:39 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And one more:
Code:
echo "${i##*/}"
 
Old 05-05-2013, 10:05 AM   #7
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
{ Edit: I think I mis-read the whole thing. Simple parameter substitution is indeed all that's needed to strip off the pathname of a file. I'll leave this up for posterity anyway. }


If you want to print only the directory names, all you need is one quick fix:

Code:
#!/bin/bash
drives=( /run/media/david/*/ )
	
for i in "${drives[@]}"; do 
    echo "$i"
done

exit
(Note that I've cleaned up a few other small things too.)

"*" outputs all entries, but "*/" only expands directories.

Note also though that hidden directories are not expanded by default. You have to add shopt -s dotglob first to get those.

You can replace the above loop with a simple printf, by the way.

Code:
printf '%s\n' "${drives[@]}"
For more advanced filtering you'll have to run tests on the inputs first, or use find. If you wanted only regular files, for example, you'd need this:

Code:
drives=( /run/media/david/* )
	
for i in "${drives[@]}"; do 
    [[ -f $i ]] && echo "$i"
done

Last edited by David the H.; 05-05-2013 at 10:09 AM.
 
Old 05-05-2013, 10:18 AM   #8
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Original Poster
Rep: Reputation: Disabled
thanks again for all the input and suggestions.
 
  


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
[SOLVED] BASH array question lleb Linux - Newbie 3 05-04-2012 03:10 PM
Bash array question edwardcode Programming 14 08-29-2011 06:54 AM
simple BASH while loop question / Array riotxix Programming 10 01-03-2010 08:57 PM
Bash array question krock923 Programming 3 06-22-2006 11:39 AM
Simple array/pointer question. smoothdogg00 Programming 3 03-15-2006 10:15 AM

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

All times are GMT -5. The time now is 04:34 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