Shell script to check Armstrong number

SCRIPT:
#!/bin/bash
echo -n "Enter the nuber : "
read read_val
number=$read_val
buf1=0
buf2=0
sum=0
while [ $number -gt 0 ]
do
    buf1=`expr $number % 10`
    buf2=`expr $buf1 \* $buf1 \* $buf1`
    sum=`expr $sum + $buf2`
    number=`expr $number / 10`
   
done
if [ $sum -eq $read_val ]
then
    echo "$read_val is armstrong number"
else
    echo "$read_val not a armstrong number"
fi
OUTPUT:
(i)
Enter the nuber : 178
178 not a armstrong number

(ii)
Enter the nuber : 178
178 not a armstrong number



   

Shell script to find factorial of an number

SCRIPT:
#!/bin/bash
input=$1
fact_op=1
while [ $input -gt 0 ]
do
    echo "$fact_op  $input"
    fact_op=$(( $fact_op * $input ))
    input=$(( $input - 1 ))
done
echo "factorial of value $1 is $fact_op"

OUTPUT:
sujin@sujin:~/work/shell$ ./factorial.sh 5
1  5
5  4
20  3
60  2
120  1
factorial of value 5 is 120

Shell script to check file executable permission availability

SCRIPT:
#!/bin/bash
USAGE="usage : $0 {filename}"
CHECK_FILE=$1
if [ $# -lt 1 ]
then
 echo "$USAGE"
 exit 1
fi
if [ -x "$CHECK_FILE" ]
then
 echo "$CHECK_FILE has the executable permission"
else
 echo "$CHECK_FILE not has executable permission"
fi
OUTPUT-1:
test.sh has the executable permission

OUTPUT-2:

sujin_sr not has executable permission

C Program Lab Exercise


I. SWAPPING OF TWO NUMBERS USING FUNCTION:
Program:
#include <stdio.h>
void swap(int, int);
int main()
{
    int x, y;
    x = 10;
    y = 20;
    swap(x, y);
    printf("Value of x and y after swap(x, y) in main()  : %d %d \n", x, y);
}

void swap(int a, int b)
{
    int t;
    printf("Value of a and b before exchange in swap()   : %d %d \n", a, b);
    t = a;
    a = b;
    b = t;
    printf("Value of a and b after exchange in swap()    : %d %d \n", a, b);
    return 0;
}

Output:
Value of a and b before exchange in swap()   : 10 20
Value of a and b after exchange in swap()    : 20 10
Value of x and y after swap(x, y) in main()  : 10 20


Shell Script to reverse string


SCRIPT
#!/bin/bash
input="$1"
output=""
output=$(echo $input | rev)
echo "Input  : $input"
echo "Output : $output"

OUTPUT:
sujin@sujin:~/sujin/common/script$ bash reverse.sh linux
Input  : linux
Output : xunil