Bash Scripting
Bash scripting cheatsheet¶
https://devhints.io/bash
Environment¶
check Environment¶
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
}
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
需要进一步的研究学习¶
暂无
遇到的问题¶
暂无
开题缘由、总结、反思、吐槽~~¶
参考文献¶
无