LinuxQuestions.org
Review your favorite Linux distribution.
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 04-05-2010, 07:23 AM   #1
fd76137
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
Newbie bash script question


Hello folks. I'm very new to scripting, so forgive the ignorance.

I'm trying to write a bash script that gets the list of files in a directory and puts them into a variable, then checks each entry and outputs them as follows:


item1 is a FILE
item2 is a DIR
item3 is a DIR

etc etc.

I am able to get the list of files into a variable, but unsure how to get the output I want.

If anyone can give any advise, thanks in advance!
 
Old 04-05-2010, 07:32 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Have a look at man test and whatever bash tutorial you have should also have more on testing
file types.

As an example: http://tldp.org/LDP/abs/html/fto.html
 
Old 04-05-2010, 07:42 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You want the list of files in **one** variable? That does not sound like the way to go...

The standard way to do something like this is with a loop:

Code:
for filename in *; do
     commands
     more commands
done
In this case, it finds every entry in the current directory and assigns it to the variable "filename", one at a time. Each time thru the loop, the commands operate on the current value of "filename"

Try it first like this:
Code:
for filename in *; do
     echo $filename
done
Then add some other commands.....
 
Old 04-05-2010, 08:06 AM   #4
fd76137
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you both. That made a world of difference.
 
  


Reply

Tags
bash


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
Newbie Bash script help Mikecore Programming 3 05-13-2008 11:23 AM
Bash Script newbie Konane Programming 9 11-18-2007 12:46 PM
Newbie bash question about a secondhand script madtinkerer Programming 22 11-27-2006 08:02 AM


All times are GMT -5. The time now is 06:25 PM.

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