Bash脚本 - 〜/ .ssh /中没有文件

时间:2017-02-26 19:39:02

标签: linux bash quoting tilde-expansion

我正在尝试复制文件:〜/ .ssh / 但每次我运行脚本时都会说

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

任何建议?

谢谢

2 个答案:

答案 0 :(得分:5)

取消指定sourceFile中的波浪号,使其正确展开。参数扩展时不会发生Tilde扩展。

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

(在这种情况下,根本不需要引号,只是为了证明~和以下/是唯一需要保持不引用波浪扩展的东西。 )

答案 1 :(得分:1)

请看一下这段代码:

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

$ script.sh
Hi!

文档在“Tilde扩展”部分 “General Commands Manual BASH”。