Linux 在 csh 中设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9739516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:13:35  来源:igfitidea点击:

Setting an environment variable in csh

linuxshellenvironment-variablescsh

提问by alwbtc

I have the following line at the first line in my script file:

我的脚本文件的第一行有以下行:

#!/bin/sh

So I'm using csh.(?)

所以我正在使用 csh。(?)

I wanto assign the output of the following to an environment variable:

我想将以下内容的输出分配给环境变量:

echo $MYUSR | awk '{print substr(
set $MYVAR = echo $MYUSR | awk '{print substr(
 #!/bin/csh

 set MYVAR = `echo $MYUSR | awk '{print substr(
   ls -l /bin/sh /bin/bash
,4)}'` echo $MYVAR
,4)}'
,4)}'

I try:

我尝试:

   MYVAR=$(echo $MYUSR | awk '{print substr(
   MYVAR=``echo $MYUSR | awk '{print substr(
MYVAR=`echo $MYUSR | awk ...`
,4)}`` echo $MYVAR # arg!! only one pair of enclosing back-ticks needed, # can't find the secret escape codes to make this look exactly right.
,4)}') echo $MYVAR

But it doesn't work, How can I do it? I want to do it in a sh file.

但它不起作用,我该怎么办?我想在 sh 文件中进行。

采纳答案by shellter

Your script should look like

你的脚本应该看起来像

#!/bin/csh

I don't have a way to test this right now, let me now if it doesn't work.

我现在没有办法测试这个,如果它不起作用,让我现在。



If you've inherited the basis of your script from someone else, with the #!/bin/sh, then you have to find out if /bin/sh is really the bourne shell, or if it is a link to /bin/bash

如果你从其他人那里继承了你的脚本的基础#!/bin/sh,那么你必须找出 /bin/sh 是否真的是 bourne shell,或者它是否是 /bin/bash 的链接

You can tell that by doing

你可以通过这样做来判断

MYVAR=`echo $MYUSR | awk '{print substr(
MYVAR=`echo $MYUSR | awk '{print substr(##代码##,4)}`
echo $MYVAR
,4)}` echo $MYVAR

if you get back information on files where the size is exactly the same, the you're really using bash, but called as /bin/sh

如果您获得有关大小完全相同的文件的信息,则您实际上是在使用 bash,但称为 /bin/sh

So try these 2 solutions

所以试试这两个解决方案

##代码##

AND

##代码##

in all cases (csh) included, the back-ticks AND the $( ... )are known as command substitution. What every output comes from running the command inside, is substituted into the command line AND then the whole command is executed.

在所有情况下 (csh) 包括在内,反引号和$( ... )被称为命令替换。内部运行命令的每个输出都被替换到命令行中,然后执行整个命令。

I hope this helps.

我希望这有帮助。

回答by dldnh

if it's /bin/shit's bourne shell or bash, and use back quotes to execute something and this to assign that...

如果它是/bin/shbourne shell 或 bash,并使用反引号来执行某些内容,然后将其分配给...

##代码##

回答by John Retterer

That script first line indicates that it should be interpreted by the Bourne shell (sh), not csh. Change it to

该脚本的第一行表明它应该由 Bourne shell (sh) 而不是 csh 来解释。将其更改为

##代码##

回答by mtk

The first line of your code shows clearly you are not using a csh. You are using a plain shenvironment/shell. You have 2 options:

代码的第一行清楚地表明您没有使用csh. 您正在使用普通sh环境/外壳。您有 2 个选择:

  1. Either change the first line to #!/bin/cshOR
  2. Keeping first line unchanged, update the code for setting the variable.

    ##代码##
  1. 将第一行更改为#!/bin/cshOR
  2. 保持第一行不变,更新设置变量的代码。

    ##代码##

Let me know, if you get any error.

让我知道,如果你有任何错误。