Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Advanced Bash Shell Scripting Gude.pdf
Скачиваний:
57
Добавлен:
17.08.2013
Размер:
4.82 Mб
Скачать

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]