output redirection using exec command

Hi team,

below is the program i have written to redirect the output to file using my own file descriptor. Iam able to successfully redirect the output to a file .
But the problem iamm facing is that , after closing my file descriptor still my programs writes the output to the file

How to solve this problem , any help is really appreciated


#script5
#!/bin/bash
#script to create our own file descriptor

exec 4>fileout
#redirect standard output to file descriptor 4

exec 1>&4
echo "This is a test program"
exec 4&-

echo " Back to normal"

# i want the output back to normal to be displayed on the screen , but it is getting stored in the file

Any help is really appreciated.
sam_2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You'll end up with far more robust code by simply doing an echo to a specific file number, rather than trying to merge/unmerge various file numbers...

Using exec is far more complex than docs suggest. Try this instead...

echo "This is a test program" >&4

Open in new window

nociSoftware EngineerCommented:
exec 4>fileout
exec 1>&4
echo "This is a test program"


This assign the FD=4 to fileout
then it dup()'s  FD=4 to FD=1 (closing original FD=1)
exec 4&-

Will dup() FD=4 to another FD>10.    (oh btw.   there needs to be a > or < with &-...)
There is nothing that will bring the original fd=1 back.


echo >fileout "This should be in Fileout"
echo "and this not"

should work a lot better...
Otherwise you need a subshell. / different shell

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam_2012Author Commented:
getting an error invalid  file descriptor as i have closed it .I cannot use it .
sam_2012Author Commented:
Thanks noci and david , below is the code iam implemented
now it is working

script5
#!/bin/bash
#script to create our own file descriptor

exec 4>fileout
exec 5>&1
#redirect standard output to file descriptor 4

exec 1>&4
echo "This is a test program"
exec 4>&-
exec 1>&5
echo " Back to normal"
exec 5>&-
nociSoftware EngineerCommented:
looks a lot better.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.