2012-05-07 4 views
1

일부 환경 변수를 설정하고 웹 서버를 시작하기 위해 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 행에'['와']'다음에 공백이 있어야합니다. 그러나 당신은'할 수있다! 루비 ...'대신'$? '를 사용합니다. 또한이 질문은 코드 검토에 더 적합 할 수 있습니다. –

답변

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

해결 방법이 없습니다. –