Practical Unix
Practical Unix
a=$1
b=$2
c=$3
if [ $# -lt 3 ]
then
exit 1
fi
if [ $a -gt $b -a $a -gt $c ]
then
then
then
else
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
read s
rvs="$(rev temp)"
if [ $s = $rvs ]
then
else
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)
#include "apue.h"
#include <fcntl.h>
int
main(void)
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>
closedir(dir);
return 0;
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
#include <stdio.h>
main()
{ char line[MAXLINE];
int found = 0;
printf("%s", line);
found++;
return found;
{ int c, i;
i = 0;
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)
{ int i, j, k;
return i;
return -1;
}
Practicalsof Unix and Shell Prog by Rakesh K Bhujade (A.P., Department of IT, Technocrats
Institute of Technology)
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
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)
exit(1);
if(geteuid()==s.st_uid)
f=1;
else if(getegid()==s.st_gid)
f=1;
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)