0% found this document useful (0 votes)
75 views

Embedded System Laboratory-I: Index

This document contains a summary of 20 shell script programs covering topics such as copying files, changing file permissions, renaming files, comparing strings, calculating gross salary, checking file types, and more. Each program is presented with its purpose, code, and example output. The programs demonstrate various shell scripting concepts like command line arguments, conditional statements, loops, reading from files, and more.

Uploaded by

Kalyan Srivatsav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Embedded System Laboratory-I: Index

This document contains a summary of 20 shell script programs covering topics such as copying files, changing file permissions, renaming files, comparing strings, calculating gross salary, checking file types, and more. Each program is presented with its purpose, code, and example output. The programs demonstrate various shell scripting concepts like command line arguments, conditional statements, loops, reading from files, and more.

Uploaded by

Kalyan Srivatsav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

EMBEDDED

SYSTEM
LABORATORY-I

INDEX
Page

1
SHELL SCRIPT PROGRAMS:

SNo DATE TOPICS

1. 27-09-2010 Copy the contents of


source file to destination
file.

2. 27-09-2010 Change the permission of the


file and execute it.

3. 27-09-2010 Rename a file like


filename.username

4. 27-09-2010 Copy a file from source to


destination ,if copied
Successfully then
print a message successful.

5. 27-09-2010 Calculate the gross salary for


the given conditions: HRA=10%,
Page

2
DA=90%, if BASIC<1500 and
HRA=500, DA=1500 if BASIC>1500.

6. 27-09-2010 Check whether the command


line argument is a
file or not.

7. 27-09-2010 Comparison of two strings.

8. 27-09-2010 Display the grade of a student based on


the marks obtained.

9. 27-09-2010 Print Sum of Ten


Natural Numbers.

10. 27-09-2010 Read each line from a file


and display on the
screen(use the concepts of loops).

11. 04-10-2010 Print all the positional


Parameters which are
passed to the program.

Page

3
12. 04-10-2010 Print all the Directories in
the current working
directory.

13. 11-10-2010 Display Good Morning,


Afternoon, Evening,
Night based on time.

14. 11-10-2010 Print the message yes if the


files or directories are present
in any one of the directories
when these(files or directories) are taken
as command line arguments.

15. 11-10-2010 Print the list of the filenames of

Page

4
a directory which contain more than
specified no of the characters in a
file.

16. 18-10-2010 Find the factorial of a given number.

17. 18-10-2010 Print the Sum of the digits of a given


number.

18. 25-10-2010 Print the number which is biggest among


Three Numbers.

19. 25-10-2010 Reverse a given number and print.

20. 01-11-2010 Check a given number is prime or not.

SYSTEM CALLS PROGRAMS:

21. 01-11-2010 Copy the contents of one file to


another.
Page

5
22. 01-11-2010 Open a file and print the contents
on the screen.

23. 04-11-2010 Concatenate two files content and


store in the third file.

24. 04-11-2010 Create a hole in file.

25. 04-11-2010 Print the contents of the file in reverse


order.

26. 04-11-2010 Count the number of blanks in a given


file.

27. 04-11-2010 Convert the file contents from


lower case to upper case and store in
other file.

28. 16-11-2010 Convert the file contents as title case.

29. 16-11-2010 Demonstrate a duplicate function.

30. 16-11-2010 List all ordinary files from current


Page

6
directory whose size exceeds
200bytes and remove all files
whose size is zero.

31. 16-11-2010 Accept the directory name as


an argument and check whether
it is present or not. If it is not a
directory then remove the file
and create directory with the
same name. If yes go to directory
and print the information.

32. 18-11-2010 List all the Directories present in current


directory.

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.

SHELL SCRIPT PROGRAMS:

1. Copy the contents of source file to destination file and


display the target file contents using command line
arguments.

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

2. Change the permissions of a file and execute it.


Program:
#usage : xyz file
chmod 0644 $1
echo $1
echo “JNTU WIFI ROCKS ”

Output:
kalyan@kalyan-desktop:~$ sh ex2.sh f1
f1
JNTU WIFI ROCKS

3. Rename any file as filename.username.

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

4. Give a message successful if the file is copied successfully.

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

5. Calculate gross salary HRA-10%,DA-90% if BASIC<1500


and HRA-500, DA-1500 if BASIC>1500.

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

6. Check whether the given command line argument is file or


not.

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

8. Display grades obtained by the student based on marks.

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

11. Enter all positional parameters passed to program.

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

12. Print all directories in current working directories.

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

13. Display GOOD MORNING,EVENING,AFTERNOON


AND NIGHT based on time.

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"

elif [ $1 -ge 15 -a $1 -lt 19 ]


then
echo "good eve"
else
echo "good night" fi

Output:
kalyan@kalyan-desktop:~$ sh ex13
10.45 am

Page

21
GOOD MORNING

14. Take src file name,directories names as command line


args and print message yes if the file is found in any
one of the directories .

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

16. Find Factorial of a given Number.

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

18. Find the Biggest number of the given Three numbers.

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

19. Reverse the Number and print it.

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

20. Check the given number is prime or not.

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:

21. Write a Program to copy the contents of one file to


another.

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

22. Write a program to open a file and print the contents


onto screen.

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

23. Write a program to concatenate the two files and store


in the third file.

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

24. Write a program to print the file in reverse order on


the screen.

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

25. Write a program to count the number of blanks in a


given file.

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

26. Write a program to convert the lower case to upper


case and store in other file.

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

28. Write a program to demonstate a duplicate file.

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

29. List all ordinary files from current working directories


whose size exceeds 200bytes and remove the all files
whose size is zero.

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

30. Accept directory name as an arg and check whether it


is present or not,if not then remove the file, create a
directory with the same name as the file name.if the go
to the directory and print all the information in the
directory.

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

31. Write a program to list out all present working


directories.

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

You might also like