0

I wrote a script that would zip a folder and then store it another. So far this is working great. But now I want to send it using scp. I want to use eval so I can change them easily. But when I use it to send the file to some destination it always tells me that the the directory or file does not exist. If I do it without $scriptd then it is working like a charm.

Any ideas why?

    #!/bin/bash
    
    # Implementation of variables
    today=$(date +"%Y-%m-%d")
    source="~/Documents"; eval source=$source
    target="~/Backups"; eval target=$target
    scriptd="~/Documents/Backups"; eval scriptd=$scriptd
    
    main(){
        backup
    }
    
    backup(){
        tar -zvcf $today.tar.gz -P $source
        rsync -zhap --progress $scriptd/$today.tar.gz $target
        rm $scriptd/$today.tar.gz
        scp $target/$today.tar.gz pi@I-Am-An-IP:$scriptd

    }
    
    main
    
    exit 0
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • When you do `scriptd="~/Documents/Backups"; eval scriptd=$scriptd`, scriptd will be like `/home/john/Documents/Backups` which may not exist on `I-Am-An-IP` – Philippe Jan 10 '21 at 22:37
  • 3
    You wouldn't need the `eval` commands if you didn't quote the `~` characters. Just use e.g. `source=~/Documents` (or use `$HOME` instead of `~`) and skip the `eval`. Except for `scriptd`, where you probably *do* want the quotes, and *don't* want `eval`. Oh, and you should also double-quote all the variable references; [shellcheck.net](https://www.shellcheck.net) will be happy to point all of this out. – Gordon Davisson Jan 11 '21 at 01:02

0 Answers0