Thursday, 5 September 2019

shell scripting examples-3

1). Example of variables
a=10
b=20
echo "a is : $a"
echo "b is : $b"
--------------------------------------------------------------------------------------------------------------------------

2). Write a script accept 2 integer no's and display
echo -e "Enter a number1 : \c"
read a
echo -e "Enter a number2 : \c"
read b
echo "a value is : $a"
echo "a value is : $b"
--------------------------------------------------------------------------------------------------------------------------

3). Write a script accept 2 integer no's and find sum
echo -e "Enter a number1 : \c"
read a
echo -e "Enter a number2 : \c"
read b
c='expr $a + $b'
echo "Sum of $a and $b is : $c"
--------------------------------------------------------------------------------------------------------------------------

4). Write a script accept a filename and open
echo -n "Enter a filename to open"
read fn
echo "-----------------------"
cat $fn
echo "-----------------------"
-------------------------------------------------------------------------------------------------------------------------

5). Write a script accept a filename and delete all empty lines.
echo -n "Enter a filename to delete blank lines:"
read fn
grep -v "^$" $fn > temp
mv temp $fn
echo "$fn file empty lines deleted"
--------------------------------------------------------------------------------------------------------------------------

6). Write a script accept a filename and delete all duplicate lines.
echo -n "Enter a filename to delete duplicate lines"
read fn
sort $fn | uniq -u > temp
mv temp $fn
echo "$fn file Duplicate lines are deleted"
--------------------------------------------------------------------------------------------------------------------------

7). Write a script accept a number and check the given no is +ve  or -ve.
echo -n "Enter a number : "
read n
if  [ $n -gt 0 ]
then
echo "$n is a +ve no."
else
echo "$n is a -ve no."
fi
-------------------------------------------------------------------------------------------------------------------------

8). Write a script accept a integer no and check the given no is even or odd number
echo -n "Enter a number:"
read n
if [ 'expr $n % 2' -eq 0]
then
echo "$n is an Even no."
echo "$n is an Odd no."
fi
--------------------------------------------------------------------------------------------------------------------------

9). Write a script accept 2 strings and check the given two strings are equal or not
echo -n "Enter a string1: "
read str1
echo -n "Enter a string2: "
read str2
if [ "$str1" = "$str2" ]
then
echo "Strings are Equal"
else
echo "Strings are not Equal"
fi
--------------------------------------------------------------------------------------------------------------------------

10). Write a script accept a filename and delete given file
echo -n "Enter a filename:"
read fn
if rm $fn
then
echo "$fn file deleted"
else
echo "No such file"
--------------------------------------------------------------------------------------------------------------------------

11). Write a script check today is Sunday or not.
x='date+%a' # x='date | cut -c l 3'
if [ $x= "Sun" ]
then
echo "Yes.Today is sunday"
else
echo "Sorry.Today is $x day"
fi
--------------------------------------------------------------------------------------------------------------------------


























No comments:

Post a Comment

Streamlining the usage of GITHUB

Streamlining the usage of Github with Commands 1. Start a new git repository under one directory locally In this step, we need to bui...