什么是$?在shell脚本中工作?

时间:2014-02-28 13:01:41

标签: shell tcsh

我目前正在尝试用Python重写一些tcsh shell脚本。

我刚才看到的一件事是

set WORK = "/dev/shm"

if ( $?WORK ) then
    if ( $WORK == "" ) then
    set WORK = "/tmp"
    endif
else
    set WORK = "/tmp"
endif

这是做什么的?

我的第一个想法(在阅读此related question之前)是检查变量$WORK是否已设置。但这并没有多大意义,因为它之前设置了两行。此外,这似乎与“错误代码”无关。你能告诉我这部分应该做什么吗?

2 个答案:

答案 0 :(得分:0)

首先看,我会说它会检查WORK中指定的目录是否存在。如果是,则检查是否“”并将其设置为“/ tmp”(如果是)。此外,如果它不存在,则将其设置为“/ tmp”。

看起来像某种工作目录变量。工作目录可以是“”(当前目录)。

所以基本上这个脚本存在,如果/ dev / shm存在,因为用户可能应该在WORK中输入自己的目录。

让我知道你的想法。

答案 1 :(得分:0)

在任何时候都没有测试目录是否确实存在。

set WORK = "/dev/shm"           # sets the WORK var to /dev/shm

if ( $?WORK ) then              # tests if the WORK var is set
    if ( $WORK == "" ) then     # WORK is set, test if it is set to ""
        set WORK = "/tmp"       # empty WORK var is set to /tmp
    endif
else
    set WORK = "/tmp"           # WORK was not set, set to /tmp
endif

换句话说,整个" if"代码块没用。