# Below is all the code to do certain things # Note that placeholders are used here and this is a guide! Do NOT use this as a script (it will not work)! # Note also that this is a guide for Bash, so ONLY for Linux (maybe also for MacOS, also based on GNU like Linux) # Basics ## Restart system reboot ## Login as user su userName ### Run code as other user sudo -u userName code_to_execute ## Remove file/directory rm file rm directory ### If the directory not empty rm -r directory ## Copy cp fromHere to ### Copy directory with their subdirectorys and files cp -r fromHere to ## Download files, archives, binarys etc wget url/file.name wget url/archive.name ## Run code as process code_to_execute & # Advanced ## Declare variables variable=example ### Declare variables with code to be executed variable=$(code_to_execute) ## Fetch variable values $variable ### If you want print a value of a variable in a file ${variable} ### For terminal declared variables (rather known as arguments) $1 $2 $3 ## Dedect OS uname ## Dedect kernel uname -r ## Dedect hostname uname -n ## Dedect Processor uname -p ## Dedect Architecture uname -m ## Math ### Greater than if [ 10 -gt 5 ]; then echo "The number is greater than 5" fi ### Smaller than if [ 5 -lt 7 ]; then echo "The number is less than 7" fi ### Equal if [ 5 -eq 5 ]; then echo "The number is equal to 5" fi ## Check for permissions and ownerships ls -lha ## Fetch configured timezone of the system cat /etc/timezone ## Write paragraph content in a file cat << EOF > file.txt ## Content EOF ## Checks the existence of a PID if ( kill -0 PID ); then # Code to execute fi ## Checks for the existence of a specific file if [ -f fileName ]; then # Code to execute fi ## Checks for the existence of a specific directory if [ -d directory ]; then # Code to execute fi ## Checks, if a directory is writable if [ -w directory]; then # Code to execute fi ## Checks, if a file is executeable if [ -x file]; then # Code to execute fi ### You can use this to merge in a one line [ ! -d directory ] && mkdir directory ### To check whether a directory is empty if [[ -z "$(find /pfad/zum/verzeichnis -mindepth 1)" ]]; then echo "The directory is empty" else echo "The directory is not empty" fi ## Use functions (handy when you want to use a whole chain of code multiple times, or simply for the overview) ### Create functions myfunction() { # Code_to_execute } ### Call functions myfunction ## Give the id of the latest runned process echo $! ### Save the id of the latest runned process echo $! > process.pid ## Replace PID journalctl _PID=PID ## Get all user given arguments echo $@ ### Fetch, if a package exists checkPackages() { for cmd in "$@" ; do if command -v "$cmd" ; then $cmd --version return fi done echo "Not found!" } checkPackages thePackage ## Deleting pushed commits in git ### Starting interactive mode git rebase -i ### Delete commit locally git reset HEAD~ ### Delete commit globally (in the repository) git push origin main --force --tags # Experts ## Return the first executing process ps -p 1 -o comm= ### Check, is used systemd or SysV-Init if [ "$(ps -p 1 -o comm=)" == "systemd" ]; then systemctl enable app systemctl start app else service app enable service app start fi ## Set memory swap fallocate -l 4G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile ## Let's print "!" 10 times c=1 while [[ "$c" -le 10 ]]; do echo -n "!" sleep 1 c=$(($c+1)) done ## Using getops while getopts ":a:b:c:d:" opt; do case ${opt} in a) a=$OPTARG ;; b) b=$OPTARG ;; c) c=$OPTARG ;; d) d=$OPTARG ;; *) echo "Option -$OPTARG requires an argument." 1>&2 exit 1 ;; esac done shift $((OPTIND -1)) echo $a echo $b echo $c echo $d ## Using while with shift while (( "$#" )); do case "$1" in -h | --host) host="$2" shift 2 ;; -p | --port) port="$2" shift 2 ;; -u | --username) username="$2" shift 2 ;; -pass | --password) password="$2" shift 2 ;; *) echo "Unknown parameter passed: $1" exit 1 esac done echo "Host: $host" echo "Port: $port" echo "Username: $username" echo "Password: $password" ## If this error comes: ## Unable to monitor directories for changes because iNotify max watches exceeded, add this to the end of the /etc/sysctl.conf file: fs.inotify.max_user_watches=99999 ### Then run: sudo sysctl -p cat /proc/sys/fs/inotify/max_user_watches ## Generate random strings (also usable as passwords) tr -dc A-Za-z0-9_ < /dev/urandom | head -c 32 | xargs ## Display the IP of the mashine (or computer) ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -1 ## Delete everything in the file file.txt after a : grep -E ".*:.*" file.txt | sed -i 's/:[^:]*//g' file.txt ## Replace test with test2 in example.txt sed -i "s|test|test2|" example.txt ## Check for memory (in KiloByte) awk '/MemTotal/ {print $2}' /proc/meminfo ## Format your echo texts coloured echo -e "\e[31mThis text is red\e[0m" ### All colors #### Black: \e[30m #### Red: \e[31m #### Green: \e[32m #### Yellow: \e[33m #### Blue: \e[34m #### Magenta: \e[35m #### Cyan: \e[36m #### White: \e[37m ### Or getColoured() { tput setaf 1 echo "$@" tput sgr0 } getColoured test #### All colors (Put after "tput setaf") ##### Light red: 1 ##### Green: 2 ##### Yellow: 3 ##### Blue: 4 ##### Purple: 5 ##### Light blue: 6 ##### White: 7 ##### Grey: 8 ##### Red: 9 ## Running code based on selected option options=( "Option 1" "Option 2" # ... ) select opt in "${options[@]}" do case $opt in "Option 1") # Code for option 1 ;; "Option 2") # Code for option 2 ;; # ... esac done