-1

This is my bash script to copy a csv file to a new file that has the timestamp added to the end it is then copied to another server.

#!/bin/bash
timestamp=`date +"%Y%m%d%H%M%S"`
ssh root@172.16.1.125 mv /var/log/asterisk/cdr-csv/Master.csv /var/log/asterisk/cdr-csv/Master.csv.$timestamp
scp root@172.16.1.125:/var/log/asterisk/cdr-csv/Master.csv.$timestamp /opt/logs/asterisk/Master.csv.$timestamp

This however adds two spaces to the end of the file. Example:

Orig File: test.csv
MV File: test.csv.20160204083042(space)(space)

Then when the scp tries to locate the file it cant file it due to the spaces at the end. I have given up as I have tried all variations.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jed
  • 929
  • 2
  • 19
  • 32
  • What happens if you add a line containing `echo "\"$timestamp\""` between the lines timestamp is set and ssh executed? –  Feb 04 '16 at 07:03
  • 1
    I don't see any way this could be doing that. Even if `$timestamp` had spaces in it, they would be discarded because you don't have the variable in quotes. – Barmar Feb 04 '16 at 07:04
  • How do you see the spaces in the file name? – Dmitry Alexeyev Feb 04 '16 at 07:24
  • Thanks all it seems it was the encoding the line endings were not set to unix – Jed Feb 04 '16 at 08:14

2 Answers2

0

Check the script at every step. What you describe shouldn't happen:

$ date +"%Y%m%d%H%M%S" | xxd
0000000: 3230 3136 3032 3034 3138 3034 3438 0a    20160204180448

There's a newline in the output, but it goes away when you assign to the variable, so $timestamp will be clean.

Any spaces in the variables will also go away at the line with ssh / scp, because your files are not quoted - even if there was a space at the end, it would be dropped when parsing the command.

To make sure, you can pipe the output, or echo the variables to xxd like in this example. You'll actually see the spaces if they do happen.

Alternatively post the whole script (I'm assuming this is a redacted version)

viraptor
  • 33,322
  • 10
  • 107
  • 191
0

Thanks. All it seems it was the encoding was incorrect the line endings were not set to unix. I develop on a windows machine and deploy to the server which messed up the line endings.

Jed
  • 929
  • 2
  • 19
  • 32