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.

cathy ~> ls -ld [[:digit:]]*<br />

drwxrwxr-x 2 cathy cathy 4096 Apr 20 13:45 2/<br />

cathy ~> ls -ld [[:upper:]]*<br />

drwxrwxr-- 3 cathy cathy 4096 Sep 30 2001 Nautilus/<br />

drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/<br />

-rw-rw-r-- 1 cathy cathy 997376 Apr 18 15:39 Schedule.sdc<br />

When the extglob shell option is enabled (using the shopt built-in), several extended pattern matching<br />

operators are recognized. Read more in the <strong>Bash</strong> info pages, section Basic shell features->Shell<br />

Expansions->Filename Expansion->Pattern Matching.<br />

4.4. Summary<br />

Regular expressions are powerful tools for selecting particular lines from files or output. A lot of UNIX<br />

commands use regular expressions: vim, perl, the PostgreSQL database and so on. They can be made<br />

available in any language or application using external libraries, and they even found their way to non-UNIX<br />

systems. For instance, regular expressions are used in the Excell spreadsheet that comes with the MicroSoft<br />

Windows Office suite. In this chapter we got the feel of the grep command, which is indispensable in any<br />

UNIX environment.<br />

The grep command can do much more than the few tasks we discussed here; we only used it as an<br />

example for regular expressions. The GNU grep version comes with plenty of documentation, which<br />

you are strongly advised to read!<br />

<strong>Bash</strong> has built-in features for matching patterns and can recognize character classes and ranges.<br />

4.5. Exercises<br />

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

These exercises will help you master regular expressions.<br />

1. Display a list of all the users on your system who log in with the <strong>Bash</strong> shell as a default.<br />

2. From the /etc/group directory, display all lines starting with the string "daemon".<br />

3. Print all the lines from the same file that don't contain the string.<br />

4. Display localhost information from the /etc/hosts file, display the line number(s) matching the<br />

search string and count the number of occurrences of the string.<br />

5. Display a list of /usr/share/doc subdirectories containing information about shells.<br />

6. How many README files do these subdirectories contain? Don't count anything in the form of<br />

"README.a_string".<br />

7. Make a list of files in your home directory that were changed less that 10 hours ago, using grep, but<br />

leave out directories.<br />

8. Put these commands in a shell script that will generate comprehensible output.<br />

9. Can you find an alternative for wc -l, using grep?<br />

10. Using the file system table (/etc/fstab for instance), list local disk devices.<br />

11. Make a script that checks whether a user exists in /etc/passwd. For now, you can specify the user<br />

name in the script, you don't have to work with arguments and conditionals at this stage.<br />

12. Display configuration files in /etc that contain numbers in their names.<br />

Chapter 4. Regular expressions 61

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

Saved successfully!

Ooh no, something went wrong!