#Check if a program exists command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; } #Block Comment in bash : ' This is a test comment Author foo bar Released under GNU ' #Unicode escape in bash =$'\xe2\x98\xa2 ' # if-else if [ "foo" = "foo" ]; then echo expression evaluated as true else echo expression evaluated as false fi # Grep only Numeric values: echo "99%" |grep -o '[0-9]*' # Bash Command output to a variable OUTPUT="$(ls -1)" echo "${OUTPUT}" # SED w variable and line replace sed -i.bak 's/.*TEXT_TO_BE_REPLACED.*/"'$VARIABLE'"/' VagrantFile >/dev/null 2>&1 # While loop i="0" while [ $i -lt 4 ] do i=$[$i+1] done # Redirect Command output command 2> /dev/null #Detect OS platform='unknown' unamestr=`uname` if [[ "$unamestr" == 'Linux' ]]; then platform='linux' elif [[ "$unamestr" == 'FreeBSD' ]]; then platform='freebsd' fi # Execute a Gist in bash #http://max.disposia.org/notes/bash-exec-gist.htm bash -c "$(curl -fsSL $raw_gist_path)" $arg0 $arg1 # Append multiple lines to a file cat <> greetings.txt line 1 line 2 EOT # Change Password using script echo "USERNAME:NEWPASSWORD" | chpasswd # Generate SSH key using script echo -e "\n\n\n" | ssh-keygen -t rsa # SSH Key using script sudo apt-get install sshpass sshpass -p "PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no USERNAME@IP # Process spawn and wait /my/process & /another/process & wait echo "All processes done!" # Alert in bash echo -e "\a" # Create offline Installation sudo apt-get download PACKAGE_NAME && apt-cache depends -i PACKAGE_NAME | awk '/Depends:/ {print $2}' | xargs apt-get download # Arrays in Bash ARRAY=(one two three) echo ${ARRAY[*]} # Print all echo ${ARRAY[2]} # Print 3rd element # For loop for VARIABLE in 1 2 3 4 5 .. N do command1 command2 commandN done # Iterating through each directory for f in *; do if [ -d ${f} ]; then # Will not run if no directories are available echo $f fi done # Iterating through an array with index for i in "${!foo[@]}"; do printf "%s\t%s\n" "$i" "${foo[$i]}" done # Single line condition checker if ps aux | grep some_proces[s] > /tmp/test.txt; then echo 1; else echo 0; fi # Array Declaration array=(1 2 3) # Print 1st Element echo ${array[1]} # Print all elements echo ${array[@]} # Get length of an array echo ${#VAR_NAME[@]} #Split Name IN="bla@some.com;john@home.com" arrIN=(${IN//;/ }) # Silent execution silent() { "$@" > /dev/null 2>&1 } silent echo "Hello World" #Switch Case case "$C" in "1") do_this() ;; "2" | "3") do_what_you_are_supposed_to_do() ;; *) do_nothing() ;; esac # Adding multiple lines to a file cat <> FILENAME export KUBE_VERSION=1.2.0 export FLANNEL_VERSION=0.5.0 export ETCD_VERSION=2.2.0 export nodes="vagrant@10.0.168.10 vagrant@10.0.168.11 vagrant@10.0.168.11" export roles="ai i i" export NUM_NODES=${NUM_NODES:-3} export SERVICE_CLUSTER_IP_RANGE=192.168.3.0/24 export FLANNEL_NET=172.16.0.0/16 EOT # Grep select only numbers echo "99%" |grep -o '[0-9]*' # Create a sample from files in a folder mkdir -p sample for f in *.csv; do head -101 $f >> sample/$f; done # Kill app based on port number PID=`lsof -i:8121 -t` echo "Stopping previous build if any . . ." && kill -9 ${PID} # Set Locale export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 # Convert multiline bash output to an array var=($(./inner.sh)) # Grep IP Address using regex echo "$str_containing_ip_addrs" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" # Wait for previous process while executing another command launch backgroundprocess & PROC_ID=$! while kill -0 "$PROC_ID" >/dev/null 2>&1; do echo "PROCESS IS RUNNING" done echo "PROCESS TERMINATED" exit 0 # To log every command before execution set -o xtrace # to revert to normal - set +o xtrace # or bash -x myscript.sh #list process on port lsof -i tcp: #Select random lines from a file shuf -n N input > output # Generate a random number between 1-10 $(( ( RANDOM % 10 ) + 1 ))