Shell常用的提效工具,脚本

 
Category: Linux-Shell

写在前面

alias 篇

export LANG="zh_CN.utf8"
export LC_CTYPE="zh_CN.UTF-8"
export MANPATH=/usr/local/share/man:$HOME/local/share/man:$MANPATH
ulimit -c unlimited


alias vi=vim
alias vimdiff='vimdiff -c "windo set wrap" '
alias vb='vi ~/.bashrc'
alias vt='vi ~/code/tool/linux_tool.sh'
alias sb='source ~/.bashrc'
alias vv="vi ~/.vimrc"
alias ll="ls -alh --color=auto"
alias ah="ag -Gh "

alias gb='git branch'
alias gt='git status'
alias gd='git diff'
alias gds='git diff --stat'
### for git log Chinese error
export LESSCHARSET=utf-8


alias py=python3
alias ipy=ipython
alias grep="grep --color"

复制粘贴篇

#!/bin/bash
copy() {
    # 如果是交互式终端(没有重定向输入),提示用法
    if [ -t 0 ]; then
        echo "Usage: cat file | copy_clipboard"
        return 1
    fi

    local content
    content=$(cat)

    # base64 编码,适配 Linux/macOS
    local encoded
    if base64 --help 2>&1 | grep -q -- "-w"; then
        encoded=$(echo -n "$content" | base64 -w 0) # Linux
    else
        encoded=$(echo -n "$content" | base64) # macOS 无 -w
    fi

    # 发送 OSC 52 复制命令
    echo -ne "\e]52;c;${encoded}\a"
}

scppath() {
    local target="$1"
    if [ -z "$target" ]; then
        target="."
    fi
    local abs_path
    abs_path=$(readlink -f "$target") || return
    echo "$(whoami)@$(hostname):$abs_path" | tee >(copy)
}

pcml() {
    if [ $# != 1 ]; then
        echo "usage: gen_pcmlst <directory of audio collection>" && return 1
    fi

    directory=$1

    if [[ $directory =~ /$ ]]; then
        directory="${directory%/}"
    fi

    res=$(find "$directory" -type f | awk '{print $0":ref"}')

    for line in $res; do
        echo $line >>$directory/pcm.list
    done
}

cmd() {
    if (($# != 1)); then
        echo "get cmdline by pid"
        echo "Usage: $0 <pid>"
        return 1
    fi
    echo $(cat /proc/$1/cmdline | tr '\0' ' ')
}

xx() {
    if [[ -z "$1" ]]; then
        echo "Usage: $0 <filename.cpp|filename.cc>"
        return 1
    fi
    local file_ext="${1##*.}"
    local CXX_bin=/opt/compiler/gcc-12/bin/g++

    if [[ "$file_ext" != "cpp" && "$file_ext" != "cc" && "$file_ext" != "cxx" ]]; then
        echo "Error: Unsupported file type '$file_ext'. Supported: .cpp, .cc, .cxx"
        return 1
    fi
    ${CXX_bin} $2 -g -O2 -std=c++20 -lpthread "$1" -o "${1%.*}.out" && ./"${1%.*}.out"
}

## for  git tools
gc() {
    git checkout "$@"
}
gp() {
    git push origin $1:refs/for/$1
}

其他

fst 相关

##### for fst operator #####
fstshow() {
    if (($# != 3)); then
        echo -e "Draw a fst.\nUsage: $0 in_sym out_sym file.fst"
        return
    fi
    local isym=$1
    local osym=$2
    local fst_file=$3
    fstdraw --isymbols=${isym} --osymbols=${osym} ${fst_file} >${fst_file}.dot
    dot -Tjpg -Gdpi=2000 ${fst_file}.dot -o ${fst_file}.jpg
    ##open ${fst_file}.jpg ## just for macos
}

fst() {
    if (($# != 3)); then
        echo -e "Compile fst, draw and open jpg.\nUsage: $0 in_sym out_sym file.txt"
        return
    fi
    local isym=$1
    local osym=$2
    local fst_txt=$3
    fstcompile --isymbols=${isym} --osymbols=${osym} ${fst_txt} >${fst_txt}.fst
    fstdraw --isymbols=${isym} --osymbols=${osym} ${fst_txt}.fst >${fst_txt}.dot
    dot -Tjpg -Gdpi=2000 ${fst_txt}.dot -o ${fst_txt}.jpg
    ##open ${fst_txt}.jpg
}