简体   繁体   English

您应该更改shell脚本中的当前目录吗?

[英]Should you change the current directory in a shell script?

I've always mentally regarded the current directory as something for users, not scripts, since it is dependent on the user's location and can be different each time the script is executed. 我总是在心理上将当前目录视为用户而不是脚本,因为它取决于用户的位置,并且每次执行脚本时都可以不同。

So when I came across the Java jar utility's -C option I was a little puzzled. 所以当我遇到Java jar实用程序的-C选项时,我有点困惑。


For those who don't know the -C option is used before specifying a file/folder to include in a jar. 对于那些不知道-C选项的人在指定要包含在jar中的文件/文件夹之前使用。 Since the path to the file/folder is replicated in the jar, the -C option changes directories before including the file: 由于文件/文件夹的路径在jar中复制,因此-C选项在包含文件之前更改目录:

in other words: 换一种说法:

jar -C flower lily.class

will make a jar containing the lily.class file, whereas: 将制作一个包含lily.class文件的jar,而:

jar flower/lily.class

will make a flower folder in the jar which contains lily.class 将在包含lily.class的罐子里制作一个flower文件夹


For a jar-ing script I'm making I want to use Bourne wild-cards folder/* but that would make using -C impossible since it only applies to the next immediate argument. 对于一个jar-ing脚本我正在制作我想要使用Bourne wild-cards folder/*但是这会使-C不可能,因为它只适用于下一个立即参数。

So the only way to use wild-cards is run from the current directory; 因此,使用通配符的唯一方法是从当前目录运行; but I still feel uneasy towards changing and using the current directory in a script. 但是我仍然对在脚本中更改和使用当前目录感到不安。

Is there any downside to using the current directory in scripts? 在脚本中使用当前目录有什么缺点吗? Is it frowned upon for some reason perhaps? 是不是因为某些原因而不赞成?

I don't think there's anything inherently wrong with changing the current directory from a shell script. 我认为从shell脚本更改当前目录没有任何内在错误。 Certainly it won't cause anything bad to happen, if taken by itself. 当然,如果单独使用它不会导致任何不好的事情发生。

In fact, I have a standard script that I use for starting up a Java-based server, and the very first line is: 事实上,我有一个标准脚本用于启动基于Java的服务器,第一行是:

cd `dirname $0`

This ensures that the rest of the commands in the script are executed in the directory that contains the script file itself (useful when a single machine is hosting multiple server instances), regardless of where the shell script was actually invoked from. 这可确保脚本中的其余命令在包含脚本文件本身的目录中执行(在单个计算机托管多个服务器实例时很有用),无论shell脚本实际从何处被调用。 Without changing the current directory in the script, it would only work correctly if the user remember to manually cd into the corresponding directory before running the script. 在不更改脚本中的当前目录的情况下,只有在用户记得在运行脚本之前手动cd到相应的目录中时,它才能正常工作。

In this case, performing the cd operation from within the script removes a manual step from the server startup/shutdown process, and makes things slightly less error-prone as a result. 在这种情况下,从脚本中执行cd操作会从服务器启动/关闭过程中删除手动步骤,从而使事情稍微不那么容易出错。

So as with most things, there are legitimate uses for this sort of thing. 因此,对于大多数事情,这类事情都有合法用途。 And I'm sure there are also some questionable ones, as well. 而且我确信也有一些值得怀疑的问题。 It really depends upon what's most appropriate for your specific use-case. 这实际上取决于最适合您具体用例的内容。 Which is something I can't really comment on...I always just let maven build my JAR's for me. 这是我无法评论的东西......我总是让maven为我建立我的JAR。

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

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