为什么这个 tcsh shell 脚本在设置 PATH LD_LIBRARY_PATH 时给出错误
我创建了一个 tcsh shell 脚本,如下所示:
#!/bin/tcsh
setenv PATH ""
setenv PATH .:$HOME/bin:/usr/sbin:/usr/bin:/bin:/usr/X11R6/bin:/usr/local/cuda/bin:/usr/local/bin:/usr/bin:$PATH
setenv LD_LIBRARY_PATH ""
setenv LD_LIBRARY_PATH .:/usr/local/cuda/lib:/usr/local/cuda/lib64/:/usr/local/cuda:/usr/lib:/usr/lib32:/usr/local/cuda/bin:/usr/local/lib/:${LD_LIBRARY_PATH}
然后我使该脚本可执行,当我尝试将其作为
./script.sh 执行时,它会给出以下错误:
script.sh: 3: setenv: not find
script.sh: 4: setenv: 未找到
script.sh: 6: setenv: 未找到
script.sh: 7: setenv: 未找到
任何指针,如何在我的 shell 脚本中设置这些路径?
I have created a tcsh shell script as follows:
#!/bin/tcsh
setenv PATH ""
setenv PATH .:$HOME/bin:/usr/sbin:/usr/bin:/bin:/usr/X11R6/bin:/usr/local/cuda/bin:/usr/local/bin:/usr/bin:$PATH
setenv LD_LIBRARY_PATH ""
setenv LD_LIBRARY_PATH .:/usr/local/cuda/lib:/usr/local/cuda/lib64/:/usr/local/cuda:/usr/lib:/usr/lib32:/usr/local/cuda/bin:/usr/local/lib/:${LD_LIBRARY_PATH}
Then I have made this script executable and when I try to execute it as
./script.sh, it gives following errors:
script.sh: 3: setenv: not found
script.sh: 4: setenv: not found
script.sh: 6: setenv: not found
script.sh: 7: setenv: not found
Any pointers, how to set these paths in my shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的系统上没有出现同样的错误。
更新:请参阅最后两段,了解我对正在发生的事情的最佳猜测。
解决问题
我最初的最佳猜测是,您可以通过将 shebang 更改为来
(或者使用
tcsh 添加到原始 csh 的功能主要用于交互式使用而不是脚本编写。)
-f
选项告诉tcsh 在启动时不会处理您的.login
和.cshrc
或.tcshrc
文件。一般来说,您不希望脚本来执行此操作;它使脚本的行为取决于您自己的环境设置。也许您的.login
中有些东西与setenv
做了一些奇怪的事情,尽管我想不出它可能是什么。尝试添加-f
并查看是否有帮助。即使没有,你也应该这样做。(并且不要将
-f
用于/bin/sh
或/bin/bash
脚本;它意味着其他东西,而不是需要。)其他一些观察:
将
$PATH
和$LD_LIBRARY_PATH
设置为空字符串是没有用的。只需删除这两行即可。编辑:
在重新阅读您的问题时,我明白您在做什么。您将
$PATH
设置为空字符串,然后在其前面添加更多文本:这比我想象的更有意义,但编写一个命令仍然更简单:
Putting
. 放在你的
$PATH
中,尤其是在开头,是一个坏主意。想一想,如果您在某个目录中运行脚本,而有人在其中存放了执行某些令人讨厌的命令名称ls
的命令,会发生什么情况。如果要执行当前目录中的命令,请使用./command
。 (将.
放在$PATH
的 end 更安全,但仍然不是一个好主意。)(并且使用 tcsh 或 csh 作为脚本语言(相对于交互式 shell)也被广泛认为是一个坏主意。 rel="nofollow">这篇文章,即使它不能说服您放弃 tcsh 脚本,至少会让您意识到其中的陷阱。)
哦,如果它是一个 tcsh 脚本,为什么要这样做你称之为
script.sh
?在类 Unix 系统(与 Windows 不同)下不需要文件名后缀,但通常.sh
后缀意味着它是 Bourne shell 脚本。将其命名为script.tcsh
或script.csh
,或者只是script
。编辑:
仔细查看您收到的错误消息,看起来错误来自
/bin/sh
,不是来自 tcsh。在我的系统上,当我将
setenv
更改为Setenv
(一个不存在的命令)时,使用 tcsh 运行脚本会给出:这与您向我们展示的错误消息不匹配。当我将其显式运行为
/bin/sh foo.tcsh
(仅保留setenv
命令)时,我得到:这确实与您收到的错误的格式匹配。
您说
/bin/tcsh --version
给出了正确的结果,所以这不是问题。不知何故,该脚本是由/bin/sh
执行的,而不是由 tcsh 执行的。这是我对正在发生的事情的最佳猜测。您正在 Cygwin 或 MSYS 上运行,但您是从
cmd
shell 调用脚本,而不是从 Cygwin shell 调用脚本。您的 Windows 系统已配置为识别.sh
后缀,以指示该文件是由C:\cygwin\bin\sh.exe
执行的脚本(如我之前提到过,文件后缀通常在 Unix 或 Cygwin 环境中并不重要,但在 Windows 上却很重要。最简单的解决方案可能是重写脚本以符合 Bourne shell 语法。但应该有办法让 Windows 调用 Cygwin 的 tcsh 来执行它。如果我猜对了,请告诉我们,我们也许可以找到解决方案。
I don't get the same error on my system.
UPDATE : See the last two paragraphs for my best guess about what's going on.
My initial best guess was that you can fix the problem by changing shebang
to
(Or use
The features that tcsh adds to the original csh are mostly useful for interactive use rather than scripting.)
The
-f
option tells tcsh not to process your.login
and.cshrc
or.tcshrc
files when it starts up. Generally you don't want a script to do this; it makes the script's behavior dependent on your own environment setup. Perhaps you have something in your.login
that does something odd withsetenv
, though I can't think of what it might be. Try adding the-f
and see if that helps. Even if it doesn't, you should do that anyway.(And don't use
-f
for/bin/sh
or/bin/bash
scripts; it means something else, and isn't needed.)Some other observations:
Setting
$PATH
and$LD_LIBRARY_PATH
to the empty string is useless. Just delete those two lines.EDIT :
On re-reading your question, I see what you're doing there. You set
$PATH
to the empty string, and then prepend more text to it:That makes more sense than I thought it did, but it's still simpler to write one command:
Putting
.
in your$PATH
, especially at the beginning, is a bad idea. Think about what happens if your run the script in a directory where someone has deposited a command namels
that does something nasty. If you want to execute a command in the current directory, use./command
. (Putting.
at the end of$PATH
is safer, but still not a very good idea.)(And using tcsh or csh as a scripting language (as opposed to an interactive shell) is widely considered to be a bad idea as well. This article, even if it doesn't persuade you to give up on tcsh scripting, will at least make you aware of the pitfalls.)
Oh, and if it's a tcsh script, why do you call it
script.sh
? Suffixes on file names aren't required under Unix-like systems (unlike Windows), but usually a.sh
suffix implies that it's a Bourne shell script. Call itscript.tcsh
, orscript.csh
, or justscript
.EDIT :
Taking a closer look at the error message you're getting, it looks like the errors are coming from
/bin/sh
, not from tcsh.On my system, when I change
setenv
toSetenv
(a nonexistent command), running the script with tcsh gives me:which doesn't match the error messages you showed us. When I run it explicitly as
/bin/sh foo.tcsh
(leaving thesetenv
commands alone), I get:which does match the format of the errors you got.
You say that
/bin/tcsh --version
gives correct results, so that's not the problem. Somehow the script is being executed by/bin/sh
, not by tcsh.Here's my best guess about what's going on. You're running on Cygwin, or maybe MSYS, but you're invoking your script from a
cmd
shell, not from a Cygwin shell. Your Windows system has been configured to recognize a.sh
suffix to indicate that the file is a script to be executed byC:\cygwin\bin\sh.exe
(as I mentioned before, file suffixes don't usually matter on Unix, or in the Cygwin environment, but they do on Windows).The simplest solution would probably be to rewrite the script to conform to Bourne shell syntax. But there should be ways to cause Windows to invoke Cygwin's
tcsh
to execute it. If I've guess right, let us know and we can probably come up with a solution.我没发现你的脚本有什么问题。在我的盒子上效果很好。
该错误的唯一原因(我能想到的)是 tcsh 不知何故没有被用作解释器。
如果我在
#!/bin/tcsh
之前添加换行符,我可以重现该错误。当 shebang 不是文件中的第一个字符时,解释器指令不会生效,并且使用您当前的 shell(我猜您的 shell 不是 c-shell 变体(csh 或 tcsh)?)。因此,请检查
#!/bin/tcsh
确实是文件中的第一行,之前没有空格。要确定实际使用的解释器,请尝试将其添加到您的脚本中:
示例:
I don't see anything wrong with your script. It works fine on my box.
The only reason for that error (that I can think of) is
tcsh
is somehow not being used as the interpreter.I can reproduce the error if I add a line feed before
#!/bin/tcsh
. When the shebang is not the first chars in the file, the interpreter directive does not take effect and your current shell is used (I'm guessing your sheel is not c-shell variant (csh or tcsh)?).So, check that
#!/bin/tcsh
is indeed the first line in the file, with no whitespace before.To determine which interpreter is actually being used, try adding this to your script:
example:
刚刚遇到这个问题,结果是由于脚本文件具有 Windows EOL。一旦我把它清理干净,一切都很好。
just hit this problem, and it turned out to be due to the script file having Windows EOLs. Once i cleaned it up, all was good.