简体   繁体   English

脚本中的bash scp在变量插值中将看不到文件

[英]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. 我使用了set x,并尝试在$ transfer_zip周围加上引号,并尝试将方括号括在其中,并且都尝试了。 The scp does not see the varaible SCP看不到变量

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. 这是脚本-一切正常,除了scp部分。

#!/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

This line is your mistake transfer_zip=$(gzip $cut_suffix.$today.csv) . 这行是您的错误transfer_zip=$(gzip $cut_suffix.$today.csv)

$(...) captures command output but gzip does not, under normal circumstances, create any output. $(...)捕获命令输出,但在正常情况下gzip不会创建任何输出。

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. 因此,您正在将空命令输出捕获到$transfer_zip变量中,并且外壳程序将空字符串愉快地扩展到了scp行中。

You need to stick the resulting file name into $transfer_zip . 您需要将生成的文件粘贴到$transfer_zip The easiest way is just to use transfer_zip=$cut_suffix.$today.csv.gz since you know how gzip operates. 最简单的方法就是使用transfer_zip=$cut_suffix.$today.csv.gz因为您知道gzip工作方式。

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. 如果不想使用该文件名,可以使用gzip -c将其写入标准输出,然后使用> your_file_name手动将其重定向到文件,然后在scp命令中使用your_file_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM