#!/bin/bash while IFS= read -a oL ; do { # reads single/one line echo "${oL}"; # prints that single/one line }; done < <(ps -ewo pid,cmd,etime | grep python | grep -v grep | grep -v sh); unset oL; Note: You can use any simple or complex command/command-set inside the <(...) which may have multiple output lines.
The default output of the ls command shows only the names of the files and directories, which is not very informative. The -l ( lowercase L) option tells ls to print files in a long listing format. When the long listing format is used, you can see the following file information: The file type.
If you use ls -lR you have to then exclude content from the output, somehow, and then grep out what you want. find is probably quicker.
1) Syntax:Syntax of for loop using in and list of values is shown below. This for loop contains a number of variables in the list and will execute for each item in the list. For example, if there are 10 variables in the list, then loop will execute ten times and value will be stored in varname.
Set IFS to newline, like this:
IFS='
'
for x in `ls -l $1`; do echo $x; done
Put a sub-shell around it if you don't want to set IFS permanently:
(IFS='
'
for x in `ls -l $1`; do echo $x; done)
Or use while | read instead:
ls -l $1 | while read x; do echo $x; done
One more option, which runs the while/read at the same shell level:
while read x; do echo $x; done << EOF
$(ls -l $1)
EOF
It depends what you want to do with each line. awk is a useful utility for this type of processing. Example:
ls -l | awk '{print $9, $5}'
.. on my system prints the name and size of each item in the directory.
As already mentioned, awk is the right tool for this. If you don't want to use awk, instead of parsing output of "ls -l" line by line, you could iterate over all files and do an "ls -l" for each individual file like this:
for x in * ; do echo `ls -ld $x` ; done
You can also try the find
command. If you only want files in the current directory:
find . -d 1 -prune -ls
Run a command on each of them?
find . -d 1 -prune -exec echo {} \;
Count lines, but only in files?
find . -d 1 -prune -type f -exec wc -l {} \;
The read(1) utility along with output redirection of the ls(1) command will do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With