Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P.
, Department of IT, Technocrats
Institute of Technology)
Write the program to find biggest among 3 numbers using shell
a=$1
b=$2
c=$3
if [ $# -lt 3 ]
then
echo "$0 n1 n2 n3"
exit 1
fi
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is largest integer"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "$b is largest integer"
elif [ $c -gt $a -a $c -gt $b ];
then
echo "$c is largest integer"
else
echo "Sorry cannot guess number"
fi
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Write a shell program to check the given string is Palindrome (or) not
echo “ Enter the string”
read s
echo $s > temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo "it is palindrome"
else
echo " it is not"
fi
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Write a shell program to check whether the user is logged (or) not
#!/bin/sh
echo "Please enter the name of a user:"
read USER
who|grep $USER > /tmp/usertest &
sleep 5
if [ -s /tmp/usertest ]
then echo "User is logged in"
else echo "User is not logged in"
fi
rm /tmp/usertest
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Implement Link & Unlink in C
#include "apue.h"
#include <fcntl.h>
int
main(void)
if (open("tempfile", O_RDWR) < 0)
err_sys("open error");
if (unlink("tempfile") < 0)
err_sys("unlink error");
printf("file unlinked\n");
sleep(15);
printf("done\n");
exit(0);
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Implement LS command in C
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv ) {
DIR *dir;
struct dirent *dirent;
char *where = NULL;
if (argc == 1) where = get_current_dir_name();
else where = argv[1];
if (NULL == (dir = opendir(where))) {
fprintf(stderr,"%d (%s) opendir %s failed\n", errno, strerror(errno), where);
return 2;
}
while (NULL != (dirent = readdir(dir))) {
printf("%s\n", dirent->d_name);
}
closedir(dir);
return 0;
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Program to implement Grep command in C
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int max)
int strindex(char source[], char searchfor[]);
char pattern[] = "ould"; /* pattern to search for */
/* find all lines matching pattern */
main()
{ char line[MAXLINE];
int found = 0;
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0) {
printf("%s", line);
found++;
return found;
/* getline: get line into s, return length */
int getline(char s[], int lim)
{ int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{ int i, j, k;
for (i = 0; s[i] != '\0'; i++) {
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
if (k > 0 && t[k] == '\0')
return i;
return -1;
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
Implementing cat command using system calls
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc,char *argv[])
int f=0,n;
char l[80];
struct stat s;
if(argc!=2)
printf("Mismatch argument");
exit(1);
if(access(argv[1],F_OK))
printf("File Exist");
exit(1);
if(stat(argv[1],&s)<0)
printf("Stat ERROR");
exit(1);
if(S_ISREG(s.st_mode)<0)
{
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
printf("Not a Regular FILE");
exit(1);
if(geteuid()==s.st_uid)
if(s.st_mode & S_IRUSR)
f=1;
else if(getegid()==s.st_gid)
if(s.st_mode & S_IRGRP)
f=1;
else if(s.st_mode & S_IROTH)
f=1;
if(!f)
printf("Permission denied");
exit(1);
f=open(argv[1],O_RDONLY);
while((n=read(f,l,80))>0)
write(1,l,n);
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)