跳到正文

个人记录

Linux Shell 使用技巧

Talaodi约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年6月16日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
  1. 定义函数
代码
run() {
    g++ $1.cpp -o $1 -O2 -Wall -std=c++14;
    if [ $? == 0 ]; then
        time ./$1;
    fi
}

在终端输入以上内容即可。

之后可以使用 run + 文件名运行程序。

比用 C++ 实现方便很多。

  1. ~/.bashrc

这个文件是每次新建终端会执行一遍的代码。

在命令行用一个编辑器打开(比如 VSCode 就是 code ~/.bashrc)。

加入上述函数代码。那么每次新建终端就都可以用了。

加入 ulimit -s 512000,即可在每次新建终端的时候自动开栈。

  1. 其他函数示例
Bash
runf() {
    g++ $1.cpp -o $1 -O2 -Wall -std=c++14 -fsanitize=address,undefined;
    if [ $? == 0 ]; then
        time ./$1;
    fi
}

使用 -fsanitize。

Bash
ckd() {
    data=$1;
    std=$2;
    mine=$3;
    file=$4;
    for ((i=1;;i++)) do
        ./$data 2> /dev/null; if [ $? != 0 ]; then echo "generator failed on test $i."; break; fi
        ./$std 2> /dev/null; if [ $? != 0 ]; then echo "std failed on test $i."; break; fi
        ./$mine 2> /dev/null; if [ $? != 0 ]; then echo "runtime error on test $i."; break; fi
        diff $file.out $file.ans; if [ $? != 0 ]; then echo "wrong answer on test $i."; break; fi
        echo "accepted on test $i.";
    done
}

对拍。2> /dev/null 用于屏蔽 stderr 输出。

Bash
test_sample() {
    l=$1; # 编号下界
    r=$2; # 编号上界
    code=$3;
    file=$4; # 样例文件前缀
    suf=$5; # 答案文件后缀
    for ((i=$l;i<=$r;i++)) do
        cp $file$i.in $code.in # 复制到 .in 文件
        ./$code; if [ $? != 0 ]; then echo "runtime error on sample $i."; continue; fi
        diff $code.out $file$i.$suf -Z; if [ $? != 0 ]; then echo "wrong answer on sample $i."; continue; fi
        echo "accepted on sample $i.";
    done
}

测试所有样例。

例:test_sample 1 3 cactus ex_cactus out。

原文
存档评论1 条抓取于