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.

Lottery Numbers<br />

One type of lottery involves picking five different numbers, in the range of 1 − 50. Write a script that<br />

generates five pseudorandom numbers in this range, with no duplicates. The script will give the<br />

option of echoing the numbers to stdout or saving them to a file, along with the date and time the<br />

particular number set was generated.<br />

Intermediate<br />

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

Managing Disk Space<br />

List, one at a time, all files larger than 100K in the /home/username directory tree. Give the user<br />

the option to delete or compress the file, then proceed to show the next one. Write to a logfile the<br />

names of all deleted files and the deletion times.<br />

Safe Delete<br />

Write, as a script, a "safe" delete command, srm.sh. Filenames passed as command−line arguments<br />

to this script are not deleted, but instead gzipped if not already compressed (use file to check), then<br />

moved to a /home/username/trash directory. At invocation, the script checks the "trash"<br />

directory for files older than 48 hours and deletes them.<br />

Making Change<br />

What is the most efficient way to make change for $1.68, using only coins in common circulations<br />

(up to 25c)? It's 6 quarters, 1 dime, a nickel, and three cents.<br />

Given any arbitrary command line input in dollars and cents ($*.??), calculate the change, using the<br />

minimum number of coins. If your home country is not the United States, you may use your local<br />

currency units instead. The script will need to parse the command line input, then change it to<br />

multiples of the smallest monetary unit (cents or whatever). Hint: look at Example 23−4.<br />

Quadratic Equations<br />

Solve a "quadratic" equation of the form Ax^2 + Bx + C = 0. Have a script take as arguments the<br />

coefficients, A, B, and C, and return the solutions to four decimal places.<br />

Hint: pipe the coefficients to bc, using the well−known formula, x = ( −B +/− sqrt( B^2 − 4AC ) ) /<br />

2A.<br />

Sum of Matching Numbers<br />

Find the sum of all five−digit numbers (in the range 10000 − 99999) containing exactly two out of the<br />

following set of digits: { 4, 5, 6 }. These may repeat within the same number, and if so, they count<br />

once for each occurrence.<br />

Some examples of matching numbers are 42057, 74638, and 89515.<br />

Lucky Numbers<br />

A "lucky number" is one whose individual digits add up to 7, in successive additions. For example,<br />

62431 is a "lucky number" (6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the "lucky numbers" between<br />

1000 and 10000.<br />

Alphabetizing a String<br />

Alphabetize (in ASCII order) an arbitrary string read from the command line.<br />

Parsing<br />

Parse /etc/passwd, and output its contents in nice, easy−to−read tabular form.<br />

Pretty−Printing a Data File<br />

Certain database and spreadsheet packages use save−files with comma−separated values (CSVs).<br />

Other applications often need to parse these files.<br />

Given a data file with comma−separated fields, of the form:<br />

Appendix I. Exercises 435

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

Saved successfully!

Ooh no, something went wrong!