执行 shell 脚本而不隐式调用 sh

发布于 2024-09-04 08:15:30 字数 191 浏览 2 评论 0原文

我想知道是否可以在 usr/bin (即)中建立一个指向 shell 脚本的“链接”。

但我只想写

% shellscript

而不是

% sh shellscript.sh

像别名一样。

这可能吗?

I was wondering if it is possible to make a "link" in usr/bin (i.e.) that leads to a shell-script.

But I want just to write

% shellscript

instead of

% sh shellscript.sh

kinda like an alias.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山有枢 2024-09-11 08:15:30

制作脚本的第一行

#!/bin/sh

然后通过输入以下命令使其可执行:

chmod +x shellscript.sh

如果您现在将脚本放置在系统 PATH 变量上的 bin 文件夹中,那么您将就可以直接运行了。要查看路径中的文件夹,请键入:

echo $PATH

我通常将 /home/[我的用户名]/bin 用于我编写的脚本,这样它们就不会干扰系统上的其他用户。如果我希望它们适用于所有用户,我可以使用 /usr/local/bin ,它在大多数发行版上都是空的。

脚本文件名末尾的 .sh 只是一种约定,可帮助您记住它是什么类型的文件。例如,如果您将其重命名为 shellscript,它仍然可以工作,这将满足您的要求。

Make the first line of the script

#!/bin/sh

Then make it executable by typing the command:

chmod +x shellscript.sh

If you now place the script in a bin folder that is on your system's PATH variable and you will be able to run it directly. To see the folders in your path, type:

echo $PATH

I usually use /home/[my username]/bin for scripts that I have written so that they don't interfere with other users on the system. If I want them to be for all users, I use /usr/local/bin which is supplied empty on most distributions.

The .sh on the end of the script's filename is only a convention to help you remember what kind of file it is. It will still work if you rename it to just shellscript, for example, which will complete your requirements.

一个人的夜不怕黑 2024-09-11 08:15:30

您可以使 shell 脚本可执行 (chmod +x shellscript.sh)。然后您可以从 /usr/bin 链接到它(ln -s shellscript.sh /usr/bin/shellscript)。

You can make the shell script executable (chmod +x shellscript.sh). Then you can link to it from /usr/bin (ln -s shellscript.sh /usr/bin/shellscript).

贱人配狗天长地久 2024-09-11 08:15:30

是的。您可以使用 ln 创建一个指向 shellscript.sh 的链接,名为 shellscript。然后,您需要使其可执行,但之后(假设 /usr/bin 在您的路径上)您可以使用 shellscript 运行它。

Yes. You can use ln to create a link to shellscript.sh named shellscript. You will then need to make it executable, but after that (assuming /usr/bin is on your path) you can run it with shellscript.

凌乱心跳 2024-09-11 08:15:30

除了使脚本可执行并将其链接到 /usr/bin 之外,正如其他人所建议的那样,您还需要将“shebang”行添加到脚本的顶部:

#!/bin/sh

# your commands here

这可以让您指定哪个 shell 解释器(bash、bourne shell、c-shell、perl、python...)
应该用来执行你的脚本。

In addition to making the script executable and linking it into /usr/bin, as others have suggested, you will also want to add the "shebang" line to the top of the script:

#!/bin/sh

# your commands here

This lets you specify which shell interpreter (bash, bourne shell, c-shell, perl, python...)
should be used to execute your script.

铁憨憨 2024-09-11 08:15:30

我思考了在 Unix/MacOSX 中使用文件扩展名的问题,我能提供的最佳答案是我几年前编写的一些代码中的开发注释的粘贴,以自动化更新我编写的包含在 /usr/ 中的脚本的整个繁琐过程。本地/bin。

I have begun to
change how I develop code for my contributions to
/ usr / local / bin. In the past I would always leave the development
file without an extension and depend on the shebang
#!/usr/bin/env ...) line. The flaw with using this approach
exclusively is it does not allow me to utilize the code colorizing
capabilities I have available for programming languages as deftly
as I might. Also allot of development environments do not
understand shebang and so see the code as plain text. Nor are the
contents of files without file extensions accessible via the MacOSX
QuickLook feature...

简而言之,我的自动化代码(而不是在这里发布)“sudo”在 /usr/local/bin 中插入了程序的可执行无扩展名副本,但开发目录中的文件保留了其脚本/程序语言扩展名。两个副本在文件顶部都有相同的 shebang 行。当然,无数其他重复性任务也在此自动化代码中实现了自动化。但最终的回报是与编写新“命令”或调整现有 /usr/local/bin“命令”过程相关的“开销”,现在需要敲击几次键盘。

