Flylib.com

Books Software

 
 
 

Section 6.15. Command-Line Options


[Page 242 ( continued )]

6.15. Command-Line Options

Bash supports many command-line options. Some of the most useful are shown in Figure 6-51.

Figure 6-51. Some Bash command-line options.

-c string

Run string as a shell command.

-s

Read commands from standard input.

--login

Run Bash as a login shell. This is useful if you can't set Bash to be your login shell with chsh .

--noprofile

Ignore Bash profile files ( systemwide and user versions).

--norc

Ignore Bash rc files (~/.bashrc).

--posix

Run in Posix mode (same as set o posix ).

--verbose -v

Print shell input lines as they are read (same as set o verbose ).




[Page 242 ( continued )]

Chapter Review

Checklist

In this chapter, I described:

  • Bash startup files

  • using shell variables

  • using aliases, the history mechanism, and command-line editing

  • arithmetic, conditionals, and control structures

  • functions

  • using the directory stack

  • job control

Quiz

1.

Describe a common use of the built-in variable $$.

2.

What is the easiest way to re-execute your ".profile" file?


[Page 243]
3.

Why are braces ( { } ) required around list variables?

4.

Why is the alias mechanism useful?

5.

What characters surround an arithmetic expression in a Bash script?

6.

How are functions made available to subshells?

7.

How do you change your command-editing mode from emacs to vim ?

8.

Name one method used to define a shell variable in a subshell (i.e., pass the value to the subshell).

9.

What shell variable contains the directory stack?

Exercises

1.

Rewrite the loan Perl program from Chapter 4, "GNU Utilities for Power Users" (beginning on page 162) in Bash. [level: medium ]

2.

Write a utility called junk that satisfies the following specification:

Utility : junk [-l] [-p] { fileName }*

junk is a replacement for the rm utility. Rather than removing files, it moves them into the subdirectory ".junk" in your home directory. If ".junk" doesn't exist, it is automatically created. The -l option lists the current contents of the ".junk" directory, and the -p option purges ".junk".


Here's an example of junk at work:

$

ls -lG reader.c

...list existing file.
-rw-r--r--    1 glass    2580 May    4 19:17 reader.c
$

junk reader.c

...junk it!
$

ls -lG reader.c

...confirm that it was moved.
reader.c not found
$

junk badguy.c

...junk another file.
$

junk -l

...list contents of "junk" directory.
-rw-r--r--    1 glass      57 May    4 19:17 badguy.c
-rw-r--r--    1 glass    2580 May    4 19:17 reader.c
$

junk -p

...purge junk.
$

junk -l

...list junk.
$ _

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

Remember to comment your script liberally. [level: medium ]

Projects

1.

Create a process management utility that allows you to kill processes based on their CPU usage. This kind of utility would be especially useful to system administrators (see Chapter 14, "System Administration"). [level: easy ]


[Page 244]
2.

Write a Bash script called mv (which replaces the GNU utility mv ) that tries to rename the specified file (using the GNU utility mv ), but if the destination file exists, instead creates an index number to append to the destination file, a sort of version number. So if I type:

$ mv a.txt b.txt


but b.txt already exists, mv will move the file to b.txt.1. Note that if b.txt.1 already exists, you must rename the file to b.txt.2, and so on, until you can successfully rename the file to a name that does not already exist. [level: medium ]

3.

Write a crafty script called ghoul that is difficult to kill; when it receives a SIGINT (from a Control-C ), it should create a copy of itself before dying. Thus, every time an unwary user tries to kill a ghoul, another ghoul is created to take its place! Of course, ghoul can still be killed by a SIGKILL (-9) signal. [level: hard ]