我想知道是否有可能在usr / bin(即)中创建一个导致shell脚本的“链接”。
但我只想写
% shellscript
而不是
% sh shellscript.sh
有点像别名。
这可能吗?
答案 0 :(得分:47)
制作剧本的第一行
#!/bin/sh
然后输入命令使其可执行:
chmod +x shellscript.sh
如果您现在将脚本放在系统的 PATH 变量上的bin
文件夹中,您就可以直接运行它。要查看路径中的文件夹,请键入:
echo $PATH
我通常将/home/[my username]/bin
用于我编写的脚本,以便它们不会干扰系统上的其他用户。如果我希望它们适用于所有用户,我使用的/usr/local/bin
在大多数发行版中都是空的。
脚本文件名末尾的.sh
只是一种约定,可以帮助您记住它是什么类型的文件。如果您将其重命名为shellscript
,它仍然有用,例如,这将完成您的要求。
答案 1 :(得分:15)
您可以使shell脚本可执行(chmod +x shellscript.sh
)。然后你可以从/ usr / bin(ln -s shellscript.sh /usr/bin/shellscript
)链接到它。
答案 2 :(得分:2)
是。您可以使用ln
创建指向shellscript.sh
shellscript
的链接。然后,您需要将其设为可执行文件,但在此之后(假设您的路径上有/usr/bin
),您可以使用shellscript
运行它。
答案 3 :(得分:2)
除了使脚本可执行并将其链接到/ usr / bin之外,正如其他人所建议的那样,您还需要将“shebang”行添加到脚本的顶部:
#!/bin/sh
# your commands here
这允许你指定哪个shell解释器(bash,bourne shell,c-shell,perl,python ......) 应该用来执行你的脚本。
答案 4 :(得分:-1)
我考虑过在Unix / MacOSX中使用文件扩展名的问题,我能提供的最佳答案是我多年前写的一些代码中的开发评论的粘贴,以自动完成更新脚本的整个繁琐过程。在/ 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...
简而言之,我的自动化代码(相当于在此发布的内容)'sudo'在/ usr / local / bin中插入了一个可执行的无扩展名的程序副本,但开发目录中的文件保留了它的脚本/程序语言扩展到位。两个副本在文件顶部都有相同的shebang行。当然,无数其他重复性任务也会在此自动化代码中自动完成。但最终的回报是与编写新“命令”或调整现有/ usr / local / bin'命令的过程相关的“开销”现在需要几次击键。
我仍留在雪豹身上并对狮子保持警惕。因此,如果Lion的情况有所不同,我会为任何困惑道歉。此外,如果您的操作系统没有相应的QuickLook,我所说的一些内容可能会丢失,但我认为任何Linux / Unix用户仍然可以通过vim / less(http:// www-)从代码着色中受益zeuthen.desy.de/~friebel/unix/less/README)命令。
我的一些自动样板着色代码输出的示例,它有助于通过开发目录中的代码进行QuickLooking:
/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中的'command'副本时,将运行该脚本: / p>
##################################### 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"
总之,对我来说,有两种理由可以使用两种文件命名方式。