read()

含义:Read from a PSL file or process channel(读取PSL文件或进程管道)

Format

read(channel,[size])

Parameters

Parameter

Definition

channel

process I/O channel number from which the read() function reads data

size

optional integer value controlling the amount of data that the read() function reads from channel


Valid Values
->size = 0 read() returns as soon as it has read something from the channel.
->size = –1 read() returns all data available by either reading until the end of file on a channel opened with fopen(), or by blocking until the child process completes for a channel opened with popen().
->size = n, where n is a number greater than zero read() returns n bytes from the output of the channel. As with -1, read() operates in blocking mode.
->size = “n”, where “n” is a literal switch read() performs a non-blocking read of all data available and returns control immediately. If no text is available, the empty string is returned and the errno variable is not set. The nonblocking mode only applies to channels opened by popen()—it is ignored without error for channels opened by fopen().
Note: Do not compare the output to EOF. Use the chan_exists() function to determine if the channel is still open.See Example section.


Default
size = 0

Description

The read() function returns the data it reads one byte at a time from channel. The read() function returns the value EOF (that is, the NULL string) on an end-of-file or error condition.Channels are created by calling the fopen() or popen() function. 

To enforce serialization for shared channels, no two reader processes (that is, read() or readln() functions) can be blocked on the same channel. The second reader process that attempts to block on the shared channel will fail, returning the NULL string and setting the PSL variable errno to E_PSL_BUSY_CHANNEL. Another possible shared channel failure can be caused by a close() function being executed against a channel that also has a blocked reader process. The close() function will cause the reader process to return the NULL string and set errno to E_PSL_UNBLOCKED_BY_CLOSE.

Example

Code:

fchan=fopen("/root/1.txt","r");
if (chan_exists(fchan)) {data=read(fchan,-1);close(fchan);
}
print(data);

Output:

编辑

Code1:

pchan=popen("OS","ls /root");
if (chan_exists(pchan)) {
data=read(pchan);
close(pchan);
}
print(data);

Output1:

BMC PSL function(26)-read()_read()