LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   YASQ Yet Another Simple Question regarding scripting (https://www.linuxquestions.org/questions/linux-newbie-8/yasq-yet-another-simple-question-regarding-scripting-595884/)

christianunix 10-30-2007 03:47 PM

YASQ Yet Another Simple Question regarding scripting
 
my pwd contains files and directories named nick and emptydr

Code:

ls -l|grep dwrx|cut -d' ' -f9
returns the name of the directories in my pwd.

let's say I save the result of the above command into a file named listing.

then the file listing will contain the following"

nick
emptydr

and now, I would like to write my script so my script uses each line in the file named listing as the input of the file. How can I do that?

thanks

pixellany 10-31-2007 09:16 AM

I think you want the "read" command.

For example, this:
read line1 < filename
reads the first line of "filename" into the variable "line1"

You could do it in a loop in which each invocation of "read" puts a line into an array, indexing the array as you go.

I recommend going to tldp.org and getting the Advanced Bash Scripting Guide. Unless you have a personal forest, don't print it!! I just use it for searches.

nx5000 10-31-2007 10:29 AM

Code:

ls -l|grep dwrx
Better to use this:
Code:

ls -l| grep ^d
^d means lines beginning with d

and then using result (here I simply use echo to print each line):

Code:

cat file  | while read i; do echo $i; done


All times are GMT -5. The time now is 08:17 AM.