Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, 3 November 2015

Linux Shell program function to find the sum of digits of a number

function sum()
{
echo "enter n"
read n
sum=0
while [ $n -gt 0 ]
do
sum=`expr $sum + $n % 10 `
n=`expr $n / 10`
done
echo -n "sum of digits:"
echo $sum
}
sum


OUTPUT:
enter n 231
sum of digits:6

Linux Shell program to compare two strings

echo enter s1
read s1
echo enter s2
read s2
if test [s1-eq s2 ]
then
echo "Same"
else
echo "Not"
fi


OUTPUT:
enter s1 Marshal
enter s2 marshal

Not

Linux Shell program to reverse a number

echo "enter a number:"
read n
while [ $n -gt 0 ]
do
echo -n `expr $n % 10 `
n=`expr $n / 10 `
done


OUTPUT:
enter a number: 345
543

Linux Shell program function to read and print a string

function Print
{
echo $1
}

echo "Enter name:"
read name

Print $name



OUTPUT:
Enter name: Pravin
Pravin

Linux Shell program to count the no. of vowels in a file

echo enter filename
read filename
egrep [^aeiou] $filename |wc -l > vowcount
cat vowcount

Linux Shell program to check access mode of a file

if [ -r /$PWD/new2 ]
then
   echo “new2 exists and is executable. Exiting.”
elif [ -x /bin/bash ]
then
   echo “/bin/bash exists and is executable. Exiting.”
elif [ -x /bin/sh ]
 then
   echo “/bin/sh exists and is executable. Exiting.”
else
   echo “Found no executable program.”
fi

Linux program to check access rights - Linux - C program

#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>

int main()
{

if(access("new",R_OK)==0)
printf("\nIt is readable");

if(access("new",W_OK)==0)
printf("\nIt is writable");
if(access("new",X_OK)==0)
printf("\nIt is executable");
return 0;
}


OUTPUT;
It is readable
It is writable