spbr
spbr

Reputation: 23

Bash Script - No file exist in ~/.ssh/

I'm trying to copy a file from: ~/.ssh/ but everytime I run the script it keeps saying

pi@raspberrypi:/etc/greenwich $ ./copybash.sh
cat: ~/.ssh/testfilegen2.log: No such file or directory

copybash.sh

!/bin/bash
sourceFile="~/.ssh/testfilegen2.log"
targetFile="/etc/network/interfaces2"
sudo cat "$sourceFile" > "$targetFile"
sudo service networking restart

Any Suggestions?

Thank you

Upvotes: 0

Views: 143

Answers (2)

Grégory Roche
Grégory Roche

Reputation: 172

Take a look to this snippet code:

#!/bin/bash
v1=~/'file1.txt'
v2=~/'file2.txt'
echo 'Hi!' > $v1 
cat $v1 > $v2
cat $v2

$ script.sh
Hi!

The documentation is in the section "Tilde Expansion" of the "General Commands Manual BASH".

Upvotes: 1

chepner
chepner

Reputation: 531480

Unquote the tilde in the assignment to sourceFile so that it expands properly. Tilde expansion does not occur on parameter expansion.

sourceFile=~/".ssh/testfilegen2.log"

(In this case, no quotes would be necessary at all, but just to demonstrate that the ~ and the following / are the only things that need to remain unquoted for tilde expansion to occur.)

Upvotes: 5

Related Questions