diff --git a/README.md b/README.md index 193e55a..8de8ee3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ 1.5. [Process Monitoring Operations](#15-process-monitoring-operations) 2. [Basic Shell Programming](#2-basic-shell-programming) 2.1. [Variables](#21-variables) - 2.2 [Array](#22-array) + 2.2. [Array](#22-array) 2.3. [String Substitution](#23-string-substitution) 2.4. [Functions](#24-functions) 2.5. [Conditionals](#25-conditionals) @@ -808,7 +808,7 @@ man command ``` ### l. `passwd` -Allows the current logged user to change his password. +Allows the current logged user to change their password. ### m. `ping` Pings host and outputs results. @@ -821,6 +821,10 @@ Lists your processes. ```bash ps -u yourusername ``` +Use the flags ef. e for every process and f for full listing. +```bash +ps -ef +``` ### o. `quota` Shows what your disk quota is. @@ -854,6 +858,44 @@ This command also accepts an option `-p` that can be used to connect to specific ```bash ssh -p port user@host ``` +For easy access: +```bash +nano ~/.ssh/config +``` +The config file is organized by hosts. Each host definition can define connection options for the specific matching host. Wildcards are also available to allow for options that should have a broader scope. + + +```bash +Host firsthost + SSH_OPTION_1 custom_value + SSH_OPTION_2 custom_value + SSH_OPTION_3 custom_value + +Host secondhost + ANOTHER_OPTION custom_value + +Host *host + ANOTHER_OPTION custom_value + +Host * + CHANGE_DEFAULT custom_value +``` +However, we could also use the full option names with the -o flag, like this: + +```bash +ssh -o "User=custom_value" -o "Port=custom_value" -o "HostName=custom_value" anything +``` +#### q1. `SOCKS5` +```bash +ssh -D port -f -C -q -N user@host +``` +#### Explanation of arguments: +- -D: Tells SSH that we want a SOCKS tunnel on the specified port number (you can choose a number between 1025-65536) +- -f: Forks the process to the background +- -C: Compresses the data before sending it +- -q: Uses quiet mode +- -N: Tells SSH that no command will be sent once the tunnel is up + ### r. `top` Displays your currently active processes. @@ -930,7 +972,7 @@ nohup command & The first line that you will write in bash script files is called `shebang`. This line in any script determines the script's ability to be executed like a standalone executable without typing sh, bash, python, php etc beforehand in the terminal. ```bash -#!/bin/bash +#!/usr/bin/env bash ``` ## 2.1. Variables @@ -1024,7 +1066,7 @@ When you run the above example the `hello` function will output "world!". The ab The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is `if` expression `then` statement where statement is only executed if expression is true. ```bash -if [expression]; then +if [ expression ]; then will execute only if expression is true else will execute if expression is false @@ -1080,7 +1122,6 @@ file1 -ot file2 # file1 is older than file2 ``` ## 2.6. Loops - There are three types of loops in bash. `for`, `while` and `until`. Different `for` Syntax: @@ -1142,6 +1183,41 @@ function finish { trap finish EXIT ``` +## Extract +Make your life easier. +```bash +function extract() # Handy Extract Program +{ + if [ -f $1 ] ; then + case $1 in + *.tar.bz2) tar xvjf $1 ;; + *.tar.gz) tar xvzf $1 ;; + *.bz2) bunzip2 $1 ;; + *.rar) unrar x $1 ;; + *.gz) gunzip $1 ;; + *.tar) tar xvf $1 ;; + *.tbz2) tar xvjf $1 ;; + *.tgz) tar xvzf $1 ;; + *.zip) unzip $1 ;; + *.Z) uncompress $1 ;; + *.7z) 7z x $1 ;; + *) echo "'$1' cannot be extracted via >extract<" ;; + esac + else + echo "'$1' is not a valid file!" + fi +} +``` +## Exit +```bash +function _exit() # Function to run upon exit of shell. +{ + echo -e "${BRed}Hasta la vista, cabron ${NC}" +} +trap _exit EXIT + +``` + ## Saving your environment variables When you do `export FOO = BAR`, your variable is only exported in this current shell and all its children, to persist in the future you can simply append in your `~/.bash_profile` file the command to export your variable @@ -1160,6 +1236,13 @@ If you can not access, try append the code below in your `~/.bash_profile` file PATH="$HOME/bin:$PATH" fi ``` +## Aliases +Put this in your ~/.bashrc file to include all your aliases in a neatly organized file: +```bash +if [ -f ~/.bash_aliases ]; then + source ~/.bash_aliases +fi +``` # 4. Debugging You can easily debug the bash script by passing different options to `bash` command. For example `-n` will not run commands and check for syntax errors only. `-v` echo commands before running them. `-x` echo commands after command-line processing. @@ -1177,6 +1260,7 @@ bash -x scriptname - Spread the word ## Translation +- [Chinese | 简体中文](https://github.com/vuuihc/bash-guide) - [Turkish | Türkçe](https://github.com/omergulen/bash-guide) - [Japanese | 日本語](https://github.com/itooww/bash-guide)