1

I'm writting a quick script for transfering a webpage from a remote server to my local machine. The problem is that I get a ": No such file or directory" error for my local folder. But if I don't use variable and just type the command in the script, it works. It works even if I echo the command and then just run it in the terminal. Im thankfull for any ideas!

This is the script:

#!/bin/bash
WEBPAGE_NAME="wikiwebpageb"
USERNAME="banana"
IP="192.168.0.12"
PORT_NUMBER="4561"
WEB_BACKUP_DIR="~/backups/wiki_backups/webpage/"
LOCAL_WEB_BACKUP_DIR="~/backups/wiki_backups/webpage/"

LOGFILE=~/backups/backups.log

scp -P $PORT_NUMBER ${USERNAME}@${IP}:${WEB_BACKUP_DIR}1_${WEBPAGE_NAME}.tgz ${LOCAL_WEB_BACKUP_DIR}1_${WEBPAGE_NAME}.tgz 2>> $LOGFILE
2
  • ~ does not expand inside a variable. That's OK for WEB_BACKUP_DIR because you want the remote system to expand the tilde. But not so good for the local variable, as you've noticed. Quick fix, use $HOME instead of ~ Commented Mar 1, 2019 at 19:43
  • Nice suggestion, will keep it in mind!
    – Charckle
    Commented Mar 1, 2019 at 20:14

1 Answer 1

2

Because tilde expansion does not work inside quotes, these two lines won't do what you hope:

WEB_BACKUP_DIR="~/backups/wiki_backups/webpage/"
LOCAL_WEB_BACKUP_DIR="~/backups/wiki_backups/webpage/"

To enable tilde expansion, replace them with:

WEB_BACKUP_DIR=~/"backups/wiki_backups/webpage/"
LOCAL_WEB_BACKUP_DIR=~/"backups/wiki_backups/webpage/"

But, there is still another issue: the tilde expansion occurs on the local machine using the local users home directory. If the remote user has a different name and different home directory, you need to put that in explicitly, maybe something like:

WEB_BACKUP_DIR="/home/${USERNAME}/backups/wiki_backups/webpage/"
LOCAL_WEB_BACKUP_DIR=~/"backups/wiki_backups/webpage/"

Also, it's bad practice to use all capitals for your variable names. The system uses all capitals for its names and you don't want to accidentally overwrite one. It's safe to use lower or mixed case names.

1
  • 1
    I manage to fix it by removing "" in the directory patchs like: web_backup_dir=~/backups/wiki_backups_webpage/. I thought that the variable would be place in the line and only then the program would read it, thus the ~ would not cause problems. Thanks for the heads up for the varialbes and thank for the answer!
    – Charckle
    Commented Mar 1, 2019 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .