浏览代码

handle input carefully in shell scripts

- prevent whitespace-splitting of variable expansions
- prevent interpretation of values as flags/options

(mostly)
Thirnearez 7 年前
父节点
当前提交
076cfe25d7
共有 2 个文件被更改,包括 21 次插入21 次删除
  1. 18
    18
      manage.sh
  2. 3
    3
      utils/update-translations.sh

+ 18
- 18
manage.sh 查看文件

1
 #!/bin/sh
1
 #!/bin/sh
2
 
2
 
3
-BASE_DIR=$(dirname "`readlink -f "$0"`")
4
-PYTHONPATH=$BASE_DIR
3
+BASE_DIR="$(dirname -- "`readlink -f -- "$0"`")"
4
+PYTHONPATH="$BASE_DIR"
5
 SEARX_DIR="$BASE_DIR/searx"
5
 SEARX_DIR="$BASE_DIR/searx"
6
-ACTION=$1
6
+ACTION="$1"
7
 
7
 
8
-cd "$BASE_DIR"
8
+cd -- "$BASE_DIR"
9
 
9
 
10
 update_packages() {
10
 update_packages() {
11
     pip install --upgrade pip
11
     pip install --upgrade pip
24
     set -e
24
     set -e
25
     geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
25
     geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
26
     set +e
26
     set +e
27
-    if [ -z $NOTFOUND ]; then
27
+    if [ -z "$NOTFOUND" ]; then
28
 	return
28
 	return
29
     fi
29
     fi
30
     GECKODRIVER_VERSION="v0.18.0"
30
     GECKODRIVER_VERSION="v0.18.0"
31
-    PLATFORM=`python -c "import six; import platform; six.print_(platform.system().lower(), platform.architecture()[0])"`
32
-    case $PLATFORM in
31
+    PLATFORM="`python -c "import six; import platform; six.print_(platform.system().lower(), platform.architecture()[0])"`"
32
+    case "$PLATFORM" in
33
 	"linux 32bit" | "linux2 32bit") ARCH="linux32";;
33
 	"linux 32bit" | "linux2 32bit") ARCH="linux32";;
34
 	"linux 64bit" | "linux2 64bit") ARCH="linux64";;
34
 	"linux 64bit" | "linux2 64bit") ARCH="linux64";;
35
 	"windows 32 bit") ARCH="win32";;
35
 	"windows 32 bit") ARCH="win32";;
47
 	fi
47
 	fi
48
     else
48
     else
49
 	GECKODRIVER_DIR="$1"
49
 	GECKODRIVER_DIR="$1"
50
-	mkdir -p "$GECKODRIVER_DIR"
50
+	mkdir -p -- "$GECKODRIVER_DIR"
51
     fi
51
     fi
52
 
52
 
53
     echo "Installing $GECKODRIVER_DIR/geckodriver from\n  $GECKODRIVER_URL"
53
     echo "Installing $GECKODRIVER_DIR/geckodriver from\n  $GECKODRIVER_URL"
54
     
54
     
55
-    FILE=`mktemp`
56
-    wget "$GECKODRIVER_URL" -qO $FILE && tar xz -C "$GECKODRIVER_DIR" -f $FILE geckodriver
57
-    rm $FILE
58
-    chmod 777 "$GECKODRIVER_DIR/geckodriver"
55
+    FILE="`mktemp`"
56
+    wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "$GECKODRIVER_DIR" -f "$FILE" geckodriver
57
+    rm -- "$FILE"
58
+    chmod 777 -- "$GECKODRIVER_DIR/geckodriver"
59
 }
59
 }
60
 
60
 
61
 pep8_check() {
61
 pep8_check() {
73
 
73
 
74
 py_test_coverage() {
74
 py_test_coverage() {
75
     echo '[!] Running python test coverage'
75
     echo '[!] Running python test coverage'
76
-    PYTHONPATH=`pwd` python -m nose2 -C --log-capture --with-coverage --coverage "$SEARX_DIR" -s "$BASE_DIR/tests/unit" \
76
+    PYTHONPATH="`pwd`" python -m nose2 -C --log-capture --with-coverage --coverage "$SEARX_DIR" -s "$BASE_DIR/tests/unit" \
77
     && coverage report \
77
     && coverage report \
78
     && coverage html
78
     && coverage html
79
 }
79
 }
80
 
80
 
81
 robot_tests() {
81
 robot_tests() {
82
     echo '[!] Running robot tests'
82
     echo '[!] Running robot tests'
83
-    PYTHONPATH=`pwd` python "$SEARX_DIR/testing.py" robot
83
+    PYTHONPATH="`pwd`" python "$SEARX_DIR/testing.py" robot
84
 }
84
 }
85
 
85
 
86
 tests() {
86
 tests() {
113
 
113
 
114
 npm_packages() {
114
 npm_packages() {
115
     echo '[!] install NPM packages for oscar theme'
115
     echo '[!] install NPM packages for oscar theme'
116
-    cd $BASE_DIR/searx/static/themes/oscar
116
+    cd -- "$BASE_DIR/searx/static/themes/oscar"
117
     npm install
117
     npm install
118
 
118
 
119
     echo '[!] install NPM packages for simple theme'    
119
     echo '[!] install NPM packages for simple theme'    
120
-    cd $BASE_DIR/searx/static/themes/simple
120
+    cd -- "$BASE_DIR/searx/static/themes/simple"
121
     npm install
121
     npm install
122
 }
122
 }
123
 
123
 
133
 }
133
 }
134
 
134
 
135
 help() {
135
 help() {
136
-    [ -z "$1" ] || printf "Error: $1\n"
136
+    [ -z "$1" ] || printf 'Error: %s\n' "$1"
137
     echo "Searx manage.sh help
137
     echo "Searx manage.sh help
138
 
138
 
139
 Commands
139
 Commands
156
 
156
 
157
 [ "$(command -V "$ACTION" | grep ' function$')" = "" ] \
157
 [ "$(command -V "$ACTION" | grep ' function$')" = "" ] \
158
     && help "action not found" \
158
     && help "action not found" \
159
-    || $ACTION "$2"
159
+    || "$ACTION" "$2"

+ 3
- 3
utils/update-translations.sh 查看文件

7
 
7
 
8
 SEARX_DIR='searx'
8
 SEARX_DIR='searx'
9
 
9
 
10
-pybabel extract -F babel.cfg -o messages.pot $SEARX_DIR
11
-for f in `ls $SEARX_DIR'/translations/'`; do
12
-    pybabel update -N -i messages.pot -d $SEARX_DIR'/translations/' -l $f
10
+pybabel extract -F babel.cfg -o messages.pot "$SEARX_DIR"
11
+for f in `ls "$SEARX_DIR"'/translations/'`; do
12
+    pybabel update -N -i messages.pot -d "$SEARX_DIR"'/translations/' -l "$f"
13
 done
13
 done
14
 
14
 
15
 echo '[!] update done, edit .po files if required and run pybabel compile -d searx/translations/'
15
 echo '[!] update done, edit .po files if required and run pybabel compile -d searx/translations/'