47
#+ since $pass_count still uninitialized.
let "pass_count += 1"
# Assumes the uninitialized variable $pass_count
#+ can be incremented the first time around.
# This works with Bash and pdksh, but
#+ it relies on non-portable (and possibly dangerous) behavior.
# Better would be to initialize $pass_count to 0 before incrementing.
while [ "$pass_count" -le $MAXPASSCNT ]
do
. $0 # Script "sources" itself, rather than calling itself.
# ./$0 (which would be true recursion) doesn't work here. Why?
done
# What occurs here is not actually recursion,
#+ since the script effectively "expands" itself, i.e.,
#+ generates a new section of code
#+ with each pass through the 'while' loop',
# with each 'source' in line 20.
#
# Of course, the script interprets each newly 'sourced' "#!" line
#+ as a comment, and not as the start of a new script.
echo
exit 0 # The net effect is counting from 1 to 100.
# Very impressive.
# Exercise:
# --------
# Write a script that uses this trick to actually do something useful.
exit
Unconditionally terminates a script. [63] The exit command may optionally take an integer argument,
which is returned to the shell as the exit status of the script. It is good practice to end all but the
simplest scripts with an exit 0, indicating a successful run.
If a script terminates with an exit lacking an argument, the exit status of the script is
the exit status of the last command executed in the script, not counting the exit. This is
equivalent to an exit $?.
An exit command may also be used to terminate a subshell.
exec
This shell builtin replaces the current process with a specified command. Normally, when the shell
encounters a command, it forks off a child process to actually execute the command. Using the exec
builtin, the shell does not fork, and the command exec'ed replaces the shell. When used in a script,
therefore, it forces an exit from the script when the exec'ed command terminates. [64]
Example 15-24. Effects of exec
#!/bin/bash
exec echo "Exiting \"$0\" at line $LINENO." # Exit from script here.
# $LINENO is an internal Bash variable set to the line number it's on.
# ----------------------------------
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
207
43
# The following lines never execute.
echo "This echo fails to echo."
exit 99 # This script will not exit here.
# Check exit value after script terminates
#+ with an 'echo $?'.
# It will *not* be 99.
Example 15-25. A script that exec's itself
#!/bin/bash
# self-exec.sh
# Note: Set permissions on this script to 555 or 755,
# then call it with ./self-exec.sh or sh ./self-exec.sh.
echo
echo "This line appears ONCE in the script, yet it keeps echoing."
echo "The PID of this instance of the script is still $$."
# Demonstrates that a subshell is not forked off.
echo "==================== Hit Ctl-C to exit ===================="
sleep 1
exec $0 # Spawns another instance of this same script
#+ that replaces the previous one.
echo "This line will never echo!" # Why not?
exit 99 # Will not exit here!
# Exit code will not be 99!
An exec also serves to reassign file descriptors. For example, exec <zzz-file replaces stdin
with the file zzz-file.
The -exec option to find is not the same as the exec shell builtin.
shopt
This command permits changing shell options on the fly (see Example 25-1 and Example 25-2). It
often appears in the Bash startup files, but also has its uses in scripts. Needs version 2 or later of Bash.
shopt -s cdspell
# Allows minor misspelling of directory names with 'cd'
# Option -s sets, -u unsets.
cd /hpme # Oops! Mistyped '/home'.
pwd # /home
# The shell corrected the misspelling.
caller
Putting a caller command inside a function echoes to stdout information about the caller of that
function.
#!/bin/bash
function1 ()
{
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
208
47
# Inside function1 ().
caller 0 # Tell me about it.
}
function1 # Line 9 of script.
# 9 main test.sh
# ^ Line number that the function was called from.
# ^^^^ Invoked from "main" part of script.
# ^^^^^^^ Name of calling script.
caller 0 # Has no effect because it's not inside a function.
A caller command can also return caller information from a script sourced within another script.
Analogous to a function, this is a "subroutine call."
You may find this command useful in debugging.
Commands
true
A command that returns a successful (zero) exit status, but does nothing else.
bash$ true
bash$ echo $?
0
# Endless loop
while true # alias for ":"
do
operation-1
operation-2
...
operation-n
# Need a way to break out of loop or script will hang.
done
false
A command that returns an unsuccessful exit status, but does nothing else.
bash$ false
bash$ echo $?
1
# Testing "false"
if false
then
echo "false evaluates \"true\""
else
echo "false evaluates \"false\""
fi
# false evaluates "false"
# Looping while "false" (null loop)
while false
do
# The following code will not execute.
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
209
45
operation-1
operation-2
...
operation-n
# Nothing happens!
done
type [cmd]
Similar to the which external command, type cmd identifies "cmd." Unlike which, type is a Bash
builtin. The useful -a option to type identifies keywords and builtins, and also locates system
commands with identical names.
bash$ type '['
[ is a shell builtin
bash$ type -a '['
[ is a shell builtin
[ is /usr/bin/[
bash$ type type
type is a shell builtin
The type command can be useful for testing whether a certain command exists.
hash [cmds]
Records the path name of specified commands -- in the shell hash table[65] -- so the shell or script
will not need to search the $PATH on subsequent calls to those commands. When hash is called with
no arguments, it simply lists the commands that have been hashed. The -r option resets the hash
table.
bind
The bind builtin displays or modifies readline[66] key bindings.
help
Gets a short usage summary of a shell builtin. This is the counterpart to whatis, but for builtins. The
display of help information got a much-needed update in the version 4 release of Bash.
bash$ help exit
exit: exit [n]
Exit the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
15.1. Job Control Commands
Certain of the following job control commands take a job identifier as an argument. See the table at end of the
chapter.
jobs
Lists the jobs running in the background, giving the job number. Not as useful as ps.
It is all too easy to confuse jobs and processes. Certain builtins, such as kill, disown,
and wait accept either a job number or a process number as an argument. The fg, bg
and jobs commands accept only a job number.
bash$ sleep 100 &
[1] 1384
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
210
46
bash $ jobs
[1]+ Running sleep 100 &
"1" is the job number (jobs are maintained by the current shell). "1384" is the PID or
process ID number (processes are maintained by the system). To kill this job/process,
either a kill %1 or a kill 1384 works.
Thanks, S.C.
disown
Remove job(s) from the shell's table of active jobs.
fg, bg
The fg command switches a job running in the background into the foreground. The bg command
restarts a suspended job, and runs it in the background. If no job number is specified, then the fg or bg
command acts upon the currently running job.
wait
Suspend script execution until all jobs running in background have terminated, or until the job number
or process ID specified as an option terminates. Returns the exit status of waited-for command.
You may use the wait command to prevent a script from exiting before a background job finishes
executing (this would create a dreaded orphan process).
Example 15-26. Waiting for a process to finish before proceeding
#!/bin/bash
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=65
E_NOPARAMS=66
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
# "Run along kid, it's past your bedtime."
exit $E_NOTROOT
fi
if [ -z "$1" ]
then
echo "Usage: `basename $0` find-string"
exit $E_NOPARAMS
fi
echo "Updating 'locate' database..."
echo "This may take a while."
updatedb /usr & # Must be run as root.
wait
# Don't run the rest of the script until 'updatedb' finished.
# You want the the database updated before looking up the file name.
locate $1
# Without the 'wait' command, in the worse case scenario,
#+ the script would exit while 'updatedb' was still running,
#+ leaving it as an orphan process.
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
211
44
exit 0
Optionally, wait can take a job identifier as an argument, for example, wait%1 or wait $PPID.
[67] See the job id table.
Within a script, running a command in the background with an ampersand (&) may cause the script to hang until
ENTER is hit. This seems to occur with commands that write to stdout. It can be a major annoyance.
#!/bin/bash
# test.sh
ls -l &
echo "Done."
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh
_
As Walter Brameld IV explains it:
As far as I can tell, such scripts don't actually hang. It just
seems that they do because the background command writes text to
the console after the prompt. The user gets the impression that
the prompt was never displayed. Here's the sequence of events:
1. Script launches background command.
2. Script exits.
3. Shell displays the prompt.
4. Background command continues running and writing text to the
console.
5. Background command finishes.
6. User doesn't see a prompt at the bottom of the output, thinks script
is hanging.
Placing a wait after the background command seems to remedy this.
#!/bin/bash
# test.sh
ls -l &
echo "Done."
wait
bash$ ./test.sh
Done.
[bozo@localhost test-scripts]$ total 1
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh
Redirecting the output of the command to a file or even to /dev/null also takes care of this problem.
suspend
This has a similar effect to Control-Z, but it suspends the shell (the shell's parent process should
resume it at an appropriate time).
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
212
44
logout
Exit a login shell, optionally specifying an exit status.
times
Gives statistics on the system time elapsed when executing commands, in the following form:
0m0.020s 0m0.020s
This capability is of relatively limited value, since it is not common to profile and benchmark shell
scripts.
kill
Forcibly terminate a process by sending it an appropriate terminate signal (see Example 17-6).
Example 15-27. A script that kills itself
#!/bin/bash
# self-destruct.sh
kill $$ # Script kills its own process here.
# Recall that "$$" is the script's PID.
echo "This line will not echo."
# Instead, the shell sends a "Terminated" message to stdout.
exit 0 # Normal exit? No!
# After this script terminates prematurely,
#+ what exit status does it return?
#
# sh self-destruct.sh
# echo $?
# 143
#
# 143 = 128 + 15
# TERM signal
kill -l lists all the signals (as does the file /usr/include/asm/signal.h).
A kill -9 is a sure kill, which will usually terminate a process that stubbornly
refuses to die with a plain kill. Sometimes, a kill -15 works. A zombie process,
that is, a child process that has terminated, but that the parent process has not (yet)
killed, cannot be killed by a logged-on user -- you can't kill something that is already
dead -- but init will generally clean it up sooner or later.
killall
The killall command kills a running process by name, rather than by process ID. If there are multiple
instances of a particular command running, then doing a killall on that command will terminate them
all.
This refers to the killall command in /usr/bin, not the killall script in
/etc/rc.d/init.d.
command
The command directive disables aliases and functions for the command immediately following it.
bash$ command ls
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
213
36
This is one of three shell directives that effect script command processing. The others
are builtin and enable.
builtin
Invoking builtin BUILTIN_COMMAND runs the command BUILTIN_COMMAND as a shell
builtin, temporarily disabling both functions and external system commands with the same name.
enable
This either enables or disables a shell builtin command. As an example, enable -n kill disables
the shell builtin kill, so that when Bash subsequently encounters kill, it invokes the external command
/bin/kill.
The -a option to enable lists all the shell builtins, indicating whether or not they are enabled. The -f
filename option lets enable load a builtin as a shared library (DLL) module from a properly
compiled object file. [68].
autoload
This is a port to Bash of the ksh autoloader. With autoload in place, a function with an autoload
declaration will load from an external file at its first invocation. [69] This saves system resources.
Note that autoload is not a part of the core Bash installation. It needs to be loaded in with enable
-f (see above).
Table 15-1. Job identifiers
Notation Meaning
%N
Job number [N]
%S
Invocation (command-line) of job begins with string S
%?S
Invocation (command-line) of job contains within it string S
%%
"current" job (last job stopped in foreground or started in background)
%+
"current" job (last job stopped in foreground or started in background)
%-
Last job
$!
Last background process
Advanced Bash-Scripting Guide
Chapter 15. Internal Commands and Builtins
214
Documents you may be interested
Documents you may be interested