在csh中设置环境变量 [英] Setting an environment variable in csh

查看:856
本文介绍了在csh中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在脚本文件的第一行中包含以下行:

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

#!/bin/sh

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

So I'm using csh.(?)

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

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

echo $MYUSR | awk '{print substr($0,4)}'

我尝试:

set $MYVAR = echo $MYUSR | awk '{print substr($0,4)}'

但这不起作用, 我该怎么做?我想在一个sh文件中完成该操作.

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

推荐答案

您的脚本应类似于

 #!/bin/csh

 set MYVAR = `echo $MYUSR | awk '{print substr($0,4)}'`

 echo $MYVAR

我目前无法测试此方法,如果它不起作用,请立即让我使用.

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

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

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

您可以这样做

   ls -l /bin/sh /bin/bash

如果返回有关大小完全相同的文件的信息,则说明您确实在使用bash,但称为/bin/sh

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

所以尝试这两种解决方案

So try these 2 solutions

   MYVAR=$(echo $MYUSR | awk '{print substr($0,4)}')
   echo $MYVAR

AND

   MYVAR=``echo $MYUSR | awk '{print substr($0,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.

在所有情况下(csh),

都包括反引号和$( ... ),称为命令替换. 每个输出来自内部运行命令的结果,被替换为命令行,然后执行整个命令.

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中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