Skip to content

Instantly share code, notes, and snippets.

@faran312
Created December 10, 2018 06:10
Show Gist options
  • Save faran312/681fe6247aea384af6ff32776a8463c7 to your computer and use it in GitHub Desktop.
Save faran312/681fe6247aea384af6ff32776a8463c7 to your computer and use it in GitHub Desktop.
Bash script cheat sheet
#! /bin/bash
# echo "Hello World"
# echo $BASH
# name=Farid
# echo "My name is $name"
# echo "Let's me know your name : "
# read name1 name2 name3
# echo "Hi, $name1, $name2, $name3"
# read -p "username: " user_var
# # using ss4 flag to hide input
# read -sp "password: " user_pass
# echo
# echo "your username is $user_var"
# echo "your password is $user_pass"
# echo "Enter names : "
# read -a names
# echo "Names : ${names[0]} ${names[1]}"
# echo "Enter names : "
# read
# echo "Names : $REPLY"
#try running this file like this ./hello.sh Farid1 2 Test
# echo "parameter1: $1, parameter2: $2, parameter3: $3"
# args=("$@")
# echo "${args[0]}, ${args[1]}, ${args[2]}"
# echo $@
# If structure on Bash Script
# if [ condition ]
# then
# statement
# fi
# count=10
# if [ $count -gt 9 ]
# then
# echo "condition is true"
# fi
#
# if (( $count > 9 ))
# then
# echo "condition is true"
# fi
#
# word="abc"
# word=a
# if [ $word == "abc" ]
# then
# echo "condition is true"
# fi
#
# if [[ $word < "b" ]]
# then
# echo "b is greater than a true"
# elif [[ $word == "a" ]]
# then
# echo "a is true"
# else
# echo "condition is false"
# fi
# get file exists
# echo -e "Enter the name of the file : \c"
# read file_name
# if [ -e $file_name ]
# then
# echo "$file_name found"
# else
# echo "$file_name not found"
# fi
#
# check if file exists and regular files
# echo -e "Enter the name of the file : \c"
# read file_name
#
# if [ -f $file_name ]
# then
# echo "$file_name found"
# else
# echo "$file_name not found"
# fi
#
# check if folder exists and its a directory
# echo -e "Enter the name of the folder : \c"
# read folder_name
#
# if [ -d $folder_name ]
# then
# echo "$folder_name found"
# else
# echo "$folder_name not found"
# fi
#
# check if file empty or not
# echo -e "Enter the name of the file : \c"
# read file_name
# if [ -s $file_name ]
# then
# echo "$file_name not empty"
# else
# echo "$file_name empty"
# fi
# echo -e "Enter the name of the file : \c"
# read file_name
# if [ -f $file_name ]
# then
# if [ -w $file_name ]
# then
# echo "Type some text data. to quit press ctrl+d."
# cat >> $file_name
# else
# echo "The file do not have permissions"
# fi
# else
# echo "$file_name not exists"
# fi
# age=25
# if [ "$age" -gt 18 ] && [ "$age" -lt 30 ]
# then
# echo "valid age"
# else
# echo "age not valid"
# fi
#
# age=25
# if [ "$age" -gt 18 -a "$age" -lt 30 ]
# then
# echo "valid age"
# else
# echo "age not valid"
# fi
#
# age=25
# if [[ "$age" -gt 18 && "$age" -lt 30 ]]
# then
# echo "valid age"
# else
# echo "age not valid"
# fi
#
# age=25
# if [[ "$age" -eq 18 || "$age" -eq 30 ]]
# then
# echo "valid age"
# else
# echo "age not valid"
# fi
#
# age=25
# if [ "$age" -eq 18 -o "$age" -eq 30 ]
# then
# echo "valid age"
# else
# echo "age not valid"
# fi
# num1=20
# num2=5
#
# echo $(( $num1 + $num2 ))
# echo $(( $num1 - $num2 ))
# echo $(( $num1 * $num2 ))
# echo $(( $num1 / $num2 ))
# echo $(( $num1 % $num2 ))
#
# num1=20
# num2=5
# echo $(expr $num1 + $num2 )
# echo $(expr $num1 - $num2 )
# echo $(expr $num1 * $num2 )
# echo $(expr $num1 / $num2 )
# echo $(expr $num1 % $num2 )
#
# num1=20
# num2=5
# echo "$num1+$num2" | bc
# echo "$num1-$num2" | bc
# echo "$num1*$num2" | bc
# echo "scale=20;$num1/$num2" | bc
# echo "$num1%$num2" | bc
#
# num3=4
# echo "scale=2;sqrt($num3)" | bc -l
# echo "scale=2;$num3^2" | bc -l
# vehicle=$1
#
# case $vehicle in
# "car" )
# echo "Rent of $vehicle is 100 dollars" ;;
# "van" )
# echo "Rent of $vehicle is 80 dollars" ;;
# "bicycle" )
# echo "Rent of $vehicle is 5 dollars" ;;
# "truck" )
# echo "Rent of $vehicle is 150 dollars" ;;
# * )
# echo "Unknown vehicle" ;;
# esac
# echo -e "Enter some character : \c"
# read value
#
# case $value in
# [a-z] )
# echo "User entered $value a to z" ;;
# [A-Z] )
# echo "User entered $value A to Z" ;;
# [0-9] )
# echo "User entered $value 0 to 9" ;;
# ? )
# echo "User entered $value special character" ;;
# * )
# echo "Unknown vehicle" ;;
# esac
# os=("ubuntu" "windows" "kali")
# os[6]=mac
# unset os[2]
# echo "${os[@]}"
# echo "${os[1]}"
# get index
# echo "${!os[@]}"
# get length
# echo "${#os[@]}"
# while loops
# while [ condition ]
# do
# command1
# command2
# command3
# done
#
# n=1
#
# while [ $n -le 10 ]
# do
# echo "$n"
# n=$(( n + 1 ))
# done
#
# while (( $n <= 10 ))
# do
# echo "$n"
# n=$(( n + 1 ))
# done
#
# while (( $n <= 10 ))
# do
# echo "$n"
# (( n++ ))
# sleep 1
# done
# content of the file will be pass to $p
# while read p
# do
# echo "$p"
# done < test.txt
#
# cat hello.sh | while read p
# do
# echo "$p"
# done
#
# -r flg is going to prevent backslashes escapes from being interpreted
# while IFS= read -r line
# do
# echo "$line"
# done < test.txt
# until loops structure
# until [ condition ]
# do
# command1
# command2
# command3
# ...
# ...
# commandN
# done
#
#n=1
#
# until [ $n -ge 10 ]
# do
# echo "$n"
# (( n++ ))
# done
#
# until (( $n >= 10 ))
# do
# echo "$n"
# (( n++ ))
# done
# for loops
# for VARIABLE in 1 2 3 4 .. N
# do
# command1
# command2
# ...
# commandN
# done
#
# OR
#
# for VARIABLE in file1 file2 file3
# do
# command1 on $VARIABLE
# command2
# ...
# commandN
# done
#
# OR
#
# for OUTPUT in $(Linux-Or-Unix-Command-Here)
# do
# command1 on $OUTPUT
# command2 on $OUTPUT
# ...
# commandN
# done
#
# OR C expression
#
# for (( exp1; exp2; exp3 ))
# do
# command1
# command2
# ...
# commandN
# done
#
# read -p "Input multiple : "
# n=()
# for VARIABLE in $REPLY
# do
# n+=('-d')
# n+=($VARIABLE)
# done
# echo ${n[@]}
#
# for i in {1..10}
# do
# echo $i
# done
#
# for i in {1..10}
# do
# echo $i
# done
#
# for (( i=0; i<5; i++ ))
# do
# echo $i
# done
# for command in ls pwd date
# do
# echo "--------$command--------"
# $command
# done
#
# for item in *
# do
# if [ -d $item ]
# then
# echo $item
# fi
# done
# select loops
# select varName in list
# do
# command1
# command2
# ...
# commandN
# done
#
# select name in Andi Budi Santi
# do
# case $name in
# Andi)
# echo "Andi selected"
# ;;
# Budi)
# echo "Budi selected"
# ;;
# Santi )
# echo "Santi selected"
# ;;
# *)
# echo "Error please provide the no. between 1..3"
# ;;
# esac
# done
# Break & Continue statement
# for (( i=1; i<10; i++ ))
# do
# if [ $i -gt 5 ]
# then
# break
# fi
# echo "$i"
# done
#
# for (( i=1; i<10; i++ ))
# do
# if [ $i -eq 5 -o $i -eq 6 ]
# then
# continue
# fi
# echo "$i"
# done
# function
# function name(){
# commands
# }
#
# name () {
# commands
# }
#
# function Hello(){
# echo "Hello"
# }
#
# quit () {
# exit
# }
#
# quit
# Hello
#
# function print(){
# echo $1 $2
# }
#
# print Hello World
# function print(){
# local name=${@}
# echo $name
# }
#
# print Hello World
# echo $name
# example use of function
# usage() {
# echo "You need to provide an argument : "
# echo "usage : $0 file_name"
# quit
# }
#
# is_file_exist() {
# local file=$1
# [[ -f "$file" ]] && return 0 || return 1
# }
#
# quit() {
# exit
# }
#
# [[ $# -eq 0 ]] && usage
# if ( is_file_exist "$1" )
# then
# echo "File found"
# else
# echo "File not found"
# fi
# Read-only command
# var=71
# readonly var
# var=50
# echo "var => $var"
# hello() {
# echo "Hello world"
# }
# readonly -f hello
# hello() {
# echo "Hello world again"
# }
# hello
# Signals and Traps
# trap "echo Exit command detected" 0
# echo "Hello"
# exit 0
#
# check signal from > man signal
# trap "echo Exit command detected; exit" 0 2 15
# echo "pid is $$"
# while (( COUNT < 10 ))
# do
# sleep 10
# (( COUNT ++ ))
# echo $COUNT
# done
# exit 0
# Debug Script
# use bash -x ./hello.sh
# or you can specify -x options in the top (ex. /bin/bash -x)
# or you can use like this
# trap "echo Exit command detected; exit" 0 2 15
# set -x
# echo "pid is $$"
# set +x
# while (( COUNT < 10 ))
# do
# sleep 10
# (( COUNT ++ ))
# echo $COUNT
# done
# exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment