Guangning Yu's Blog
Home
Code
Data
Setup
Industry
MachineLearning
Archive
Bash Basics
2019-02-17 01:40:29
|
shell
- assign default value to variable ``` DEFAULT=5 RESULT=${VAR:-$DEFAULT} ``` - trap error exit ``` function rollback() { if [[ $? -ne 0 ]]; then echo "rollback" fi } set -ueo pipefail trap rollback EXIT ``` - if file exists ``` #!/bin/bash FILE="/etc/passwd" if [[ -f $FILE ]];then echo "$FILE exists" else echo "$FILE doesn't exist" fi ``` - if directory exists ``` #!/bin/bash DIR="/var/log" if [[ -d $DIR ]]; then echo "$DIR exists" else echo "$DIR doesn't exist" fi ``` - eval ``` function assert_var_exists() { local condition="[[ ! -n \"\${$1+set}\" ]]" if eval $condition; then echo "The variable ${1} does not exist. Exit." exit 1 fi } function export_var() { assert_var_exists $1 local var_value=$(($1)) eval "export $1=\$var_value" echo "$1=$var_value" } ``` - Check platform ``` uname -m lsb_release ```
Previous:
Setup Nginx on Ubuntu 14.04
Next:
Git Basics