0

I am trying to write a set of bash commands into a shell script file which will then be executed on a server.

It works fine up until the point at which I want to write a diff command into a script file that is then expected to be executed and have the console output written into a *.txt file. For some reason, the *.txt file doesn't include the diff results... anything I can do to fix this?

A_DIR_1=methuselah/test1
A_DIR_2=methuselah/test2
A_DIR_3=methuselah/test3
B_DIR_1=methuselah/backup1
B_DIR_2=methuselah/backup2
B_DIR_3=methuselah/backup3
DST_DIR=methuselah/output

echo "touch $DST_DIR/output.txt" >> /tmp/$$.sh
for i in {1..3}
do
   A_DIR="A_DIR_$i"
   B_DIR="B_DIR_$i"
   echo "diff -qr ${!A_DIR} ${!B_DIR} >> $DST_DIR/output.txt" >> /tmp/$$.sh
done

cat /tmp/$$.sh
ssh meth@$SERVER 'sh -s' < /tmp/$$.sh
rm /tmp/$$.sh
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Which shell are you writing this script on? `(1..3)` is a wrong way for a brace expansion which should throw a syntax error – Inian Dec 01 '17 at 10:55
  • Sorry, fixed that. I am using bash (Bourne Again Shell) – methuselah Dec 01 '17 at 10:58
  • Are you getting any errors? Does the script `/tmp/num.sh` work when executed directly on the server? – PesaThe Dec 01 '17 at 11:15
  • You *are* aware that the `-q` option to diff turns of the reporting of the actual differences? – tripleee Dec 01 '17 at 11:28
  • @tripleee -q, --brief report only when files differ – PesaThe Dec 01 '17 at 11:30
  • Precisely. So *"For some reason, the *.txt file doesn't include the diff results*" could be simply because of that. – tripleee Dec 01 '17 at 11:33
  • Also notice the typo `DIST_DIR` vs `DST_DIR`, though I don't think that actually matters. – tripleee Dec 01 '17 at 11:35
  • @tripleee Sorry, I can't read, didn't see OP wants the actual differences included. – PesaThe Dec 01 '17 at 11:38
  • Sorry I've updated the typo `DIST_DIR` to `DST_DIR` – methuselah Dec 01 '17 at 11:45
  • So wait, what you're saying is I just need to change this line `echo "diff -qr ${!A_DIR} ${!B_DIR} >> $DST_DIR/output.txt"` to `echo "diff -r ${!A_DIR} ${!B_DIR} >> $DST_DIR/output.txt"`. I'm not clear here... – methuselah Dec 01 '17 at 12:08
  • @PesaThe the script runs as expected but for some reason nothing is outputted to the results file – methuselah Dec 01 '17 at 12:12
  • With the `-q` option, you get output like "these files differ, this file is missing from that directory" but not what the exact difference are. – tripleee Dec 01 '17 at 12:24
  • 1
    If there are no differences in the files, you will get an empty output. Do any of the files compared actually differ? – PesaThe Dec 01 '17 at 12:28
  • And just like @tripleee asked, could you specify where you want the output file to be created? Locally or on the server? – PesaThe Dec 01 '17 at 12:32
  • The files definitely differ. The output file will be created on the server. The lines above form part of a deployment script that is executed on a target server from TeamCity – methuselah Dec 01 '17 at 12:33
  • Does the `diff` output to stdout when removing `>> $DST_DIR/output.txt`? Are you getting any errors? – PesaThe Dec 01 '17 at 13:12

1 Answers1

0

It's looking like you are expecting the file to be created locally, but of course, it's in the command you are sending to the remote server, so that's where it's being created.

I don't think you need any of these redirections anyway.

# no need to touch, either
for loop in looopity loop; do
    echo "diff something"  # no redirect!
done |
ssh remoteserver >"$DST_DIR"/output.txt

Add a tee before the ssh if you really genuinely need to store the commands you are sending to the remote host.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • More generally, the variable redirection looks like a box of nails for this big hammer of yours. I'm imagine if we saw your general use case, we would be able to articulate a less convoluted way of doing what you want. – tripleee Dec 01 '17 at 11:23
  • Perhaps you are actually looking for https://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash – tripleee Dec 01 '17 at 11:23
  • Hi, this is part of a much larger script, so ideally I would like to just change this line: `echo "diff -qr ${!A_DIR} ${!B_DIR} >> $DST_DIR/output.txt"` to get it working – methuselah Dec 01 '17 at 12:10
  • Is my speculation correct that you hoped and expected the file to be created locally, but it ends up on the remote server, or is your problem of a different sort? Your question isn't very clear on this point. – tripleee Dec 01 '17 at 12:23
  • I want the `*.txt` file to be created on the remote server - with details of the diff command after it is executed. So the `*.sh` script that I am generating should contain lines like: `diff -qr methuselah/test1 methuselah/backup1 >> $DST_DIR/output.txt`. – methuselah Dec 01 '17 at 12:32