Showing posts with label shell program. Show all posts
Showing posts with label shell program. Show all posts

Wednesday, April 8, 2015

Pointer variables

Algorithm
Step 1 : Initialize two integer variables x and y with some values.
Step 2 : Create a pointer iptr of integer type
Step 3 : Assign the address of x to iptr
Step 4 : Print the value and address stored in iptr
Step 5 : Assign the address of y to iptr
Step 6 : Print the value and address stored in iptr
Step 7 : Print the storage requirement of iptr, address of iptr and main function

Program (ptrvar.c)

/* Pointer variables */
# include<stdio.h>
main()
{
int x, y, *iptr;
x = 125;
iptr = &x ; /* iptr points to x */
y = 23;
float *fptr;
printf("X value is %d and stored at %u\n", x, &x);
printf("Y value is %d and stored at %u\n", y, &y);
printf("\nInt pointer holds the address %u\n", iptr);
printf("Aceesing value thru pointer : %d\n", *iptr);
iptr = &y; /* iptr points to y */

Tuesday, April 7, 2015

Reference and Dereference operator

Algorithm
Step 1 : Start
Step 2 : Initialize x to 10
Step 3 : Print the value of x
Step 4 : Print the address of x using address operator
Step 5 : Print the value of x by dereferencing the address operator
Step 6 : Stop

Program (refderef.c)
/* Data and address */
#include <stdio.h>
main()
{
int x;
x=10;
printf("Value of x is %d",x);
printf("\nAddress of x is %u",&x);
printf("\nValue of x is %d\n",*(&x));
}

Monday, April 6, 2015

Keystroke detection

Program (keystroke.sh)
# Detect key stroke
echo "Hit a key and then hit return."
read Keypress
case "$Keypress" in
[[:lower:]] ) echo "Lowercase letter";;
[[:upper:]] ) echo "Uppercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac

Output
[vijai@localhost command]$ sh keystroke.sh
8
Digit

Sunday, April 5, 2015

Compilation and execution of a C program

# Compile and execute
while true
do
gcc -o $1.out $1.c
case "$?" in
0)echo "Executing ..."
./$1.out
exit;;
*)
sleep 5
vi $1.c
esac
done

