12.07.2015 Views

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

Red Hat Enterprise Linux 5 Administration Unleashed

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scheduling Tasks with Cron 249LISTING 11.10Example Sed Commands#double space a text file that is single spacedsed -e G singlespace.txt > doublespace.txt11#replace the word one with the number 1sed -s ‘s/one/1/g’ old.txt > new.txt#replace the word old with the word new but only for the first instance on each➥linesed -s ‘s/old/new/’ old.txt > new.txtFor a Sed reference including a list of regular expressions accepted by Sed, refer to the Sedmanual at http://www.gnu.org/software/sed/manual/sed.html.Writing Scripts with AwkCompared to Sed, Awk is a more complete language with arrays, built-in functions, and theability to print from an Awk program. It, too, can be called directly from the command lineor from a Bash script. Install the gawk package to use Awk (<strong>Red</strong> <strong>Hat</strong> <strong>Enterprise</strong> <strong>Linux</strong> includesthe GNU version of Awk called Gawk). To use Awk from the command line, invoke the awkcommand followed by the code inside curly brackets, inside single quotation marks:awk ‘{}’To use Awk from a Bash script, use the same syntax as if from the command line. Refer tothe “Executing Commands in a Bash Script” section earlier in this chapter for furtherexplanation on including commands in a Bash script.Listing 11.11 shows a simple awk command to parse through the output of the uptimecommand and only display the number of days since the last reboot.LISTING 11.11Awk Program to Parse Output of uptime Command#only print how long the system has been running from the uptime commanduptime | awk ‘{print $3 “ “ $4}’Because the output of uptime is always in the same format, you know that the third andfourth fields in the space-separated list contain the number of days that the system hasbeen running and the word “days.” This data is displayed using the Awk print command.For a complete Gawk reference including a list of built-in functions, refer to the Gawkmanual at http://www.gnu.org/software/gawk/manual/.Scheduling Tasks with CronNow that you understand the basics of automating tasks with a script or program, thenext step is to know how to schedule the tasks so they are executed at a specific time oron a set schedule. Some scripts such as removing users may not need to be scheduled, but

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!