Operating System - HP-UX
1748256 Members
3807 Online
108760 Solutions
New Discussion юеВ

Re: tar is sheel script problem !!!

 
kpatel786
Frequent Advisor

tar is sheel script problem !!!

I have created a script which copys a file for path1 to path2 and extracts the file in path2 via su command.
I have tried implementing the below commands in script.
1) su - $PROCESS -c "tar -tvf $file" | tee -a 2) $LOG/$file.log
su - $PROCESS <tar -xvf $PATH2/$file|tee -a $LOG/$file.log
exit
eof

The idea is to append the tar output in the log file.
The 2nd command captures the output however it capture the su copy righ information along the tar extract output, which I do not want.
Also it change the current shells stty setting once the script is completed.
Also I donot know how to put check for success/failure on tar extract

Kindly provide your suggestions and inputs.

Thanks in advance.
10 REPLIES 10
OFC_EDM
Respected Contributor

Re: tar is sheel script problem !!!

Trying to get more of an idea of what your command is doing:

What's the content of $PROCESS? I'm assuming a user id.

As well is that the full syntax of your command? There's an unmatched ) in the line you provided.

The basic commands work:
tar -tvf $file | tee -a mylog.log
tar -xvf $file | tee -a mylog.log

Your error code is messed up because the tee statement follows it...so it's always successful.

If you only do tar -tvf somefilethatdoesntexist.tar

Then do echo $? you'll get a return code 255.
Any code > 0 means there was a problem.

Need more detail to be able to help. Specifically content of $PROCESS.

Cheers
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: tar is sheel script problem !!!

su - userid -c "tar -tvf mytar.tar" | tee -a mylog.log

I took the variables out and ran the above command specifying the user id and the logfile name.

The output of tar -tvf mytar.tar was successfully put into the log mylog.log.

Is that what you're trying to do?
The Devil is in the detail.
Dennis Handly
Acclaimed Contributor

Re: tar is sheel script problem !!!

First of all, I suggest you don't use tee(1).
Also, create a script to run as the other user.
That would allow you to put the output exactly where you like. If you are going to put the output in that logfile, why send it to the stdout?
Don't always try to create one-liners.
kpatel786
Frequent Advisor

Re: tar is sheel script problem !!!

The command is in actual as follows:-
su - $PROCESS -c "tar -tvf $file" | tee -a $LOG/$file.log

You are right the $PROCESS is userid.
Strangely I get the list when I use tvf command but when I actually try to extract the output via xvf there is nothing written in the log file.

Thank you both for your prompt reply
OFC_EDM
Respected Contributor

Re: tar is sheel script problem !!!

Dennis is correct.

I only use tee when I want to see the output at the same time it's logging to the file.

Otherwise just redirect via >> to append.
The Devil is in the detail.
Dennis Handly
Acclaimed Contributor

Re: tar is sheel script problem !!!

The command is in actual as follows:-
su - $PROCESS -c "tar -tvf $file" | tee -a $LOG/$file.log

If you move that tee inside the quotes, you would have to write to a file that $PROCESS can't write.
For -tvf, why not read the file as you?

>but when I actually try to extract the output via xvf there is nothing written in the log file.

What is that command? The same except -xvf?
kpatel786
Frequent Advisor

Re: tar is sheel script problem !!!

Yes the command is in actual xvf. I have used tvf just for testing purpose.
kpatel786
Frequent Advisor

Re: tar is sheel script problem !!!

I am attaching the script for better understanding.
Kindly suggest if any modifications are required.

Dennis Handly
Acclaimed Contributor

Re: tar is sheel script problem !!!

>Kindly suggest if any modifications are required.

>chmod 775 $file
>chown $PROCESS:tarot $file
>chown -R $PROCESS:tarot $LOG/$file.log
>chmod 755 $LOG/$file.log

You shouldn't make these files executable. Nor is -R (recursive) needed for chown:
chmod 664 $file
chown $PROCESS:tarot $file
chown $PROCESS:tarot $LOG/$file.log
chmod 644 $LOG/$file.log

>#su - $PROCESS -c "tar -tvf $file" | tee -a $LOG/$file.log

No need to use "su -", remove the "-".

su - $PROCESS <tar -xvf $PATH2/$file|tee -a $LOG/$file.log
exit
eof

Again, no need to use "su -", remove the "-", but you need to cd to the home directory:
su $PROCESS <cd
tar -xvf $PATH2/$file | tee -a $LOG/$file.log
exit
eof