0

I want to create the tar file on another server. When I execute this command it works, but when I put this command in my shell script it returns an error.

tar cvzf - prod_test.txt| ssh [email protected] "cat > prod_test5.tar.gz"

tar: -: Cannot stat: No such file or directory tar: prod_test.txt|: Cannot stat: No such file or directory tar: ssh: Cannot stat: No such file or directory tar: [email protected]: Cannot stat: No such file or directory tar: "cat: Cannot stat: No such file or directory tar: >: Cannot stat: No such file or directory tar: prod_test4.tar.gz": Cannot stat: No such file or directory v ÷Om7 7Þ'(tar:

#!/bin/sh
dy=`date +%a`
bk='/d01/backup'

Transfer Command

trans_oracle='tar cvzf -  prod_test.txt| ssh [email protected] "cat >  prod_test4.tar.gz"'

Delete Command

del_oracle='rm -f '$bk'/'$dy'_apps_indus_prod.tar.gz'
echo '****** Start Time ******' >> bk'/'$dy'_transfer_apps_bk_net.log'
date >> $bk'/'$dy'_transfer_apps_bk_net.log'
printf '\n' >> $bk'/'$dy'_transfer_apps_bk_net.log'

--#Transfer Backup
$trans_oracle
--#Delete Backup
#$del_oracle

echo '******  End Time  ******' >> $bk'/'$dy'_transfer_apps_bk_net.log'
date >> $bk'/'$dy'_transfer_apps_bk_net.log'

printf '\n' >> $bk'/'$dy'_transfer_apps_backuplog.log'
printf '\n' >> $bk'/'$dy'_transfer_apps_backuplog.log'

error

tar: -: Cannot stat: No such file or directory tar: prod_test.txt|: Cannot stat: No such file or directory tar: ssh: Cannot stat: No such file or directory tar: [email protected]: Cannot stat: No such file or directory tar: "cat: Cannot stat: No such file or directory tar: >: Cannot stat: No such file or directory tar: prod_test4.tar.gz": Cannot stat: No such file or directory v ÷Om7 7Þ'(tar: Error exit delayed from previous errors –

The code:

#!/bin/sh
dy=`date +%a`
bk='/d01/backup'

#Transfer Command 
trans_oracle='tar cvzf -  prod_test.txt| ssh [email protected] "cat >  prod_test4.tar.gz"'
#Delete Command
del_oracle='rm -f '$bk'/'$dy'_apps_indus_prod.tar.gz'


echo '****** Start Time ******' >> $bk'/'$dy'_transfer_apps_bk_net.log'
date >> $bk'/'$dy'_transfer_apps_bk_net.log'
printf '\n' >> $bk'/'$dy'_transfer_apps_bk_net.log'

#Transfer Backup
$trans_oracle
#Delete Backup
#$del_oracle

echo '******  End Time  ******' >> $bk'/'$dy'_transfer_apps_bk_net.log'
date >> $bk'/'$dy'_transfer_apps_bk_net.log'

printf '\n' >> $bk'/'$dy'_transfer_apps_backuplog.log'
printf '\n' >> $bk'/'$dy'_transfer_apps_backuplog.log'
10
  • Welcome to unix.se! Please post the error message. When you do not want to locally create a file with tar, you can drop the f option and -: tar cvz prod_test.txt| ssh [email protected] "cat > prod_test5.tar.gz". Both versions worked for me. Feb 15, 2017 at 6:46
  • tar: -: Cannot stat: No such file or directory tar: prod_test.txt|: Cannot stat: No such file or directory tar: ssh: Cannot stat: No such file or directory tar: [email protected]: Cannot stat: No such file or directory tar: "cat: Cannot stat: No such file or directory tar: >: Cannot stat: No such file or directory tar: prod_test4.tar.gz": Cannot stat: No such file or directory v ÷Om7 7Þ'(tar: Error exit delayed from previous errors
    – Farrukh
    Feb 15, 2017 at 6:56
  • 2
    Then please post the code from your script. Do you use bash? Did you try a space between prod_test.txt and pipe? Feb 15, 2017 at 6:59
  • 2
    @Farrukh We can't make much out of your code if you don't put it in the text of the question and format it properly. Please press the edit link above to edit the question.
    – Kusalananda
    Feb 15, 2017 at 7:20
  • 1
    @daniel not every version of tar defaults to stdout. It's better to be explicit and keep the -f - unless you can be absolutely sure if the default setting. Feb 15, 2017 at 9:01

1 Answer 1

1

To show the issue you are getting a small testcase will suffice:

Individually this runs fine:

ls; pwd

then we do this

cmd='ls ; pwd'

Now $cmd will give the error the files named ';' and 'pwd' are missing.

This is where precisely the "eval" command comes in when you want to give the shell another go:

eval "$cmd"

runs successfully as if you had given it at the command line.

So instead of $trans_oracle give it as: eval "$trans_oracle"

4
  • I have done this, actually the problem now is that it is showing errors tar: -: Cannot stat: No such file or directory prod_test.txt tar: Error exit delayed from previous errors
    – Farrukh
    Feb 16, 2017 at 5:39
  • but even then it is creating the tar file on my target server
    – Farrukh
    Feb 16, 2017 at 5:39
  • This error indicates you are not using the -f option in tar. Give: tar cvzf - prod_test.txt | .... Feb 16, 2017 at 6:23
  • 1
    finally this commnad works tar -cz prod_test.txt | ssh [email protected] "cat > /d01/prod_bk_net/PROD_TEST.tar.gz"' but now i want to replace prod_text.txt with a directory
    – Farrukh
    Feb 16, 2017 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .