12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

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.

{ a=321; }echo "a = $a" # a = 321 (value inside code block)# Thanks, S.C.<strong>Advanced</strong> <strong>Bash</strong>-<strong>Scripting</strong> <strong>Guide</strong>The code block enclosed in braces may have I/O redirected to and from it.Example 3-1. Code blocks and I/O redirection#!/bin/bash# Reading lines in /etc/fstab.File=/etc/fstab{read line1read line2} < $Fileecho "First line in $File is:"echo "$line1"echoecho "Second line in $File is:"echo "$line2"exit 0# Now, how do you parse the separate fields of each line?# Hint: use awk, or . . .# . . . Hans-Joerg Diers suggests using the "set" <strong>Bash</strong> builtin.Example 3-2. Saving the output of a code block to a file#!/bin/bash# rpm-check.sh# Queries an rpm file for description, listing,#+ and whether it can be installed.# Saves output to a file.## This script illustrates using a code block.SUCCESS=0E_NOARGS=65if [ -z "$1" ]thenecho "Usage: `basename $0` rpm-file"exit $E_NOARGSfi{ # Begin code block.echoecho "Archive Description:"rpm -qpi $1 # Query description.echoecho "Archive Listing:"Chapter 3. Special Characters 14

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

Saved successfully!

Ooh no, something went wrong!