跳转至

Bash Scripting

Bash scripting cheatsheet

https://devhints.io/bash

Environment

SCRIPT_DIR="$(dirname $0)"
export HOSTNAME="$(hostname)"

check Environment

if [[ -z "${ITHEMAL_HOME}" ]]; then
    echo "ITHEMAL_HOME environment variable must be set!"
    exit 1
fi

yes/no input

function container_id() {
    sudo docker ps -q --filter 'name=ithemal$'
}

CONTAINER="$(container_id)"

if [[ -z "${CONTAINER}" ]]; then
    read -p "Container is not currently running. Would you like to start it? (y/n) " -r

    if [[ !($REPLY =~ ^[Yy]) ]]; then
    echo "Not starting."
    exit 1
    fi

    # others
fi

functions

get_sudo

function get_sudo() {
    if ! sudo -S true < /dev/null 2> /dev/null; then
        echo "sudo access required for docker:"
        sudo true
    fi
}
/dev/null是一个几乎不管向它写入什么,都只返回成功,但是什么都没真的写入的文件。换句话说就是个“无底洞”,扔进去的东西肯定算扔进去了,但是扔进去就看不见了。

tmux attach

#!/usr/bin/env bash

SESSION=$(tmux ls -F '#S #{session_attached}' | grep ' 0$' | head -n 1 | awk '{$NF=""; print $0}' | awk '{$1=$1;print}')

if [ ! -z "${SESSION}" ]; then
    tmux attach -t "${SESSION}"
else
    tmux new "bash -l"
fi

argument

$? is a special variable in shell that reads the exit status of the last command executed.

$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument

需要进一步的研究学习

暂无

遇到的问题

暂无

开题缘由、总结、反思、吐槽~~

参考文献