Читайте также:
|
|
If the variable noclobber installed, shell does not allow to destroy an existing file, unless explicitly stated otherwise removing it with an exclamation point after the redirection operator for C shell or a vertical line in ksh and bash. Table. 20 shows an example of setting a variable noclobber.
Table 20 - Example of setting a variable noclobber
For C shell and tcsh | For ksh and bash |
% set noclobber % ls filea fileb % program > fi.leb fileb: File exists. % Program>! Fileb % | $ Set-o noclobber $ ls filea fileb $ program> fileb fileb: fileb: Cannot clobber existing file $ program> fileb |
Program tee is writing output data to a file and simultaneously send them to the standard output. You may need to collect output data from several teams and in turn send them to the same file by using the command tee. The obvious solution to this problem - the use of option - a.
$ command 1 | tee file
$ command 2 | tee –a file
$ command 3 | tee –a file
A simpler way to solve is the following;
$ (command- am1
>command 2
Ø command 3) | tee file
Ø
With the help of the operators () joining Imprint three teams. These data are then transmitted to a team - tee. The result of processing is the same, but in the second case used in the two channels and two smaller programs tee and one generated by the interpreter shell more.
Note: noclobber is not an environment variable, so each new instance of the shell will inherit it. Consequently, the regime noclobber always been installed, insert the command contained in the examples set in the configuration file his interpreter shell.
2.4 Device /dev/null
The device /dev/null is not a regular file, although it can be used as such. This is a special device in the system of UNIX (not physical), which "absorbs" any writes the text. When reading the text of the device, it returns end of file (a file of zero length).
This device is needed for:
- purification of another file (copy the file / dev / null on top of another file);
- the hidden work of the program by redirecting its output to this device. For example, you run the program in the background and do not want it to be disturbed: % program > /dev/null.
This standard output is redirected to the program, but in case of error messages about them will come to the terminal;
- to respond to the programs, ask many questions when it is known that in response to each question, simply press [RETURN]. In many cases, you can redirect standard input from the device /dev/null. % program < dev/null
- when the program requires another file, and you do not want to read or write this file. For example, grep will not display the file name, in which it found a match if the command line does not contain at least two names. When using wildcards in the directory where it can be only one appropriate file, and enter / dev / null, so the program grep is always looking through more than one file. % grep «string» * dev/null
2.5 Redirecting stdin using exec
Consider a few files containing examples of the possible use of the system call exec.
Redirecting stdin using 'exec'.
#! /bin/bash
# redirecting stdin using 'exec'.
exec 6 <& 0 # Link file descriptor. # 6 from the standard input
(stdin).
# Saving stdin.
exec <data-file #stdin replaced by file "data - file"
read a1 # Reads the first line of "data-file".
read a2 # Read the second line of "data-file."
echo
echo "The following lines were read from the file."
echo "---------------------------------------------------------"
echo $a1
echo $a2
echo; echo; echo
exec 0<& 6 6<&-
#Rehabilitating stdin from decr. #6, where it was previously saved,
#+ and descr. #6 closing (6<&-) relisting it for another processes.
#
# <&6 6<&- gives the same result.
echo -n “Enter string ”
read b1 #Now function “reed” receives data from ordinary stdin.
echo “String received from stdin.”
echo “-----------------------------------”
echo “b1 = $b1”
echo
exit 0
Similarly, the construction exec> fdename redirects output to stdout in specified file. After, the whole output from teams, which usually sent to stdout, now output in this file.
#! /bin/bash
#reassign-stdout.sh
LOGFILE=logfile.txt
Exec 6>&1 # Bind descr. #6 with stdout.
# Saving stdout.
Exec > $LOGFILE # Stdout is changed by file “logfile.txt”.
#-------------------------------------------------------------------------------------------------------#
# All enter from commands in this block is writing into the file
&LOGFILE.
Echo -n “Logfile: ”
Date
Echo “-----------------------------------------------”
Echo
Echo “Enter command \”ls - al\””
Echo
ls -al
echo; echo
echo “Output of commad \”df\””
echo
df
#--------------------------------------------------------------------------------------------------------#
exec 1>&6 6>&- #Restore stdout and close descr. #6.
echo
echo “== stdout restored in default value == ”
echo
ls -al
echo
exit 0
Дата добавления: 2015-11-14; просмотров: 41 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Standard streams | | | Поняття частин мови. |