Angus
Angus

Reputation: 12631

I/O redirection using the shell scripting

I was writing a sheel scripting to print the 7th column of the output from an executable

   get_events -r > f
   awk '{print $7}' f > k
   while read h
   do
           fsstat $h
   done <k

I need to directly execute the command fsstat which takes the i/p from the o/p derived with the get_events. How to get the command fsstat executed without involving the storage on files(f and h) as above

Upvotes: 1

Views: 41

Answers (2)

anubhava
anubhava

Reputation: 785731

You can combine this script into one:

while IFS=' ' read -ra arr; do
   fsstat "${arr[6]}"
done < <(get_events -r)
  1. This script is using process substitution here < <(get_events -r) to read from the output of command get_events -r
  2. Each line of your command's output is read into an array array using read -a
  3. Then from the read array we are using ${arr[6]} which is 7th index since it starts with 0.

Upvotes: 2

konsolebox
konsolebox

Reputation: 75568

get_events -r | awk '{print $7}' | while read h; do
    fsstat "$h"
done

Another if you're using bash:

while read h; do
    fsstat "$h"
done < <(get_events -r | awk '{print $7}')

And this one would prevent fsstat from eating up input:

while read -u 4 h; do
    fsstat "$h"
done 4< <(get_events -r | awk '{print $7}')

Update

And here's another portable way in which you don't have to use awk:

get_events -r | while read _ _ _ _ _ _ h _; do
    fsstat "$h"
done

Upvotes: 2

Related Questions