- •Advanced Bash-Scripting Guide
- •Dedication
- •Table of Contents
- •Part 1. Introduction
- •Advanced Bash-Scripting Guide
- •Chapter 2. Starting Off With a Sha-Bang
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Security Issues
- •34.8. Portability Issues
- •34.9. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.4. Credits
- •List of Tables
- •List of Examples
- •Bibliography
Terminal Control Commands
|
Advanced Bash-Scripting Guide: |
|
Prev |
Chapter 12. External Filters, Programs and Commands |
Next |
12.7. Terminal Control Commands
Command affecting the console or terminal
tput
Initialize terminal and/or fetch information about it from terminfo data. Various options permit certain terminal operations. tput clear is the equivalent of clear, below. tput reset is the equivalent of reset, below.
bash$ tput longname
xterm terminal emulator (XFree86 4.0 Window System)
Note that stty offers a more powerful command set for controlling a terminal.
reset
Reset terminal parameters and clear text screen. As with clear, the cursor and prompt reappear in the upper lefthand corner of the terminal.
clear
The clear command simply clears the text screen at the console or in an xterm. The prompt and cursor reappear at the upper lefthand corner of the screen or xterm window. This command may be used either at the command line or in a script. See Example 10-24.
script
This utility records (saves to a file) all the user keystrokes at the command line in a console or an xterm window. This, in effect, creates a record of a session.
Prev |
Home |
Next |
http://tldp.org/LDP/abs/html/terminalccmds.html (1 of 2) [7/15/2002 6:35:23 PM]
Terminal Control Commands
Communications Commands |
Up |
Math Commands |
http://tldp.org/LDP/abs/html/terminalccmds.html (2 of 2) [7/15/2002 6:35:23 PM]
/dev
|
Advanced Bash-Scripting Guide: |
|
Prev |
Chapter 28. /dev and /proc |
Next |
28.1. /dev
The /dev directory contains entries for the physical devices that may or may not be present in the hardware. [1] The hard drive partitions containing the mounted filesystem(s) have entries in /dev, as a simple df shows.
bash$ df |
|
|
|
|
|
Filesystem |
1k-blocks |
Used Available Use% |
|
||
Mounted on |
|
|
|
|
|
/dev/hda6 |
495876 |
222748 |
247527 |
48% |
/ |
/dev/hda1 |
50755 |
3887 |
44248 |
9% |
/boot |
/dev/hda8 |
367013 |
13262 |
334803 |
4% |
/home |
/dev/hda5 |
1714416 |
1123624 |
503704 |
70% |
/usr |
|
|
|
|
|
|
Among other things, the /dev directory also contains loopback devices, such as /dev/loop0. A loopback device is a gimmick that allows an ordinary file to be accessed as if it were a block device. [2] This enables mounting an entire filesystem within a single large file. See Example 13-6 and Example 13-5.
A few of the pseudo-devices in /dev have other specialized uses, such as /dev/null,
/dev/zero and /dev/urandom.
Notes
[1]The entries in /dev provide mount points for physical and virtual devices. These entries use very little drive space.
Some devices, such as /dev/null, /dev/zero, and /dev/urandom are virtual. They are not actual physical devices and exist only in software.
http://tldp.org/LDP/abs/html/devref1.html (1 of 2) [7/15/2002 6:35:24 PM]
/dev
[2]A block device reads and/or writes data in chunks, or blocks, in contrast to a character device, which acesses data in character units. Examples of block devices are a hard drive and CD ROM drive. An example of a character device is a keyboard.
Prev |
Home |
Next |
/dev and /proc |
Up |
/proc |
http://tldp.org/LDP/abs/html/devref1.html (2 of 2) [7/15/2002 6:35:24 PM]
Interactive and non-interactive shells and scripts
|
Advanced Bash-Scripting Guide: |
|
Prev |
Chapter 34. Miscellany |
Next |
34.1. Interactive and non-interactive shells and scripts
An interactive shell reads commands from user input on a tty. Among other things, such a shell reads startup files on activation, displays a prompt, and enables job control by default. The user can interact with the shell.
A shell running a script is always a non-interactive shell. All the same, the script can still access its tty. It is even possible to emulate an interactive shell in a script.
#!/bin/bash MY_PROMPT='$ ' while :
do
echo -n "$MY_PROMPT" read line
eval "$line" done
exit 0
#This example script, and much of the above explanation supplied by
#Stephane Chazelas (thanks again).
Let us consider an interactive script to be one that requires input from the user, usually with read statements (see Example 11-2). "Real life" is actually a bit messier than that. For now, assume an interactive script is bound to a tty, a script that a user has invoked from the console or an xterm.
Init and startup scripts are necessarily non-interactive, since they must run without human intervention. Many administrative and system maintenance scripts are likewise non-interactive. Unvarying repetitive tasks cry out for automation by non-interactive scripts.
Non-interactive scripts can run in the background, but interactive ones hang, waiting for input that never comes. Handle that difficulty by having an expect script or embedded here document feed input to an interactive script running as a background job. In the simplest case, redirect a file to supply input to a read statement (read variable <file). These particular workarounds make possible general
http://tldp.org/LDP/abs/html/intandnonint.html (1 of 2) [7/15/2002 6:35:25 PM]
Interactive and non-interactive shells and scripts
purpose scripts that run in either interactive or non-interactive modes.
If a script needs to test whether it is running in an interactive shell, it is simply a matter of finding whether the prompt variable, $PS1 is set. (If the user is being prompted for input, then the script needs to display a prompt.)
if [ -z $PS1 ] # no prompt? then
# non-interactive
...
else
# interactive
...
fi
Alternatively, the script can test for the presence of option "i" in the $- flag.
case $- in
*i*) |
# interactive shell |
;; |
|
*) |
# non-interactive shell |
;; |
|
# (Courtesy of "UNIX F.A.Q.," 1993)
Scripts may be forced to run in interactive mode with the -i option or with a #!/bin/bash - i header. Be aware that this can cause erratic script behavior or show error messages even when no error is present.
Prev |
Home |
Next |
Miscellany |
Up |
Shell Wrappers |
http://tldp.org/LDP/abs/html/intandnonint.html (2 of 2) [7/15/2002 6:35:25 PM]