Saturday, March 25, 2017

Shell Scripting Tutorial for Beginners 14 - Array variables

#! /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[@]}"

Wednesday, March 22, 2017

Shell Scripting Tutorial for Beginners 13 - The case statement Example



#! /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$ 

Tuesday, March 21, 2017

Shell Scripting Tutorial for Beginners 12 - The case statement

#! /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

Saturday, March 18, 2017

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



#! /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

Thursday, March 16, 2017

Shell Scripting Tutorial for Beginners 10 - Perform arithmetic operations

#! /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 )

Wednesday, March 15, 2017

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



#! /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

Monday, March 13, 2017

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

#! /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

Saturday, March 11, 2017

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



#! /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



#! /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

Monday, March 6, 2017

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





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 comparisonNumeric comparisonFile operators and Logical operators and it is represented by [expression]:
  • String Comparisons: 


=  compare if two strings are equal

!=  compare if two strings are not equal

-n  evaluate if string length is greater than zero

-z  evaluate if string length is equal to zero

  • Examples:


[ s1 = s2 ]  (true if s1 same as s2, else false)

[ s1 != s2 ]  (true if s1 not same as s2, else false)

[ s1 ]   (true if s1 is not empty, else false)

[ -n s1 ]   (true if s1 has a length greater then 0, else false)

[ -z s2 ]   (true if s2 has a length of 0, otherwise false)

Bash Shell

Expressions 
  • Number Comparisons:


-eq compare if two numbers are equal

-ge         compare if one number is greater than or equal to a number

-le  compare if one number is less than or equal to a number

-ne  compare if two numbers are not equal

-gt  compare if one number is greater than another number

-lt  compare if one number is less than another number

  • Examples:


[ n1 -eq n2 ]  (true if n1 same as n2, else false)

[ n1 -ge n2 ]  (true if n1greater then or equal to n2, else false)

[ n1 -le n2 ]  (true if n1 less then or equal to n2, else false)

[ n1 -ne n2 ]  (true if n1 is not same as n2, else false)

[ n1 -gt n2 ]  (true if n1 greater then n2, else false)

[ n1 -lt n2 ]  (true if n1 less then n2, else false)

Bash Shell

Examples 
cat user.sh

 #!/bin/bash

      echo -n “Enter your login name: "

      read name

      if [ “$name” = “$USER” ];

      then

            echo “Hello, $name. How are you today ?”

      else

            echo “You are not $USER, so who are you ?”

      fi

cat number.sh

#!/bin/bash

      echo -n “Enter a number 1 < x < 10: "

      read num

      if [ “$num” -lt 10 ]; then

            if [ “$num” -gt 1 ]; then

                  echo “$num*$num=$(($num*$num))”

            else

                  echo “Wrong insertion !”

            fi

      else

            echo “Wrong insertion !”

      fi





#! /bin/bash

word=a

if  [[ $word == "b" ]]
then
  echo "condition b is true"
elif [[ $word == "a" ]]
then 
  echo "condition a is true" 
else
  echo "condition is false"    
fi

Saturday, March 4, 2017

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





#! /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



#! /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"
IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