LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie bash script question (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-bash-script-question-800111/)

fd76137 04-05-2010 07:23 AM

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!

grail 04-05-2010 07:32 AM

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

pixellany 04-05-2010 07:42 AM

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.....

fd76137 04-05-2010 08:06 AM

Thank you both. That made a world of difference.


All times are GMT -5. The time now is 09:05 PM.