bash if else声明

时间:2012-02-17 15:05:50

标签: bash if-statement

这很简单,但我无法让它发挥作用。我想在ssh bash脚本中做一个简单的if else语句。这是我的代码:

#!/bin/sh

echo -n "enter what type of logs. (ie. cpanel, apache) "
read type
if [$type == cpanel]
then
LOGTYPES ="error, access"
else
LOGTYPES ="error, access, suphp, suexec"
fi

echo -n "enter which log to tail (ie. $LOGTYPES) "
read logs
echo -n "enter last number of lines to view. (ie. 10, 5000 etc) "
read lines

tail -$lines /usr/local/$type\/logs/$logs\_log

我已尝试过所有内容,但我一直收到此错误:

[~]# /apachetail
enter what type of logs. (ie. cpanel, apache) cpanel
/apachetail: line 5: [cpanel: command not found
/apachetail: line 9: LOGTYPES: command not found
enter which log to tail (ie. )

我尝试了以下方法,没有工作:

    if [$type -eq cpanel]
    if [$type -eq cpanel];
    if [$type == cpanel];
    if [$type = cpanel]
    if [$type = cpanel];
    if ["$type" = cpanel]
    if ["$type" = cpanel];
    if ["$type" == cpanel]
    if ["$type" == cpanel];
    if ["$type" -eq cpanel];
    if ["$type" -eq cpanel]

2 个答案:

答案 0 :(得分:9)

更改

if [$type == cpanel]

if [ "$type" = "cpanel" ]

   LOGTYPES ="error, access"

   LOGTYPES="error, access"

任何赋值必须在=(赋值运算符)周围没有空格。

答案 1 :(得分:3)

LOGTYPES =应为LOGTYPES= - 请注意缺少的空格。 [$type == cpanel]应为[ $type == cpanel ] - 请注意添加的空间。