Opening a command pipeline

As mentioned previously, the open command can be used to open a command pipeline. A command pipeline is a mechanism that allows us to read from or write to a command. The syntax is similar to the standard open command. However, if the first character passed as an argument to the open command is a pipe character (|) the remaining characters are treated as a list of arguments describing a command pipeline that is to be invoked. The syntax is as follows:

	open | command access_permissions

The arguments provided by command are similar to those used for the exec command. The open command will return a file pointer that may be used to write to the specified command's input pipe or read from its output pipe. The specific functionality (read or write) is determined by the access permissions.

If the open command or one of the commands provided as arguments should return an error, a Tcl error will be generated when the close command is invoked on the channel unless the pipeline has been configured for non-blocking. If the channel is configured for non-blocking, no exit status will be returned.

How to do it…

Enter the following command:


%set fp [open "|cmd.exe /c dir text.txt" r]
fileabb5b0

%set data [read $fp]
	Volume in drive C has no label.
	Volume Serial Number is A02F-AD99

	Directoy of C:Documents and SettingsBert
12/09/2010 01:01 PM 			13 text.txt
		1 File(s) 		13 bytes
		0 Dir(s) 31,133,941,760 bytes free

%If {[catch {close $fp} err]} {
	Puts "Error: $err"
}
%

How it works…

As you can see, the open command has returned a file pointer named fileabb5b0 to our command. Also, note that because the command was invoked on a Windows platform additional command syntax was required to access the dir command. This is due to the fact that Windows built-in commands are not implemented by using the executables themselves. This is not required on a Unix- or a Linux-based system.

The return of the command was accessible as we had set the file privileges to r (open the file for "read-only" and the file must exist) and the return was read into our data variable and displayed on the console. The returns are specific to your platform and will vary based on the platform and path variables in place.

We next used a catch statement to trap any errors that might have resulted. These would be returned by the close command. As we successfully executed our dir command, there were no error codes returned and the catch statement simply returned us to our console prompt without invoking the puts command.

There's more…

Now enter the following command line:


%set fp [open "|cmd.exe /c dir no_such_file" r]
fileaca748

%set data [read $fp]
	Volume in drive C has no label.
	Volume Serial Number is A02F-AD99
	
	Directoy of C:Documents and SettingsBert

%If {[catch {close $fp} err]} {
		Puts "Error: $err"
}
ERROR: File Not Found
%

In this instance, we have performed the same command with the exception that we have referenced a non-existing file. As you can see, the catch command has trapped the error return and the puts command was used to display it on the console. Please note that the basic return from the command was displayed when we set the variable data with the read command, but this is not always indicative of a successful return code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.186.178