Linux Shell脚本相关语法及示例

变量

赋值

  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值