Output
[vijai@localhost command]$ sh debug.sh samp
samp.c:4:10: warning: multi-line string literals are
deprecated
samp.c:4:10: missing terminating " character
samp.c:4:10: possible start of unterminated string literal
samp.c: In function `main':
samp.c:4: parse error at end of input
Executing ...
Hello

Saturday, April 4, 2015

Duplicate file removal

Program (dupremove.sh)
# Duplicate file removal
if cmp $1 $2
then
echo "Files $1, $2 are identical"
rm $2
echo "$2 file deleted"
else
echo "Files $1, $2 are distinct"
fi

Output
[vijai@localhost command]$ sh dupremove.sh samp.c s.c
Files samp.c, s.c are identical
s.c file deleted

Friday, April 3, 2015

Detecting user when logged on

Program (logdetect.sh)
#!/bin/bash
#Detect the user at logon
while true
do
if who|grep $1>/dev/null
then
echo $1 is logged in
exit
fi
done

Output
[vijai@localhost command]$ sh logdetect.sh cse1
cse1 is logged in

Thursday, April 2, 2015

Detecting file type

Program (filetype.sh)
# File type
echo -n "Enter filename : "
read fname
if [ -e $fname ]
then
if [ -f $fname ]
then
echo "Regular file"
elif [ -d $fname ]
then
echo "Directory"
else
echo "Special file"
fi
else
echo "File does not exist"
fi
Output
[vijai@localhost command]$ sh filetype.sh
Enter filename : samp.c
Regular file

Wednesday, April 1, 2015

Directory Listing

Program (cmdmenu.sh)
for x in .
do
ls -R $x
done

Output
[vijai@localhost command]$ sh list.sh
.:
list.sh shellscripts
./shellscripts:
command decision loops patmatch simple
./shellscripts/command:
cmdmenu.sh debug.sh dupremove.sh filetype.sh keystroke.sh
./shellscripts/decision:
big3.sh emppay.sh grade.sh leap.sh oddeven.sh strcomp.sh
./shellscripts/loops:
armstrong.sh datastat.sh fact.sh fibo.sh multable.sh
./shellscripts/patmatch:
calc.sh rental.sh vote.sh vowel.sh
./shellscripts/simple:
circle.sh degconv.sh simpint.sh swap.sh

Tuesday, March 31, 2015

Commands menu

Program (cmdmenu.sh)
# Menu program
ch='y'
while [ $ch == 'y' ]
do
echo -e "\tMENU
1. List of files
2. Working Directory
3. Date and Time
4. Users of the system
5. Calendar
Enter the option : \c"
read choice
case "$choice" in
1) ls -l ;;
2) pwd ;;
3) date ;;
4) who ;;
5) cal
esac
echo -n "Do you wish to continue (y/n) : "
read ch
done

Monday, March 30, 2015

Number of days in a month

Program (monthdays.sh)
# Number of days in a month
mth=`date +%m`
mn=`date +%B`
case $mth in
02)echo "February usually has 28 days"
echo "If leap year, then it has 29 days" ;;
04|06|09|11)echo "The current month $mn has 30 days" ;;
*) echo "The current month $mn has 31 days"
esac

Output
[vijai@localhost command]$ sh monthdays.sh
The current month April has 30 days

Sunday, March 29, 2015

Time based greeting

Program (wish.sh)
#!/bin/bash
x=`date +%H`
mrd=`date +%P`
if [ $mrd=='am' ]
then
if [ $x -le 11 ]
then
echo "Good Morning"
fi
else
if [ $x -le 2 ]
then
echo "Good Afternoon"
elif [ $x -le 6 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
fi

Output
[vijai@localhost command]$ sh wish.sh
Good Morning

Saturday, March 28, 2015

Data Statistics

Algorithm
Step 1 : Start
Step 2 : Initialize 0 to pc, sum, i
Step 3 : Read a number
Step 4 : If number = 9999 then goto step 10
Step 5 : Increment i by 1
Step 6 : If number􀀃􀂔 0 then goto step 3
Step 7 : Increment pc by 1
Step 8 : Add number to sum
Step 9 : Goto step 3
Step 10 : Compute avg = sum / pc
Step 11 : Print i, pc, avg,
Step 12 : Stop

Program (datastat.sh)
# Aggregate of positive nos using break and continue
clear
pc=0
s=0
i=0
until false
do
echo -n "Enter a number (9999 to quit) : "
read n
if [ $n -eq 9999 ]
then
break
fi

Friday, March 27, 2015

Sum of 1..N natural numbers

Algorithm
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize 0 to sum and 1 to i
Step 4 : Add i to sum
Step 5 : Increment i by 1
Step 6: Repeat steps 4–6 until i􀀃􀂔 n
Step 7 : Print sum
Step 8 : Stop

Program (sum1ton.sh)
# Sum of 1+2+3+ ... +N numbers
echo -n "Enter N value : "
read n
sum=0
i=1

Thursday, March 26, 2015

Factorial Value

Algorithm
Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 1 to fact and number to i
Step 4 : fact = fact * i
Step 5 : Decrement i by 1
Step 6: Repeat steps 4–6 until i > 0
Step 7 : Print fact
Step 8 : Stop

Program (fact.sh)
# Factorial value using until
echo -n "Enter a positive number : "
read n
f=1
until [ $n -lt 1 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo "Factorial value : $f"

Output
[vijai@localhost loops]$ sh fact.sh
Enter a positive number : 10
Factorial value : 3628800

Wednesday, March 25, 2015

Prime Number

Algorithm
Step 1 : Start
Step 2 : Read the value of n
Step 3 : Initialize i to 2
Step 4 : If n is divisible by i then
Print “Not Prime” and Stop
Step 5 : Increment i by 1
Step 6 : Repeat steps 4 and 5 until i 􀁤 n/2
Step 7 : Print "Prime"
Step 8 : Stop

Program (prime.sh)
# Prime number using exit
echo -n "Enter the number : "
read n
i=2
m=`expr $n / 2`

Tuesday, March 24, 2015

Fibonacci Series

Algorithm
Step 1 : Start
Step 2 : Read number of terms as n
Step 3 : Initialize 0 to f1, 1 to f2 and 2 to i
Step 4 : Print initial fibonacci terms f1, f2
Step 5 : Generate next term using the formula f3 = f1 + f2
Step 6 : Print f3
Step 7 : Increment i by 1
Step 8 : Assign f2 to f1
Step 9 : Assign f3 to f2
Step 10 : Repeat steps 5–9 until i 􀁤 n
Step 11 : Stop

Program (fibo.sh)
# Fibonacci series using while loop
echo -n "Enter number of terms : "
read n
echo "Fibonacci Series"
f1=0
f2=1
echo -n "$f1 "
echo -n " $f2 "
i=2

Monday, March 23, 2015

Number Reverse

Algorithm
Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 0 to reverse
Step 4 : Extract lastdigit by computing number modulo 10
Step 5 : Compute reverse = reverse10 + lastdigit
Step 6 : Divide number by 10
Step 7: Repeat steps 4–6 until number > 0
Step 8 : Print reverse
Step 9 : Stop

Program (reverse.sh)
# To reverse a number using while loop
echo -n "Enter a number : "
read n
rd=0
while [ $n -gt 0 ]
do
rem=`expr $n % 10`
rd=`expr $rd \* 10 + $rem`
n=`expr $n / 10`
done
echo "Reversed number is $rd"

Output
[vijai@localhost loops]$ sh reverse.sh
Enter a number : 234
Reversed number is 432

Sunday, March 22, 2015

Armstrong Number

Algorithm
Step 1 : Start
Step 2 : Read number
Step 3 : Initialize 0 to sum and number to num
Step 4 : Extract lastdigit by computing number modulo 10
Step 5 : Cube the lastdigit and add it to sum
Step 6 : Divide number by 10
Step 7: Repeat steps 4–6 until number > 0
Step 8 : If sum = number then
Print “Armstrong number”
Step 8.1 : else
Print “Not an Armstrong number”
Step 9 : Stop

Program (armstrong.sh)
# Armstrong number using while loop
echo -n "Enter a number : "
read n
a=$n
s=0
while [ $n -gt 0 ]

Friday, March 20, 2015

Vote Eligibility

Algorithm
Step 1 : Start
Step 2 : Read age
Step 3 : If age 􀂔 17
Print "Not eligible to vote"
Step 3.1 : else
Print "Eligible to vote"
Step 4 : Stop

Program
# Vote--range matching
echo -n "Enter your age : "
read age
case $age in
[0-9]|1[0-7])echo "You are not eligible to vote";;
*)echo "Eligible to vote"
esac

Output
[vijai@localhost multway]$ sh vote.sh
Enter your age : 12
You are not eligible to vote

Thursday, March 19, 2015

Rental Options

Algorithm
Step 1 : Start
Step 2 : Read vehicle
Step 3 : If vehicle = "car" then
Print "Rental is Rs. 20/km"
Step 3.1 : else if vehicle = "van" then
Print "Rental is Rs. 10/km"
Step 3.2 : else if vehicle = "jeep" then
Print "Rental is Rs. 5/km"
Step 3.3 : else if vehicle = "bicycle" then
Print "Rental is Rs. 0.2/km"
Step 3.4 : else
Print "Vehicle not available"
Step 4 : Stop

Program (rental.sh)
# String matching
echo "Two/Four wheeler rental"
echo -n "Enter the required vehicle : "
read vehicle