Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12: More Structured Commands > Processing the Output of a Loop

Processing the Output of a Loop

Finally, you can either pipe or redirect the output of a loop within your shell script. You do this by adding the processing command to the end of the done command:

 for file in /home/rich*

  do

    if [ -d “$file” ]

    then

       echo “$file is a directory”

    elif

       echo “$file is a file”

    fi

 done > output.txt

Instead of displaying the results on the monitor, the shell redirects the results of the for command to the file output.txt.

Consider the following example of redirecting the output of a for command to a file:

 $ cat test23

 #!/bin/bash

 # redirecting the for output to a file

 

 for (( a = 1; a < 10; a++ ))

 do

    echo “The number is $a”

 done > test23.txt

 echo “The command is finished.”


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint