在 Linux 上编写脚本
我正在尝试创建一个脚本,该脚本将在列表中的每个文件上运行一个程序。我一直在尝试使用 .csh 文件来执行此操作(我不知道这是否是最好的方法),并且我从像 hello world 这样简单的东西开始,
echo "hello world"
问题是我无法执行此脚本,或验证它是否有效正确。 (我试图做 ./testscript.csh 这显然是错误的)。我还没有找到任何真正解释如何运行 C 脚本的东西,我猜还有更好的方法来做到这一点。我需要改变什么才能让它发挥作用?
I am trying to create a script that will run a program on each file in a list. I have been trying to do this using a .csh file (I have no clue if this is the best way), and I started with something as simple as hello world
echo "hello world"
The problem is that I cannot execute this script, or verify that it works correctly. (I was trying to do ./testscript.csh which is obviously wrong). I haven't been able to find anything that really explains how to run C Scripts, and I'm guessing there's a better way to do this too. What do I need to change to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将其标记为可执行文件; Unix 不会根据扩展名任意执行事情。
另外,我强烈建议使用
sh
或bash
而不是csh
,否则您很快就会了解csh
循环和控制流结构(有些东西仅当以特定方式完成时才能在其中工作,特别是对于单行版本,事情非常受到限制)。You need to mark it as executable; Unix doesn't execute things arbitrarily based on extension.
Also, I strongly recommend using
sh
orbash
instead ofcsh
, or you will soon learn about the idiosyncrasies ofcsh
's looping and control flow constructs (some things only work inside them if done a particular way, in particular with the single-line versions things are very limited).您可以使用./testscript.csh。但是,您需要首先使其可执行:
这意味着将 testscript 设置为具有用户的执行权限(无论该文件的所有者是谁 - 在本例中应该是您自己!)
还要告诉操作系统这是一个 csh 脚本您需要将其放在
第一行(其中
/path/to/csh
是系统上 csh 的完整路径。您可以通过发出命令which csh
来找到它>)。这应该会给你你想要的行为。
编辑 正如一些评论中所讨论的,您可能希望选择 C Shell (csh) 的替代 shell。它对于脚本编写来说并不是最友好的。
You can use ./testscript.csh. You will however need to make it executable first:
Which means set testscript to have execute permissions for the user (who ever the file is owned by - which in this case should be yourself!)
Also to tell the OS that this is a csh script you will need put
on the first line (where
/path/to/csh
is the full path to csh on your system. You can find that out by issuing the commandwhich csh
).That should give you the behvaiour you want.
EDIT As discussed in some of the comments, you may want to choose an alternative shell to C Shell (csh). It is not the friendliest one for scripting.
您有多种选择。
您可以从当前 shell 中运行该脚本。如果您运行的是 csh 或 tcsh,则语法为
source testscript.csh
。如果您运行 sh、bash、ksh 等,则语法为。 ./testscript.sh
。请注意,我更改了文件名后缀;source
或.
在当前 shell 中运行指定文件中的命令。如果您有任何特定于 shell 的语法,则除非您的交互式 shell 与脚本使用的语法相匹配,否则这将不起作用。如果脚本非常简单(只是一系列简单命令),那可能并不重要。您可以使脚本成为可执行程序。 (我将重复其他人已经写过的一些内容。)添加“shebang”作为第一行。对于 csh 脚本,请使用
#!/bin/csh -f
。-f
避免在您自己的个人启动脚本(.cshrc 等)中运行命令,这样可以节省时间并使其他人更有可能使用它。或者,对于 sh 脚本(推荐),使用#!/bin/sh
(没有-f
,它具有完全不同的含义)。无论哪种情况,请运行chmod +x the_script
,然后运行 ./the_script
。当我想要执行一些中等复杂的动作时,我经常使用一个技巧。假设我想删除当前目录中的一些文件,但不是全部,但标准无法用单个命令方便地表达。我可能会运行
ls > tmp.sh
,然后用我最喜欢的编辑器(我的恰好是 vim)编辑 tmp.h。然后我浏览文件列表并删除所有我想保留的文件。完成此操作后,我可以用命令替换每个文件名以将其删除;在 vim 中,:%s/.*/rm -f &/
。我在顶部添加#!/bin/sh
保存它,chmod +x foo.sh
,然后添加./foo.sh
。 (如果某些文件名可能包含特殊字符,我可以使用:%s/.*/rm -f '&'/
。)You have several options.
You can run the script from within your current shell. If you're running csh or tcsh, the syntax is
source testscript.csh
. If you're running sh, bash, ksh, etc., the syntax is. ./testscript.sh
. Note that I've changed the file name suffix;source
or.
runs the commands in the named file in your current shell. If you have any shell-specific syntax, this won't work unless your interactive shell matches the one used by the script. If the script is very simple (just a sequence of simple commands), that might not matter.You can make the script an executable program. (I'm going to repeat some of what others have already written.) Add a "shebang" as the first line. For a csh script, use
#!/bin/csh -f
. The-f
avoids running commands in your own personal startup scripts (.cshrc et al), which saves time and makes it more likely that others will be able to use it. Or, for a sh script (recommended), used#!/bin/sh
(no-f
, it has a completely different meaning). In either case, runchmod +x the_script
, then./the_script
.There's a trick I often use when I want to perform some moderately complex action. Say I want to delete some, but not all, files in the current directory, but the criterion can't be expressed conveniently in a single command. I might run
ls > tmp.sh
, then edit tmp.h with my favorite editor (mine happens to be vim). Then I go through the list of files and delete all the ones that I want to leave alone. Once I've done that, I can replace each file name with a command to remove it; in vim,:%s/.*/rm -f &/
. I add a#!/bin/sh
at the top save it,chmod +x foo.sh
, then./foo.sh
. (If some of the file names might have special characters, I can use:%s/.*/rm -f '&'/
.)