#! /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
Monday, March 13, 2017
Shell Scripting Tutorial for Beginners 8 - Logical 'AND' Operator
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
Wednesday, March 8, 2017
Tuesday, March 7, 2017
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.
Expressions
- An expression can be: String comparison, Numeric comparison, File 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 $#
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"
Subscribe to:
Posts (Atom)
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