1
linux 3.5.6-1.fc17.x86_64
GNU bash, version 4.2.37
我有以下bash腳本文件。我正在使用它來測試我的程序。但是,我希望能夠測試使用和不使用valgrind。所以我從命令行'valgrind'得到參數並在if語句中進行比較。然而,隨着時間的推移,我的測試用例會變得更大,我將不得不在if-else中重複大量代碼。有沒有辦法將這個if-else最小化?
非常感謝您的任何建議,
#!/bin/bash
# Abort on error
set -e
echo $1
if [ "$1" == "valgrind" ]; then
echo "TEST STARTED RUNNING WITH VALGRIND"
echo "TEST CREATE DATABASE"
valgrind ./db db.dat c
echo "TEST CREATE DATABASE OK"
echo "TEST INSERT"
valgrind ./db db.dat s 1 steve [email protected]
valgrind ./db db.dat s 2 ben [email protected]
valgrind ./db db.dat s 3 lisa [email protected]
echo "TEST INSERT OK"
echo "TEST LIST"
valgrind ./db db.dat l
echo "TEST LIST OK"
echo "TEST DELETE"
valgrind ./db db.dat d 2
echo "TEST DELETE OK"
echo "TEST LIST"
valgrind ./db db.dat l
echo "TEST LIST OK"
echo "TEST FETCH"
valgrind ./db db.dat g 2
valgrind ./db db.dat g 3
echo "TEST FETCH OK"
echo "TEST COMPLETED OK"
else
echo "TEST STARTED"
echo "TEST CREATE DATABASE"
./db db.dat c
echo "TEST CREATE DATABASE OK"
echo "TEST INSERT"
./db db.dat s 1 steve [email protected]
./db db.dat s 2 ben [email protected]
./db db.dat s 3 lisa [email protected]
echo "TEST INSERT OK"
echo "TEST LIST"
./db db.dat l
echo "TEST LIST OK"
echo "TEST DELETE"
./db db.dat d 2
echo "TEST DELETE OK"
echo "TEST LIST"
./db db.dat l
echo "TEST LIST OK"
echo "TEST FETCH"
./db db.dat g 2
./db db.dat g 3
echo "TEST FETCH OK"
echo "TEST COMPLETED OK"
fi
或者,你可以拿起VALGRIND'的'從調用環境中的值。將初始化保留在腳本之外,當您想要使用'valgrind':'VALGRIND = valgrind test.sh'時將其初始化。 – chepner