我仍然选择 Snow Leopard,并对 Lion 保持警惕。因此,如果《Lion》的情况有所不同,我对造成的任何混乱表示歉意。另外,如果您的操作系统没有相当于 QuickLook 的功能,您可能会忘记我所说的一些内容,但我认为任何 Linux/Unix 用户仍然可以通过 vim/less 受益于代码着色(http://www- zeuthen.desy.de/~friebel/unix/less/README)命令。

我的一些自动样板着色代码输出的示例,有助于通过开发目录中的代码进行快速查找:

/usr/bin/highlight --syntax sh --style neon -i "/Users/pcs/Projects  /Shell/Bash/findpdftext/findpdftext.sh" >| "/Users/pcs/Projects/Shell /Bash/findpdftext/findpdftext.sh.html" 

生成此样板 html 不需要花费任何费用,并且在 QuickLook 中比自动生成的手册页更漂亮。另一个自动输出的小例子,它进入程序开发目录中的脚本,当我准备将对相关脚本的任何更改提交到 /usr/local/bin 中的“命令”副本时,该脚本将运行

##################################### REWIRE findpdftext.sh: 
chmod 777 "/Users/pcs/Projects/Shell/Bash/findpdftext/findpdftext.sh" 
cp -f "findpdftext.sh" "findpdftext" 
sudo ln -f "findpdftext" /usr/local/bin 
cp -f "findpdftext" "/Users/pcs/man/cat1/findpdftext.1" 

############### SYMBOLIC-LINK-NAMES supplied from command-line: 
##### fpdf is a symbolic link to findpdftext 
sudo ln -s -f "/usr/local/bin/findpdftext" "/usr/local/bin/fpdf" 
cp -f "/usr/local/bin/findpdftext" "/Users/pcs/man/cat1/fpdf.1" 

############### SYMBOLIC-LINK-NAMES supplied from command-line: 
##### fpt is a symbolic link to findpdftext 
sudo ln -s -f "/usr/local/bin/findpdftext" "/usr/local/bin/fpt" 
cp -f "/usr/local/bin/findpdftext" "/Users/pcs/man/cat1/fpt.1" 

:结论,对我来说,使用这两种文件命名风格都是有原因的。

I pondered this question of using filename extensions in Unix/MacOSX and the best answer I can provide is a paste from my development comments in some code I wrote years ago to automate the whole tedious process of updating scripts I write for inclusion into /usr/local/bin.

I have begun to
change how I develop code for my contributions to
/ usr / local / bin. In the past I would always leave the development
file without an extension and depend on the shebang
#!/usr/bin/env ...) line. The flaw with using this approach
exclusively is it does not allow me to utilize the code colorizing
capabilities I have available for programming languages as deftly
as I might. Also allot of development environments do not
understand shebang and so see the code as plain text. Nor are the
contents of files without file extensions accessible via the MacOSX
QuickLook feature...

In short, my automation code (rather to much to post here) 'sudo' inserts an executable extension-less copy of the program in /usr/local/bin but the file in the development directory keeps it's script/program language extension in place. Both copies have the same shebang line at the top of the file. Myriad other repetitive tasks are automated in this automation code as well of course. But the ultimate reward is the 'overhead' associated with the process of writing new 'commands' or tweaking existing /usr/local/bin 'commands' takes a couple keystrokes now.

I remain on Snow Leopard and remain wary of Lion. So if things are different on Lion I apologize for any confusion. Also if your OS doesn't have the equivalent of QuickLook some of what I've said may be lost on you but I think any Linux/Unix user can still benefit from code colorizing via the vim/less (http://www-zeuthen.desy.de/~friebel/unix/less/README) commands.

An example of some of my automated boilerplate colorizing code output that facilitates QuickLooking thru code in the development directory:

/usr/bin/highlight --syntax sh --style neon -i "/Users/pcs/Projects  /Shell/Bash/findpdftext/findpdftext.sh" >| "/Users/pcs/Projects/Shell /Bash/findpdftext/findpdftext.sh.html" 

Generating this boilerplate html costs me nothing and is prettier in QuickLook than the man pages that are auto generated as well. Another little example gob of automated output that goes into the script in the program's development directory which will be run when I'm ready to commit any changes to the script in question to the 'command' copy in /usr/local/bin:

##################################### REWIRE findpdftext.sh: 
chmod 777 "/Users/pcs/Projects/Shell/Bash/findpdftext/findpdftext.sh" 
cp -f "findpdftext.sh" "findpdftext" 
sudo ln -f "findpdftext" /usr/local/bin 
cp -f "findpdftext" "/Users/pcs/man/cat1/findpdftext.1" 

############### SYMBOLIC-LINK-NAMES supplied from command-line: 
##### fpdf is a symbolic link to findpdftext 
sudo ln -s -f "/usr/local/bin/findpdftext" "/usr/local/bin/fpdf" 
cp -f "/usr/local/bin/findpdftext" "/Users/pcs/man/cat1/fpdf.1" 

############### SYMBOLIC-LINK-NAMES supplied from command-line: 
##### fpt is a symbolic link to findpdftext 
sudo ln -s -f "/usr/local/bin/findpdftext" "/usr/local/bin/fpt" 
cp -f "/usr/local/bin/findpdftext" "/Users/pcs/man/cat1/fpt.1" 

In conclusion, for me there are reasons for using both file naming styles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文