?>

PROBLEM

You want a temporary redirection for some commands in a script

SOLUTION 1

EXEMPLE 1

You group the commands in a sub shell

			(
				command
				.
				.
			) </tmp/infile >/tmp/outfile
		

NOTE

To group commands, you can use parenthesis "()" or braces "{}". In the later case, you access and can modify the environnment of the running shell.

SOLUTION 2

You modify the file descriptors table of the script process

EXEMPLE 2

			echo -modify stdin-----------
			exec 3<&0 </etc/passwd
			grep root
			echo -restore it-------------
			exec 0<&3
			echo -modify stdout----------
			exec 4<&1 >/tmp/outfile
			grep daemon /etc/passwd
			# restore it
			exec 1<&4
			echo -show the result--------
			# 
			cat /tmp/outfile
			echo -normal-----------------
			grep jrc /etc/passwd
		

and the result is :

			-modify stdin-----------
			root:x:0:0:root:/root:/bin/bash
			-restore it-------------
			-modify stdout----------
			-show the result--------
			daemon:x:2:2:daemon:/sbin:/bin/sh
			-normal-----------------
			jrc:x:501:501:CHAUVIERE Jean-Raymond:/home/jrc:/bin/bash
		

NOTE

For the same task, the second way is more performant as it doesn't need process management (fork, sleep, exec, exit).