24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

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

Server: JPackage 1.5 for Fedora Core 2<br />

Server: JPackage 1.5, generic<br />

Server: Fedora Linux 2 - i386 - updates<br />

Finding updated packages<br />

Downloading needed headers<br />

Resolving dependencies<br />

Dependencies resolved<br />

I will do the following:<br />

[install: tuxracer 0.61-26.i386]<br />

Is this ok [y/N]: EnterDownloading Packages<br />

Running test transaction:<br />

Test transaction complete, Success!<br />

tuxracer 100 % done 1/1<br />

Installed: tuxracer 0.61-26.i386<br />

Transaction(s) Complete<br />

8.3. Summary<br />

In this chapter, we learned how to provide user comments and how to prompt for user input. This is usually<br />

done using the echo/read combination. We also discussed how files can be used as input and output using file<br />

descriptors and redirection, and how this can be combined with getting input from the user.<br />

We stressed the importance of providing ample message for the users of our scripts. As always when others<br />

use your scripts, it is better to give too much information than not enough. Here documents is a type of shell<br />

construct that allows creation of lists, holding choices for the users. This construct can also be used to execute<br />

otherwise interactive tasks in the background, without intervention.<br />

8.4. Exercises<br />

These exercises are practical applications of the constructs discussed in this chapter. When writing the scripts,<br />

you may test by using a test directory that does not contain too much data. Write each step, then test that<br />

portion of code, rather than writing everything at once.<br />

1.<br />

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Write a script that asks for the user's age. If it is equal to or higher than 16, print a message saying that<br />

this user is allowed to drink alcohol. If the user's age is below 16, print a message telling the user how<br />

many years he or she has to wait before legally being allowed to drink.<br />

As an extra, calculate how much beer an 18+ user has drunk statistically (100 liters/year) and print<br />

this information for the user.<br />

2. Write a script that takes one file as an argument. Use a here document that presents the user with a<br />

couple of choices for compressing the file. Possible choices could be gzip, bzip2, compress and zip.<br />

3. Write a script called homebackup that automates tar so the person executing the script always uses<br />

the desired options (cvp) and backup destination directory (/var/backups) to make a backup of<br />

his or her home directory. Implement the following features:<br />

♦ Test for the number of arguments. The script should run without arguments. If any arguments<br />

are present, exit after printing a usage message.<br />

♦ Determine whether the backups directory has enough free space to hold the backup.<br />

♦ Ask the user whether a full or an incremental backup is wanted. If the user does not have a<br />

full backup file yet, print a message that a full backup will be taken. In case of an incremental<br />

backup, only do this if the full backup is not older than a week.<br />

Chapter 8. Writing interactive scripts 106

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

Saved successfully!

Ooh no, something went wrong!