2012-05-07 65 views
1

我正在编写一个Bash脚本来设置一些环境变量,然后启动一个Web服务器。什么是一些很好的验证检查添加到脚本,以确保它是一个失败的证明尽可能:Bash脚本验证/最佳做法

1 #!/bin/bash 
    2 
    3 # Allow checking that variables don't get set to empty strings. 
    4 set -o nounset 
    5 
    6 readonly qi_redmine_base="/srv/redmine" 
    7 
    8 if test "${qi_redmine_base}" -eq "notset" 
    9 then 
10 »···echo 
11 fi 
12 
13 readonly qi_redmine_etc="/etc/redmine/default" 
14 
15 if test "${qi_redmine_etc}" -eq "notset" 
16 then 
17 »···echo 
18 fi 
19 
20 
21 readonly RAILS_ETC="${qi_redmine_etc}" 
22 export RAILS_ETC 
23 
24 readonly RAILS_LOG="${qi_redmine_base}/log" 
25 export RAILS_LOG 
26 
27 readonly RAILS_VAR="${qi_redmine_base}" 
28 export RAILS_VAR 
29 
30 readonly RAILS_CACHE="${qi_redmine_base}/tmp" 
31 export RAILS_CACHE 
32 
33 export RAILS_ENV=production 
34 export X_DEBIAN_SITEID=default 
35 
36 
37 ruby /usr/share/redmine/script/server webrick -e production 
38 if [$? -ne 0]; then 
39 echo "Server failed to start" 
40 fi 

任何帮助,不胜感激!

格雷格

+1

您可能希望从检查您引用的目录(通过您的环境变量)开始确实存在(或者创建它们,如果它们没有,并且按需要创建它们)。 –

+1

'-eq'用于数字比较。使用'='来表示字符串相等。不要在任何地方使用'readonly',你可以执行'export var = value'。缺少第40行的“fi”。 –

+1

在第38行,在'['之前和']'之后必须有空格。但你可以做'如果!红宝石......而不是使用'$?'。此外,这个问题可能更适合Code Review。 –

回答

2
help test 

示出了不是未定义的字符串一些可能的测试的机会(lenght> 0)和变量。

甚至还有一个构建体来定义的默认值:

${parameter:-word} 
      Use Default Values. If parameter is unset or null, the expansion of word is 
      substituted. Otherwise, the value of parameter is substituted. 
    ${parameter:=word} 
      Assign Default Values. If parameter is unset or null, the expansion of word is 
      assigned to parameter. The value of parameter is then substituted. Positional 
      parameters and special parameters may not be assigned to in this way. 
    ${parameter:?word} 
      Display Error if Null or Unset. If parameter is null or unset, the expansion of 
      word (or a message to that effect if word is not present) is written to the 
      standard error and the shell, if it is not interactive, exits. Otherwise, the 
      value of parameter is substituted. 
    ${parameter:+word} 
      Use Alternate Value. If parameter is null or unset, nothing is substituted, 
      otherwise the expansion of word is substituted. 

(援引man bash)。

一般来说,您需要解决暂停问题,以完成您的任务。

+0

解决暂停问题的好方法是什么? – wonbyte

+0

没有办法解决它。 –