Posts

Showing posts from March, 2017

Shell Scripting Tutorial for Beginners 14 - Array variables

Image
#! /bin/bash os=('ubuntu' 'windows' 'kali') os[6]='mac' unset os[2] echo "${os[@]}" echo "${os[0]}" echo "${!os[@]}" echo "${#os[@]}" string=dasfdsafsadfasdf echo "${string[@]}" echo "${string[0]}" echo "${string[1]}" echo "${#string[@]}"

Shell Scripting Tutorial for Beginners 13 - The case statement Example

Image
#! /bin/bash 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 input" ;; esac Output: test@test$ ./hello.sh Enter some character : f User entered f a to z test@test$ ./hello.sh Enter some character : K User entered K a to z test@test$ LANG = C test@test$ ./hello.sh Enter some character : K User entered K A to Z test@test$ ./hello.sh Enter some character : 9 User entered 9 0 to 9 test@test$ ./hello.sh Enter some character : 5 User entered 5 0 to 9 test@test$ ./hello.sh Enter some character : & User entered & special character test@test$ ./hello.sh Enter some character : sdsdsdsd Unknown input test@test$

Shell Scripting Tutorial for Beginners 12 - The case statement

Image
#! /bin/bash vehicle=$1 case $vehicle in "car" ) echo "Rent of $vehicle is 100 dollar" ;; "van" ) echo "Rent of $vehicle is 80 dollar" ;; "bicycle" ) echo "Rent of $vehicle is 5 dollar" ;; "truck" ) echo "Rent of $vehicle is 150 dollar" ;; * ) echo "Unknown vehicle" ;; esac

Shell Scripting Tutorial for Beginners 11 - Floating point math operatio...

Image
#! /bin/bash num1=20.5 num2=5 echo "$num1+$num2" | bc echo "$num1-$num2" | bc echo "20.5*5" | bc echo "scale=20;20.5/5" | bc echo "20.5%5" | bc num=4 echo "scale=2;sqrt($num)" | bc -l echo "scale=2;3^3" | bc -l

Shell Scripting Tutorial for Beginners 10 - Perform arithmetic operations

Image
#! /bin/bash num1=20 num2=5 echo $(( num1 + num2 )) echo $(( num1 - num2 )) echo $(( num1 * num2 )) echo $(( num1 / num2 )) echo $(( num1 % num2 )) echo $(expr $num1 + $num2 ) echo $(expr $num1 - $num2 ) echo $(expr $num1 \* $num2 ) echo $(expr $num1 / $num2 ) echo $(expr $num1 % $num2 )

Shell Scripting Tutorial for Beginners 8 - Logical 'OR' Operator

Image
#! /bin/bash age=60 # for using OR operator use || if [ "$age" -gt 18] || ["$age" -lt 30 ] then echo "valid age" else echo "age not valid" fi # The -o option provide # an alternative compound condition test. if [ "$age" -gt 18 -o "$age" -lt 30 ] then echo "valid age" else echo "age not valid" fi # if [[ $condition1 || $condition2 ]] # Also works. if [[ "$age" -gt 18 || "$age" -lt 30 ]] then echo "valid age" else echo "age not valid" fi

Shell Scripting Tutorial for Beginners 8 - Logical 'AND' Operator

Image
#! /bin/bash age=60 # for using And operator use && if [ "$age" -gt 18] && ["$age" -lt 30 ] then echo "valid age" else echo "age not valid" fi # The -a option provide # an alternative compound condition test. if [ "$age" -gt 18 -a "$age" -lt 30 ] then echo "valid age" else echo "age not valid" fi # if [[ $condition1 && $condition2 ]] # Also works. if [[ "$age" -gt 18 && "$age" -lt 30 ]] then echo "valid age" else echo "age not valid" fi

Shell Scripting Tutorial for Beginners 7 - How to append output to the e...

Image
#! /bin/bash 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 write permissions" fi else echo "$file_name not exists" fi

Shell Scripting Tutorial for Beginners 6 - File test operators

Image
#! /bin/bash 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

How to Download and Install Python 3.6 on Mac OS X

Image

How to Download and Install Python 3.6 on Mac OS X

Image

How to Install Node.js and NPM on a Mac

Image

How To Change your Mac's Language

Image

Shell Scripting Tutorial for Beginners 5 - If Statement ( If then , If t...

Image
Bash Shell Conditional Statements   Conditionals  let us decide whether to perform an action or not, this decision is taken by evaluating an expression. The most basic form is:        if [ expression ];        then              statements        elif [ expression ];        then              statements        else              statements        fi the  elif  (else if) and  else  sections  are optional Put  spaces after [  and  before ],  and  around the operators  and  operands.   Bash Shell Expressions   An  expression  can be:  String comp...

Shell Scripting Tutorial for Beginners 4 - Pass Arguments to a Bash-Script

Image
#! /bin/bash # $* Returns a single string (``$1, $2 ... $n'') # comprising all of the positional parameters # separated by the internal field separator character #(defined by the IFS environment variable). # $0 Refers to the name of the script itself echo $0 $1 $2 $3 ' > echo $1 $2 $3' # $@ Returns a sequence of strings # (``$1'', ``$2'', ... ``$n'') # wherein each positional parameter # remains separate from the others. args=("$@") echo ${args[0]} ${args[1]} ${args[2]} echo $@ # $# Refers to the number of arguments # specified on a command line. echo $# output: test@test$ ./hello.sh Mark Tom John ./hello.sh Mark Tom John > echo $1 $2 $3 Mark Tom John Mark Tom John 3

Shell Scripting Tutorial for Beginners 3 - Read User Input

Image
#! /bin/bash echo "Enter name : " read name echo "Enterd name : $name" # Multiple inputs echo "Enter names : " read name1 name2 name3 echo "Names : $name1 , $name2, $name3" # Two commonly used options however are # -p which allows you to specify a prompt # -s which makes the input silent. read -p 'username : ' user_var read -sp 'password : ' pass_var echo echo "username : $user_var" echo "password : $pass_var" # -a makes read command to read into an array echo "Enter name : " read -a names echo "Names : ${names[0]}, ${names[1]}" # read command will now store the reply into the default build-in variable $REPLY echo "Enter name : " read echo "Name : $REPLY"