Scripting Language Practice Session



Objective:
·         Learn how to work with expressions in Shell Scripting
·         Learn about conditional structures in Shell
·         Learn about functions in Shell
·         Hands on Practice on different shell scripts using case and fucntions

Tips:
For example, the $ character represents the process ID number, or PID, of the current shell
$echo $$

The following table shows a number of special variables that you can use in your shell scripts −
Sr.No.
Variable & Description
1
$0
The filename of the current script.
2
$n
These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
3
$#
The number of arguments supplied to a script.
4
$*
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
5
$@
All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
6
$?
The exit status of the last command executed.
7
$$
The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
8
$!
The process number of the last background command.
#!/bin/sh

echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

For Loop We can do these like C++:
#!/bin/sh

for TOKEN in $*
do
   echo $TOKEN
done
Result
$./test.sh Hamail Ayaz 26 Years Old
hamail
Ayaz
26
Years
Old
Example
echo "Enter numnber : "
read n
rem=$(( $n % 2 ))
 if [ $rem -eq 0 ]
 then echo "$n is even number"
else
  echo "$n is odd number"
fi
Example
count=1
while [ $count –le 10]
do
           
echo $count
count=`expr $count + 1`
done
Example
for i in {1..10..2}
do
            echo $i
done

Moving Forward:
Ten basic Program in order to learn Shell scripting
1.  Q1 print hello word?
2.  #include <iostream>
3.  using namespace std;
4.   
5.  int main()
6.  {
7.      cout << "Hello, World!";
8.      return 0;
9.  }
Q1: Write a Shell Script to hello world?

1.  Q2 write a code to add 2 numbers In integer? 
2.  #include <iostream>
3.  using namespace std;
4.   
5.  int main()
6.  {
7.      int firstNumber, secondNumber, sumOfTwoNumbers;
8.      
9.      cout << "Enter two integers: ";
10.     cin >> firstNumber >> secondNumber;
11.  
12.     // sum of two numbers in stored in variable sumOfTwoNumbers
13.     sumOfTwoNumbers = firstNumber + secondNumber;
14.  
15.     // Prints sum
16.     cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;    
17.  
18.     return 0;
19. }
Q2: Write a Shell Script to add two variable in shell scripting?
1.  Q3: Compute quotient and remainder?
2.  #include <iostream>
3.  using namespace std;
4.  int main()
5.  {    
6.      int divisor, dividend, quotient, remainder;
7.   
8.      cout << "Enter dividend: ";
9.      cin >> dividend;
10.  
11.     cout << "Enter divisor: ";
12.     cin >> divisor;
13.  
14.     quotient = dividend / divisor;
15.     remainder = dividend % divisor;
16.  
17.     cout << "Quotient = " << quotient << endl;
18.     cout << "Remainder = " << remainder;
19.  
20.     return 0;
21. }
Q3: Write a Shell Script to compute quotient and remainder?

 

1.  Q4: Swap Numbers (Using Temporary Variable)?
2.  #include <iostream>
3.  using namespace std;
4.   
5.  int main()
6.  {
7.      int a = 5, b = 10, temp;
8.   
9.      cout << "Before swapping." << endl;
10.     cout << "a = " << a << ", b = " << b << endl;
11.  
12.     temp = a;
13.     a = b;
14.     b = temp;
15.  
16.     cout << "\nAfter swapping." << endl;
17.     cout << "a = " << a << ", b = " << b << endl;
18.  
19.     return 0;
20. }

Q4: Write a Shell Script to Swap Numbers (Using Temporary Variable)?

 

1.  Q5: Check Whether Number is Even or Odd using if else
2.  #include <iostream>
3.  using namespace std;
4.   
5.  int main()
6.  {
7.      int n;
8.   
9.      cout << "Enter an integer: ";
10.     cin >> n;
11.  
12.     if ( n % 2 == 0)
13.         cout << n << " is even.";
14.     else
15.         cout << n << " is odd.";
16.  
17.     return 0;
18. }

Q5: Write a Shell Script to Check Whether Number is Even or Odd using if else?



Q6// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n

#include <iostream>
using namespace std;

int main()
{
    int i, n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}

Q6: Write a Shell Script to determine the factorial of “N” number?

Post a Comment

0 Comments