命令在脚本中不起作用,但在shell中起作用

时间:2010-12-29 20:45:41

标签: linux bash scripting ubuntu ssh

我正在编写一个使用SSH“个人资料”的脚本,〜/ scripts / ssh-profiled.sh

PROFILE=`cat ~/script/ssh-profiles/$1`
echo [ssh $PROFILE]
ssh $PROFILE

〜/脚本/ SSH型材/ tummi

-i ~/Dropbox/security/key-nopass/key-nopass.pvt bart@example.com

当我运行脚本时,它失败了:

bart@bart-laptop:~$ script/ssh-profiled.sh tummi
[ssh -i ~/Dropbox/security/key-nopass/key-nopass.pvt bart@example.com]
Warning: Identity file ~/Dropbox/security/key-nopass/key-nopass.pvt not accessible: No such file or directory.
bart@example.com's password:

但这有效:

bart@bart-laptop:~$ ssh -i ~/Dropbox/security/key-nopass/key-nopass.pvt bart@example.com
Linux tummi 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux
Ubuntu 10.04.1 LTS

Welcome to the Ubuntu Server!

我的脚本中是否有错误/陷阱?

4 个答案:

答案 0 :(得分:5)

将第1行更改为

eval PROFILE=`cat ~/script/ssh-profiles/$1`

有关说明,请参阅here

答案 1 :(得分:2)

你文件中的〜需要是完整的主目录路径,它不会被扩展。

答案 2 :(得分:1)

我的猜测是“〜/”在以这种方式传递时没有被解释为预期。尝试使用显式完整路径。

答案 3 :(得分:0)

.pvt文件的权限是什么?如果只有您具有读访问权限,并且没有人可以执行它,那么您的脚本可能甚至无法查看该文件。这可能就是您收到"...not accessible: No such file or directory."消息的原因。