【问题标题】:Bash Script - No file exist in ~/.ssh/Bash 脚本 - ~/.ssh/ 中不存在文件
【发布时间】:2017-02-26 19:39:01
【问题描述】:

我正在尝试从以下位置复制文件:~/.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

有什么建议吗?

谢谢

【问题讨论】:

  • 您确实需要提供更多信息,尤其是 copybash.sh 脚本中的代码。
  • @Fred 包含 copybash.sh 代码

标签: linux bash quoting tilde-expansion


【解决方案1】:

sourceFile 的赋值中取消引用波浪号,以便它正确扩展。参数扩展时不会发生波浪号扩展。

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

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

【讨论】:

    【解决方案2】:

    看看这个sn-p代码:

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

    文档位于“波浪号扩展”部分 “General Commands Manual BASH”。

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多