Embedded System Laboratory-I: Index
Embedded System Laboratory-I: Index
SYSTEM
LABORATORY-I
INDEX
Page
1
SHELL SCRIPT PROGRAMS:
2
DA=90%, if BASIC<1500 and
HRA=500, DA=1500 if BASIC>1500.
Page
3
12. 04-10-2010 Print all the Directories in
the current working
directory.
Page
4
a directory which contain more than
specified no of the characters in a
file.
5
22. 01-11-2010 Open a file and print the contents
on the screen.
6
directory whose size exceeds
200bytes and remove all files
whose size is zero.
Page
7
33. 18-11-2010 Take one or more file or directory
names as command line arguments and
report the following information
a) File Type
b) Number of links
c) Time of last Access.
d) Read, Write, Execute
permissions are there or
not.
Program:
#usage : ssn source file target file
Page
8
cp $1 S2;
cat $2;
Output:
kalyan@kalyan-desktop :~$ sh ex1.sh f1 f2
WELCOME TO UNIX LAB
WELCOME DDP STUDENTS
KALYAN SRIVATSAV
Output:
kalyan@kalyan-desktop:~$ sh ex2.sh f1
f1
JNTU WIFI ROCKS
Page
9
Program:
#usage : ssn filename
a=$1
set `who am i`
mv $a $a.$1
Output:
kalyan@kalyan-desktop:~ sh ex3.sh f1
kalyan@kalyan-desktop:~ cat f1.kalyan
WELCOME TO UNIX LAB
WELCOME DDP STUDENTS
Program:
echo “ Enter the source name , destination file name ”
read sf df
if cp $sf $df
Page
10
then
echo “The Source File is copied to Destination file ”
else
echo “Coping of the Source file was unsuccessful ”
fi
Output:
kalyan@kalyan-desktop:~$ sh ex4.sh
Enter the Source and Destination file
f1 f3
The Source file was copied to Destination file
kalyan@kalyan-desktop:~ $ cat f5
KALYAN SRIVATSAV
kalyan@kalyan-desktop:~$ cat f1
KALYAN SRIVATSAV
Program:
echo “ENTER THE BASIC SALARY”
read b
if [ $b –lt 1500 ]
then
Page
11
hra=`expr $b \* 10 / 100`
da=`expr $b \* 10 / 100`
else
hra=1500
da=500
fi
gs=`expr $b + $hra + $da`
echo $gs
Output:
kalyan@kalyan-desktop:~$ sh ex5.sh
ENTER THE BASIC SALARY
2200
Gross Salary is 4400
Program:
echo “ ENTER THE FILE NAME”
read fn
if [ -f $fn ]
then
echo “The $fn is a file”
Page
12
else
echo “The $fn is not a file”
fi
Output:
kalyan@kalyan-desktop:~$ sh ex5.sh
ENTER THE FILE NAME
f2
The f2 is a file
7. Comparison of strings.
Program:
s1= “THIS IS FIRST STRING”
s2= “THIS IS SECOND STRING”
s3= “THIS IS FIRST STRING”
if [ $s1 = $s2 ]
then
echo “\n BOTH THE STRINGS ARE EQUALL\n”
Page
13
else
echo “\n BOTH THE STRINGS ARE NOT EQUALL\n”
if [ $s1 = $s3 ]
then
echo “\n BOTH THE STRINGS ARE EQUALL\n”
else
echo “\n BOTH THE STRINGS ARE NOT EQUALL\n”
fi
Output:
kalyan@kalyan-desktop:~$ sh ex7.sh
BOTH THE STRINGS ARE NOT EQUALL
BOTH THE STRINGS ARE EQUALL
Program:
echo “ENTER THE 3 SUBJECT MARKS”
read m1 m2 m3
total= `expr $m1 + $m2 + $m3`
echo $total
avg= `echo $total / 3 | bc`
if [ $avg –ge 70 ]
Page
14
then
echo “THE STUDENT IS IN DIVISION A”
else
if [$avg –gt 60 –a $avg –le 70 ]
then
echo “THE STUDENT IS IN DIVISION B”
else
echo “ THE STUDENT IS IN DIVISION C”
fi
fi
Output:
kalyan@kalyan-desktop:~$ sh ex8.sh
ENTER THE 3 SUBJECTS MARKS
80 70 38
THE STUDENT IS IN DIVISION B
Page
15
9. Print the Sum of 10 natural numbers.
Program:
echo “ENTER THE VALUE OF N ”
read n
i=1
sum=0
while [ $n –gt 0 ]
do
Page
16
sum=`expr $i + 1`
done
echo
Output:
kalyan@kalyan-desktop:~$ sh ex9.sh
ENTER THE VALUE OF N
10. Read each line from a file and print the contents on to
screen.
Program:
echo "enter file name"
read file
#terminal=`tty`
exec < $file
Page
17
while read line
do
echo $line
done
#exec < $terminal
Output:
kalyan@kalyan-desktop:~$ $ sh ex10
ENTER THE FILE NAME
f1
JNTU WIFI ROCKS
Program:
for word in $*
do
echo $word
done
echo "number of positional parameters" $#
Output:
Page
18
kalyan@kalyan-desktop:~$ sh ex11 KALYAN HARSHA
KALYAN
HARSHA
Program:
for entry in *
do
if [ -d $entry ]
then
echo “THIS ISA DIRECTORY WHICH YOU HAVE
Page
19
ENTERED”
fi
done
Output:
kalyan@kalyan-desktop:~$ sh ex12
kal THIS IS A DIRECTORY WHICH YOU HAVE
ENTERED
Program:
set `date +%H`
if [ $1 -lt 11 ]
then
echo "$1 Am"
else
Page
20
echo "$1 Pm"
fi
if [ $1 -lt 12 ]
then
echo "good morn"
elif [ $1 -ge 12 -a $1 -lt 15 ]
then
echo "good afternoon"
Output:
kalyan@kalyan-desktop:~$ sh ex13
10.45 am
Page
21
GOOD MORNING
Program:
x=$1
shift
while [ "$1" ]
do
Page
22
if [ -f $1/$x ]
then
echo yes
exit
else
shift
fi
done
echo no
Output:
kalyan@kalyan-desktop:~$ sh ex14 kal
/home/Documents/kal
yes
kalyan@kalyan-desktop:~$ sh script12 kw
/home/Documents/kw
no
Page
23
15. To list all filenames of a directory which contain more
than specified no of characters in a file.
Program:
echo "enter size"
read size
for entry in *
do
set `wc -c $entry`
Page
24
echo $1
if [ $1 -gt $size ]
then
echo $entry
fi
done
Output:
kalyan@kalyan-desktop:~$ sh ex15
500
a.out
wc: kal1: is a directory
wc: jack: is a directory
goodmor
wc: media: is a directory
Page
25
prime
wc: processes: is a directory
wc: signals: is a directory
stud
wc: syscall: is a directory
Program:
echo enter number
read n
f=1
x=$n
while [ $n -gt 1 ]
do
Page
26
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo factorial of $x is $f
Output:
kalyan@kalyan-desktop:~$ sh ex16
enter number
6
factorial of 5 is 720
17. Find the Sum of the digits for the given number.
Program:
echo "enter a number"
read num
sum=0
num1=$num
while [ $num -gt 0 ]
do
Page
27
rem=` expr $num % 10 `
sum=` expr $sum + $rem `
num=` expr $num / 10 `
done
echo sum of the digits of $num1 is $sum
Output:
kalyan@kalyan-desktop:~$ sh ex17
enter a number
234
sum of the digits of 234 is 9
Program:
echo " enter values for a b c "
read a b c
if [ $a -gt $b -a $b -gt $c ]
then
echo $a is big
elif [ $b -gt $c -a $b -gt $a ]
then
Page
28
echo $b is big
elif [ $c -gt $b ]
then
echo $c is big
else
echo equal
fi
Output:
kalyan@kalyan-desktop:~$ sh ex18
enter the values for a b c
34 56 23
56 is big
Program:
echo "enter number"
read n
rev=0
n1=$n
while [ $n -gt 0 ]
do
Page
29
r=`expr $n % 10`
rev=`expr $rev \* 10 + $r`
n=`expr $n / 10`
done
echo "reverse of $n1 is $rev"
Output:
kalyan@kalyan-desktop:~$ sh reverse
enter number
453
reverse of 453 is 354
Program:
echo "enter a number"
read n
f=0
i=1
while [ $i -le $n ]
do
Page
30
if [ `expr $n % $i` -eq 0 ]
then
f=`expr $f + 1`
fi
i=`expr $i + 1`
done
if [ $f -eq 2 ]
then
echo $n is prime
else
echo $n is not a prime
fi
Output:
kalyan@kalyan-desktop:~$ sh ex20
enter a number
2354
2354 is not a prime
Page
31
SYSTEM CALLS PROGRAMS:
Program:
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
int main()
Page
32
{
int fd1,fd2,n=1:
char buf;
fd1=open("f1",o_rdonly);
fd2=open("f2",o_wrongly|o_append|o_creat,s_iwusr);
if((fd1 ==-1)||(fd2==-1))
printf("error");
else
{
while(n>0)
{
n=read(fd1,&buf,1);
write(fd2,&buf,1);
}
}
}
Output:
kalyan@kalyan-desktop:~$ cc sys1.c
kalyan@kalyan-desktop:~$ cat f1
123
kalyan@kalyan-desktop:~$ cat f2
567
Page
33
kalyan@kalyan-desktop:~$ ./a.out
kalyan@kalyan-desktop:~$ cat f2
567
123
Program:
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>
int main()
Page
34
{
int fd1,n=1;
char buf;
fd1=open("f2",O_RDONLY);
if(fd1==-1)
printf("Error");
else
{
while(n>0)
{
n=read(fd1,&buf,1);
printf("%c",buf);
}
}
close(fd1);
}
Output:
kalyan@kalyan-desktop:~$ cc sys2.c
kalyan@kalyan-desktop:~$ cat f2
123
556677
[root@ecedomain ~]# ./a.out
Page
35
123
556677
Program:
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>
#include<sys/types.h>
int main()
Page
36
{
int fd1,fd2,fd3,n=1;
char buf;
fd1=open("example1",O_RDONLY);
fd2=open("example2",O_RDONLY);
fd3=open("f3",O_CREAT|O_APPEND|
O_WRONLY,S_IWUSR|S_IRUSR);
printf("%d %d %d",fd1,fd2,fd3);
if((fd3==-1))
printf("NO third file exists");
if((fd1==-1)||(fd2==-1))
printf("Error -1 not exists:");
else
{
while(n>0)
{
n=read(fd1,&buf,1);
printf("\n %d",n);
write(fd3,&buf,1);
}
n=1;
while(n>0)
{
Page
37
n=read(fd2,&buf,1);
printf("%d \n",n);
write(fd3,&buf,1);
}
}
close(fd1);
close(fd2);
close(fd3);
}
Output:
kalyan@kalyan-desktop:~$ cc sys3.c
kalyan@kalyan-desktop:~$ cat f1
123
kalyan@kalyan-desktop:~$ cat f2
567
kalyan@kalyan-desktop:~$ .a/.out
kalyan@kalyan-desktop:~$cat f3
123
Page
38
567
Program:
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>
int main()
{
Page
39
int fd1,pos;
char buf;
fd1=open("f3",O_RDONLY);
pos=lseek(fd1,0,SEEK_END);
if(fd1==-1)
printf(" no file exists");
else
{
pos=lseek(fd1,-1,SEEK_END);
while(pos>=0)
{
read(fd1,&buf,1);
write(1,&buf,1);
pos--;
lseek(fd1,-2,SEEK_CUR);
}
}
close(fd1);
return 0;
}
Output:
kalyan@kalyan-desktop:~$ cc sys4.c
Page
40
kalyan@kalyan-desktop:~$ cat f1
123
kalyan@kalyan-desktop:~$ ./a.out
321
Program:
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd1,n=1,c=0;
Page
41
char buf;
fd1=open("file1.user",O_RDONLY);
if(fd1==-1)
printf("error");
else
{
while(n>0)
{
n=read(fd1,&buf,1);
write(STDOUT_FILENO,&buf,1);
if(buf==' ')
c=c+1;
}
}
printf("number of blanks are %d",c);
}
Output:
kalyan@kalyan-desktop:~$ cc sys5.c
kalyan@kalyan-desktop:~$ cat f5
JNTU WIFI ROCKS
Page
42
kalyan@kalyan-desktop:~$ ./a.out
JNTU WIFI ROCKS
Number of Blank are 3
Program:
#include "all.h"
int main()
{
int fd1,n=1;
char buf,buf2;
fd1=open("file1.user",O_RDONLY);
if(fd1==-1)
Page
43
printf("no file exists:");
else
{
n=read(fd1,&buf,1);
buf2=toupper(buf);
write(1,&buf2,1);
while(n>0)
{
n=read(fd1,&buf,1);
buf2=toupper(buf);
write(1,&buf2,1);
}
}
close(fd1);
return 0;
}
Output:
kalyan@kalyan-desktop:~$ cc sys.26
kalyan@kalyan-desktop:~$ cat f1
Jntu kukatpally embedded system lab I
kalyan@kalyan-desktop:~$ ./a.out
JNTU KUKATPALLY EMBEDDED SYSTEM LAB II
Page
44
27. Write a program to convert the file contents as title
case(Start,of word,newline,space ).
Program:
#include "all.h"
int main()
{
int fd1,n=1;
char buf,buf2;
fd1=open("file1.user",O_RDONLY);
if(fd1==-1)
Page
45
printf("\n no file exists:");
else
{
read(fd1,&buf,1);
buf2=toupper(buf);
write(1,&buf,1);
while(n>0)
{
n=read(fd1,&buf,1);
if(buf==' '||buf=='\n'||buf=='.')
{
write(1,&buf,1);
n=read(fd1,&buf,1);
buf2=toupper(buf);
write(1,&buf2,1);
}
else
write(1,&buf,1);
}
}
close(fd1);
return 0;
Page
46
}
Output:
kalyan@kalyan-desktop:~$ cc sys6.c
kalyan@kalyan-desktop:~$ cat f6
welcome to unix lab
kalyan@kalyan-desktop:~$ ./a.out
Welcome To Unix Lab
Program:
#include "all.h"
int main()
{
int fd1,fd2;
char buf[10];
fd1=open("file1.user",0);
fd2=dup(fd1);
Page
47
read(fd1,&buf,5);
write(1,buf,5);
//read(fd2,&buf,10);
//write(1,buf,10);
}
Output:
kalyan@kalyan-desktop:~$ cc sys7.c
kalyan@kalyan-desktop:~$ cat f4
welcome to unix lab
kalyan@kalyan-desktop:~$ ./a.out
welcome to unix
Program:
#include "all.h"
int main()
{
DIR *dp;
FILE *fp;
Page
48
int d;
struct dirent *s;
struct stat buf;
dp=opendir("Example12");
chdir("Example12");
while((s=readdir(dp))!=NULL)
{
lstat(s->d_name,&buf);
if(S_ISREG(buf.st_mode))
{
printf("regular file");
printf("%s\n",s->d_name);
if(buf.st_size>4)
printf("%s",s->d_name);
printf("\n %d",buf.st_size);
if(buf.st_size==0)
unlink(s->d_name);
}
if(S_ISDIR(buf.st_mode))
{
printf("directory");
printf("%s\n",s->d_name);
Page
49
}
}
}
Output:
kalyan@kalyan-desktop:~$ cc sys29.c
kalyan@kalyan-desktop:~$./a.out
dprompt.c
pidgin.c
procs.c
edit.c
hex.c
Program:
#include "all.h"
int main(int argc,char *argv[])
{
Page
50
DIR *dp;
struct stat buf;
struct dirent *s;
lstat(argv[1],&buf);
if(S_ISDIR(buf.st_mode))
{
printf("directory");
dp=opendir(argv[1]);
while((s=readdir(dp))!=NULL)
{
printf("%d %s\n",s->d_ino,s->d_name);
}
}
if(S_ISREG(buf.st_mode))
{
printf("regular file");
printf("%s\n",argv[1]);
unlink(argv[1]);
mkdir(argv[1],"S_IRWXU");
chdir(argv[1]);
}
}
Page
51
Output:
kalyan@kalyan-desktop:~$ cc sys30.c
kalyan@kalyan-desktop:~$ ./a.out kal
ex1.c
ex2.c
ex3.c
f1
f2
f3
Program:
#include "all.h"
int main()
{
DIR *dp;
struct dirent *s;
struct stat buf;
dp=opendir("Example12");
Page
52
chdir("Example12");
while((s=readdir(dp))!=NULL)
{
lstat(s->d_name,&buf);
if(S_ISDIR(buf.st_mode))
{
printf("directory");
printf("%s",s->d_name);
}
}}
Output:
kalyan@kalyan-desktop:~$ ./a.out
kal
kw
jack
Page
53
32. Take one or more file or directory names as command
line args and report the following information:
a. File types
b. No of Links
c. Time of last access
d. RWX(All three) permissions are there or not.
Program:
#include "all.h"
int main()
{
struct stat buf;
Page
54
if(stat("file1.user",&buf)==-1)
printf("cannot stat");
else
{
if(S_ISREG(buf.st_mode))
printf("regularfile");
if(S_ISDIR(buf.st_mode))
printf("directory");
if(S_ISCHR(buf.st_mode))
printf("character special file");
if(S_ISBLK(buf.st_mode))
printf("block special file");
if(S_ISSOCK(buf.st_mode))
printf("socket special file");
printf("number of links %d\n",buf.st_nlink);
//printf("Last access time is %c \n",(ctime(buf.st_atime)));
if(access("file1.user",R_OK|W_OK|X_OK))
printf("readable,Writable,Executable \n");
else if(access("file1.user",R_OK|W_OK))
printf("readable,Writable \n");
else if(access("file1.user",R_OK|X_OK))
printf("readable,Executable \n");
Page
55
else if(access("file1.user",W_OK|X_OK))
printf("Writable,Executable \n");
else if(access("file1.user",R_OK))
printf("readable \n");
else if(access("file1.user",W_OK))
printf("Writable \n");
else
printf("Executable \n");
}
}
Output:
kalyan@kalyan-desktop:~$ cc sys32.c
kalyan@kalyan-desktop:~$ ls –l f1
-rw-rw-rw-4 root root 26 Nov 30 10:20 f1
kalyan@kalyan-desktop:~$ ./a.out
Regular file
Number of links 4
Readable and Writable
Page
56
Page
57