1

我正在学习 shell 脚本,我发现 shell 脚本条件语句有不同的语法。csh 脚本的语法是否与 tcsh 脚本不同。有人说

 if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
echo "You have access!"
 else
echo "ACCESS DENIED!"
  fi

一些使用

 if ($PASSWORD == $VALID_PASSWORD)
 echo "You have access!"
 else
echo "ACCESS DENIED!"
 endif

我尝试了自己的方法,但得到了诸如“if: Empty if”或“If: Expression Syntax”之类的错误,这些消息非常简单易懂。所以就想问问怎么解决这些问题,有没有基于shell(csh,tcsh)不同的解决方案

如果我的 shell 是 tcsh,我应该始终使用 tcsh 脚本还是可以编写 bash 脚本。

4

2 回答 2

4

cshtcshif语句共享相同的语法:

if (expr) then
...
else if (expr2) then
...
else
...
endif

您的另一个示例(可能)来自shor bash

if list; then list; [ elif list; then list; ] ... [ else list; ] fi

您的列表恰好是程序的调用(或有时是 shell 内置)[

你可以为任何你想要的shell编写脚本,只要你把正确的一个

#!/bin/sh
#!/bin/bash
#!/bin/csh
#!/bin/tcsh

在文件顶部匹配脚本的预期外壳。

于 2013-06-21T17:54:13.023 回答
0

这是实际的脚本 -

#!/bin/sh
# This is some secure program that uses security.

VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
    echo "You have access!"
else
    echo "ACCESS DENIED!"
fi
于 2014-04-16T14:35:02.373 回答