变量
赋值
flag=1 (等式两侧均不可有空)
引用
$flag
$符相关使用
- $0 shell的命令本身(包括完整路径)
- $1到$9 数字表示shell 的第几个参数
- $# 传递到脚本的参数个数
- $* 以一个单字符串显示所有向脚本传递的参数
- $$ 脚本运行的ID号
- $! 后台运行的最后一个进程的ID号
- $@ 与$*相同
- $- 显示shell使用的当前选项。
- $? 显示最后命令的执行状况。0表示没有错误。
运算符
- -eq [等于]
- -ne [不等于]
- -lt [小于]
- -gt [大于]
- -le [小于等于]
- -ge [大于等于]
- -d [判断文件是否是目录]
- -s [判断文件是否为空,不为空返回真]
- -e [判断文件(包括目录)是否存在]
控制语句
if-else
# if后可跟判断语句也可跟命令,判断语句中括号两侧留空格
if [ $flag -ne 0 ]; then
...
else
...
fi
循环语句
while-do
while ...; do
...
done
for-do
for ...; do
...
done
示例
#!/usr/bin/env bash
#
# ------------------------------------------------------------------------------
# This script works on all the defects4j coverage information to transform them to the suspiciousness.
#
SCRIPT_DIR=$(cd `dirname $0` && pwd)
# ------------------------------------------------------------------ Envs & Args
# Check whether BLACKLIST_FILE exists
BLACKLIST_FILE="$FL_DATA_HOME/data/blacklist.csv"
[ -s "$BLACKLIST_FILE" ] || die "$BLACKLIST_FILE file does not exist or it is empty!"
# ------------------------------------------------------------------------- Main
for pid in Mockito Time; do
for bid in $(cut -f1 -d',' "$D4J_HOME/framework/projects/$pid/commit-db"); do
if grep -q "^$pid,.*,$bid," "$BLACKLIST_FILE"; then
continue
fi
pushd . > /dev/null 2>&1
cd "$pid/$bid" > /dev/null 2>&1
if [ $? -ne 0 ]; then
continue
fi
flag=0
zip_file="gzoltar-files.tar.gz"
if [ -s "$zip_file" ]; then
tar -zxf "$zip_file"
if [ $? -ne 0 ]; then
echo "[ERROR] It was not possible to extract '$zip_file', therefore a sanity-check on $pid-$bid could not be performed."
else
python3 $SCRIPT_DIR/do-ochiai --formula ochiai --matrix gzoltars/$pid/$bid/matrix --element-type Statement --element-name gzoltars/$pid/$bid/spectra --total-defn tests --output suspc.csv > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[ERROR] $pid/$bid Can't do the ochiai!"
flag=1
fi
fi
rm -r gzoltars
if [ $? -ne 0 ]; then
echo "[ERROR] $pid/$bid Can't delete the data"
fi
fi
popd > /dev/null 2>&1
if [ $flag -ne 0 ]; then
rm -r $pid/$bid
echo "[WARNING] $pid/$bid has been deleted"
continue
fi
echo "$pid-$bid has been completed"
done
done
echo "All jobs have been done!"
exit 0