【问题标题】:bash shell $HOME assignment and script executionbash shell $HOME 赋值和脚本执行
【发布时间】:2012-02-15 11:21:41
【问题描述】:

我刚刚开始学习 Unix,到目前为止遇到了两个基本但难以解决的问题:

  • 当我在 shell 脚本中将HOME='' 设置为指定目录时,当前 目录似乎不再被识别。也就是说,'cd ~/' 会吐出消息:'no such file or directory' 消息。尽管奇怪的是,如果在脚本中进行了别名分配,源调用似乎仍然激活了它们。怎么会?

例如:

$ more .profile
HOME="~/Documents/Basics/Unix/Unix_and_Perl_course"
cd $HOME
[...]
$ source .profile
-bash: cd: ~/Documents/Basics/Unix/Unix_and_Perl_course: No such file or directory
  • 当我通过 nano ('hello.sh') 创建一个简单的 shell 脚本时,我似乎无法通过在终端中键入 'hello.sh' 来执行它。即使在我 'chmod +x' 文件后,此问题也无法解决。有什么问题?

例如:

$ more hello.sh 
# my first Unix shell script
echo "Hello World"
$ hello.sh
bash: hello.sh: command not found

谢谢!

【问题讨论】:

  • 这里有两个不同的问题。最好分别问他们。最好给出一个有意义的标题,让问题对未来的读者更有用。
  • 感谢您的建议,迈克尔!我会记住的。

标签: macos bash shell unix execution


【解决方案1】:

您也不想“重载”$HOME,HOME 的默认位置始终是您的主目录。如果你搞砸了,很多东西都会坏掉。

至于 hello.sh - 那是因为你没有 '.'在你的 $PATH 中。 (这是好事)

试试:

./hello.sh

如果它说它不能执行

chmod 755 hello.sh
./hello.sh

【讨论】:

    【解决方案2】:
    1. ~ = $HOME
    2. 。 (pwd) 不在 $PATH 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多