简体   繁体   English

bash shell $ HOME赋值和脚本执行

[英]bash shell $HOME assignment and script execution

I've just begun learning Unix and have so far encountered two elementary though difficult to resolve problems: 我刚刚开始学习Unix,到目前为止,尽管遇到了难以解决的问题,但遇到了两个基本问题:

  • When I set HOME='' in a shell script to a designated directory, the current directory no longer seems to be recognized. 当我在shell脚本中将HOME=''设置为指定目录时,似乎不再识别当前目录。 That is, 'cd ~/' spits out the message: 'no such file or directory' message. 也就是说,'cd~ /'吐出消息:'没有这样的文件或目录'消息。 Although, curiously enough, if aliases assignments are made within the script, a source call seems to activated them nonetheless. 奇怪的是,如果在脚本中进行了别名分配,则源调用似乎仍然激活了它们。 How come? 怎么会?

Ex: 例如:

$ 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
  • When I created a simple shell script via nano ('hello.sh'), I can't seem to execute it simply by typing 'hello.sh' in the terminal. 当我通过nano('hello.sh')创建一个简单的shell脚本时,我似乎无法通过在终端中输入'hello.sh'来执行它。 This issue fails to resolve even after I 'chmod +x' the file. 即使在我'chmod + x'文件后,此问题也无法解决。 What's the problem? 有什么问题?

Ex: 例如:

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

Thanks! 谢谢!

You also don't want to 'overload' $HOME, the default location for HOME is always your home directory. 您也不想'重载'$ HOME,HOME的默认位置始终是您的主目录。 If you goof with that, lots of things will break. 如果你这样做,很多事情都会破裂。

As far as hello.sh - thats because you don't have '.' 至于hello.sh-那是因为您没有'。' in your $PATH. 在你的$ PATH中。 (Which is a good thing) (这是件好事)

Try: 尝试:

./hello.sh

If it says it can't execute 如果它说它无法执行

chmod 755 hello.sh
./hello.sh
  1. ~ = $HOME 〜= $ HOME
  2. . (pwd) is not in $PATH (pwd)不在$ PATH中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM