27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Analyze Example A−11, and reorganize it in a simplified and more logical style. See how many of its<br />

variables can be eliminated and try to optimize the script to speed up its execution time.<br />

Alter the script so that it accepts any ordinary ASCII text file as input for its initial "generation". The script<br />

will read the first $ROW*$COL characters, and set the occurrences of vowels as "living" cells. Hint: be sure to<br />

translate the spaces in the input file to underscore characters.<br />

I.2. Writing Scripts<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

Write a script to carry out each of the following tasks.<br />

Easy<br />

Home Directory Listing<br />

Perform a recursive directory listing on the user's home directory and save the information to a file.<br />

Compress the file, have the script prompt the user to insert a floppy, then press ENTER. Finally, save<br />

the file to the floppy.<br />

Converting for loops to while and until loops<br />

Convert the for loops in Example 10−1 to while loops. Hint: store the data in an array and step<br />

through the array elements.<br />

Having already done the "heavy lifting", now convert the loops in the example to until loops.<br />

Changing the line spacing of a text file<br />

Write a script that reads each line of a target file, then writes the line back to stdout, but with an<br />

extra blank line following. This has the effect of double−spacing the file.<br />

Include all necessary code to check whether the script gets the necessary command line argument (a<br />

filename), and whether the specified file exists.<br />

When the script runs correctly, modify it to triple−space the target file.<br />

Finally, write a script to remove all blank lines from the target file, single−spacing it.<br />

Backwards Listing<br />

Write a script that echoes itself to stdout, but backwards.<br />

Automatically Decompressing Files<br />

Given a list of filenames as input, this script queries each target file (parsing the output of the file<br />

command) for the type of compression used on it. Then the script automatically invokes the<br />

appropriate decompression command (gunzip, bunzip2, unzip, uncompress, or whatever). If a target<br />

file is not compressed, the script emits a warning message, but takes no other action on that particular<br />

file.<br />

Unique System ID<br />

Generate a "unique" 6−digit hexadecimal identifier for your computer. Do not use the flawed hostid<br />

command. Hint: md5sum /etc/passwd, then select the first 6 digits of output.<br />

Backup<br />

Archive as a "tarball" (*.tar.gz file) all the files in your home directory tree<br />

(/home/your−name) that have been modified in the last 24 hours. Hint: use find.<br />

Primes<br />

Print (to stdout) all prime numbers between 60000 and 63000. The output should be nicely formatted<br />

in columns (hint: use printf).<br />

Appendix I. Exercises 434

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

Saved successfully!

Ooh no, something went wrong!