Open main menu

CDOT Wiki β

Changes

Shell Scripting - Part 3

1 byte removed, 08:14, 13 October 2015
no edit summary
We will continue with using shell scripts to create a Software Information Report that manipulates output generated by the '''rpm''' command. The '''sed''' and '''awk''' commands are very useful tools in shell scripting to manipulate text. In this lab, we will be using ''sed'' to allow the user to select certain portions from the rpm command (options -qi).
If you require <u>'''additional practice'''</u> in creating shell scripts using the "sed" utility, run the following command in your '''Matrix''' account:<br>'''/home/murray.saul/scripting-4'''  
<u>'''Bash Shell Scripting Tips'''</u><br><br><ul><li>'''The Here Document'''<br><br>A neat little trick involving a special type of redirection of stdin ( '''&lt;&lt;''' ) that allows input to be redirected to a command from within the command. '''The name relates to where the stdin is contained: not in a file, but "here in the command itself"'''. A character (like '''+''') is used to mark the boundary of stdin. It is important that the ending boundary only contains a line with that matching character (eg '''+'''); otherwise the stdin will continue to be read! This command is a convenience way to display multiple lines on that screen, but this command can be used with any Linux command that accept stdin.<br><br><u>'''Examples''' (try at the shell prompt)</u><br><br>''cat &lt;&lt;+<br>This is a test message<br>This is the second line<br>+''<br><br>''mail -s "test message" youremailaddr &lt;&lt;+<br>This is a test message<br>I hope you like it.''<br>+<br><br>''tr [a-z] [A-Z] &lt;&lt;+<br>i like ops235<br>i love scripting.<br>+''<br><br></li><li>'''Using sed to Manipulate Text'''<br><br>The Linux command '''sed''' stands for <u>'''S'''</u>treaming <u>'''Ed'''</u>itor which is an effective way to manipulate a text file, output sent from a command, or from within a "here document". This command can manipulate matching text on a variety of criteria (such as line number, regular expression match, etc). Commands can then be used for manipulation such as omitting, printing, substituting, adding, inserting, etc. The sed option -n suppresses display of text so the print (p) command can be used; otherwise, the text will be displayed (with edits via the sed command instructions). Results of text manipulation with sed can be stored in a variable using command substitution, or redirected to a file. NEVER redirect the stdout from a sed command to the same input file (or the input file will be destroyed)! <br><br></li><li>'''<u>Examples''' (try at the shell prompt)</u><br><br>''sed 's/&#124;/ /g' &lt;&lt;+<br>I&#124;like&#124;weekends!<br>+''<br><br>''sed 's/$/\n/g' &lt;&lt;+<br>This text<br>should be<br>double-spaced!''<br>+<br><br></li></ul>
13,420
edits