2012-05-07 15 views
1

いくつかの環境変数を設定してからWebサーバーを起動するためのBashスクリプトを作成しています。いくつかの良い検証チェックが可能と証明に失敗であることを確認するためのスクリプトに追加するどのようなものです: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行目では、' ['の前に'] 'の前にスペースが必要です。しかし、あなたは 'もし! '$?'を使用する代わりに '' ruby​​ ... ''を使用します。また、この質問はCode Reviewに適しているかもしれません。 –

答えて

2
help test 

は文字列(な長さ> 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

解決する方法はありません。 –