capser
capser

Reputation: 2635

bash scp in script will not see file in variable interpolation

I keep on getting this when i run this script (which is listed below)

casper@foo0170pap:/home/data$ ./change_while
20141023
gzip: tr_mmet.20141023.csv.gz already exists; do you wish to overwrite (y or n)? y
: No such file or directory
gzip: tr_mmstk.20141023.csv.gz already exists; do you wish to overwrite (y or n)? y
: No such file or directory

I use the set x and I tried putting quotes around the $transfer_zip I tried putting brackets around it, I tried both. The scp does not see the varaible

casper@foo0170pap:/home/data$ ./change_while
++ /bin/date +%Y%m%d
+ today=20141023
+ echo 20141023
20141023
+ for i in tr_mmet.csv tr_mmstk.csv
++ printf tr_mmet
+ cut_suffix=tr_mmet
+ cp -p tr_mmet.csv tr_mmet.20141023.csv
+ sleep 2
++ gzip tr_mmet.20141023.csv
gzip: tr_mmet.20141023.csv.gz already exists; do you wish to overwrite (y or n)? y
+ transfer_zip=
+ scp -r -p '' tr_writer@tr_report.casper_bank.com:/data/Dropbox
: No such file or directory
+ sleep 2

Here is the script - everything works but the scp part.

#!/bin/bash
set -x
today=$(/bin/date +%Y%m%d)
echo $today
for i in tr_mmet.csv  tr_mmstk.csv
do
cut_suffix=$(printf ${i%%.*})
cp -p $i $cut_suffix.$today.csv
sleep 2
transfer_zip=$(gzip $cut_suffix.$today.csv)
scp -r -p "${transfer_zip}" tr_writer@tr_report.casper_bank.com:/data/Dropbox
sleep 2
done

Upvotes: 0

Views: 204

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81012

This line is your mistake transfer_zip=$(gzip $cut_suffix.$today.csv).

$(...) captures command output but gzip does not, under normal circumstances, create any output.

So you are capturing empty command output into your $transfer_zip variable and the shell is happily expanding an empty string into your scp line.

You need to stick the resulting file name into $transfer_zip. The easiest way is just to use transfer_zip=$cut_suffix.$today.csv.gz since you know how gzip operates.

If you don't want to make that filename assumption you can use gzip -c to get it to write to standard output and then manually redirect that to a file with > your_file_name and then use your_file_name in the scp command.

Upvotes: 1

Related Questions