Redirect terminal output to file

My program output error messages because of the chmod 755 in the code. Chmod , changes the permission on the filename foo.txt. Also, the contents that are printed to the terminal screen are redirected to this file .  I do not know why foo.txt has no data after the code is generated. The attachment contains contents that are printed to the terminal screen when the code executes.

#!/bin/sh
set -v
 
export SAS94=/path/Lev1/sashome/sasfoundation/
   
sas94 -initstmt "proc setinit; endsas; "-nonotes -nonews -stdio &> chmod 755 foo.txt && grep -- "---" foo.txt

Open in new window


 

The contents that are printed to terminal  screen when the code executes are as follows:

1                the SAS System 17:01                Tuesday, September 1, 2015
 
site numbe:zykbcds
expiration: 30019999
grace period: 45 days
product expiration dates:

---B colb software        30Nov2015
---S/G                             30OCT2015
---GH/Software            30OCT2015
---KP/software             30Nov2015
print-to-filename-foo.txt
dfn48Asked:
Who is Participating?
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.

Kanti PrasadCommented:
Hi

can you try   http://www.tutorialspoint.com/unix/unix-io-redirections.htm

yourCommands &> someFile.txt

or

script output.txt
try the script output.txt before starting up either the server or the client Then everything that you see on the screen will be written to the file.
1
Gerwin Jansen, EE MVETopic Advisor Commented:
You should post what errors you get and where.
1
Gerwin Jansen, EE MVETopic Advisor Commented:
> foo.txt && chmod
1
skullnobrainsCommented:
your script pipes the sas94 command's output into the chmod command which the chmod command ignores entirely. the file is grepped but not created. the chmod will issue an error if the file does not exist.

try this

sas94 -initstmt "proc setinit; endsas; "-nonotes -nonews -stdio &>foo.txt ; chmod 755 foo.txt && grep -- "---" foo.txt

or this ( if you want to see the output while the program still runs )

sas94 -initstmt "proc setinit; endsas; "-nonotes -nonews -stdio 2>1 | tee foo.txt | grep -- "---" foo.txt ; chmod 755 foo.txt

or this ( if you also want to make sure that your file is already 755 while the script runs )

touch foo.txt
chmod 755 foo.txt
truncate -s 0 foo.txt
sas94 -initstmt "proc setinit; endsas; "-nonotes -nonews -stdio 2>1 | tee -a foo.txt | grep -- "---" foo.txt ;

or this ( if you want some basic error handling as well )

! {
touch foo.txt \
&& chmod 755 foo.txt \
&& truncate -s 0 foo.txt \
&& sas94 -initstmt "proc setinit; endsas; "-nonotes -nonews -stdio 2>1 | tee -a foo.txt | grep -- "---" foo.txt ;
} && echo "something went wrong while blah blahing whatever action" >&2 \
&& exit 1
1

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
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
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.