Posts Tagged ‘unix shell programs’

System Software Lab Programs ( UNIX Programs ) – 6b

// June 6th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

6b) C program that accepts a valid directory names as a command line argument and lists all the files in the given directory as well as all  the subsequent subdirectories. (The solution can be recursive or nonrecursive).

#include<stdio.h>
#include<ftw.h>
int recursive(const char *dir,const struct stat *name,int type)
{
if(type==FTW_D)
printf(“n Directory->%s”,dir);
else if(type==FTW_F)
printf(“nFile->%s”,dir);
return 0;
}

int main(int argc,char *argv[])
{
int depth=5;
ftw(argv[1],recursive,depth);
printf(“n”);
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 6a

// June 6th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

6. a) Shell script that accepts valid log-in names as arguments and prints their corresponding home directories. If no arguments are specified, print a suitable error message.

if [ $# -eq 0 ]
then
echo -e “No arguments”
else
for a in $*
do
grep $a /etc/passwd>list
if [ $? -eq 0 ]
then
echo “Home dir of $a is :”
cut -c 20-46 list
else
echo “Improper login name $a”
fi
done
fi

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 5b

// June 5th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

5b) C program that accepts one command-line argument, executes the arguments as a shell command, determines the time taken by it and prints the time values, Use the “times”, function and the “tms” structure. The code need not include error checking.

#include<stdio.h>
#include<sys/times.h>
#include<unistd.h>

int main(int argc,char *argv[10])
{
char cmd[10];
struct tms obj1,obj2;
clock_t st,end;
float time;
st=times(&obj1);
system(“clear”);
printf(“%Sn”,argv[1]);
end=times(&obj2);
printf(“Real time taken = %fn”,st);
printf(“User time taken = %fn”,end);
time=(end-st)/3600;
printf(“nSystem time taken : %f”,time);
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 5a

// June 5th, 2009 // Comments // Educational, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

5. a) Shell script that accepts path names and creates all the components in that pathnames as directories. For example, if the script name is
mpe, then the command mpe a/b/c/d should create directories a, a/b, a/b/c, and a/b/c/d.

v1=`echo $* |tr “/” ” “`
echo $v1
for i in $v1
do
mkdir $i
cd $i
done

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 4b

// June 4th, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

4b) C program to do the following: Using fork( ) create a child process. The child process prints its own process-id and id of its parent and then exits. The parent process waits for its child to finish (by executing the wait( )) and prints its own process-id and the id of its child process and then exits.

#include<stdio.h>
#include<sys/types.h>

int main()
{
int status,ppid,mpid,pid;
pid=fork();

if(pid<0)
{
printf(“Error in forking a child..!”);
exit(0);
}

if(pid==0)
{
ppid=getppid();

printf(“nChild is executing and it’s Parent ID is  : %d”,ppid);
mpid=getpid();
printf(“nChild ID is  : %d”,mpid);
kill();
exit(0);
}
//pid=wait(pid,&status,0);
mpid=getpid();

printf(“nParent ID is : %d  and it’s child ID is : %dn”,mpid,pid);
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 4a

// June 3rd, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

4. a) Shell script that accepts file names specified as arguments and creates a shell script that contains this file as well as the code to
recreate these files. Thus if the script generated by your script is executed, it would recreate the original files(This is same as the “bundle” script described by Brain W. Kernighan and Rob Pike in “ The Unix Programming Environment”, Prentice – Hall India).

#!/bin/bash

echo “# to unbundle,bash this file n:”
for i
do
echo “echo $ i1>i2″
echo “cat >$i <<’END of $i’”
cat $i
echo “END OF $i”
echo -e
done

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 3b

// June 2nd, 2009 // Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

Problem Definition : C program that accepts valid file names as command line arguments and for each of the arguments, prints the type of the file (Regular file, Directory file, Character special file, Block special file, Symbolic link etc.)

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>

int main(int argc,char *argv[])
{
int i;
struct stat buf;

for(i=1;i<=argc;i++)
{
printf(“%s”,argv[i]);

if(lstat(argv[i],&buf)==-1)
{
printf(“nlstat error..n”);
continue;
}

if(S_ISREG(buf.st_mode))
printf(“nRegular filen”);

else if(S_ISDIR(buf.st_mode))
printf(“nDirectory filen”);

else if(S_ISCHR(buf.st_mode))
printf(“nCharacter special filen”);

else if(S_ISBLK(buf.st_mode))
printf(“nBlock special filen”);

else if(S_ISLNK(buf.st_mode))
printf(“nSymbolic link filen”);

else
printf(“nUnknown filen”);

return 0;
}
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 3a

// March 8th, 2009 // Comments // Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….


3.a) Shell function that takes a valid directory names as an argument an recursively descends all the sub directories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.

……………………………………………………………………………………………………………………………

clear

find

{

clear tput

echo -e “Enter a valid directory name : ”

read dir

if [  -d @dir ]

then

ls -1R $dir>file1

grep -h “^-r” file1>file2

cat file2

cut -c 24-28 file2>file3

sort -n file3>file4

tail -1 file4>file5

echo -e “Max length is ”

cat file5

else

else

echo -e “Invalid dir ”

fi

}

[Click Here to get other programs by email ]

[Request : If you are going to post this program on any other website please link back to original post ]


System Software Lab Programs ( UNIX Programs ) – 2b

// March 6th, 2009 // Comments // Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

2. b) C program to create a file with 16 bytes of arbitrary data from the beginning and another 16 bytes of arbitrary data from an offset of 48. Display the file contents to demonstrate how the hole in file is handled.

……………………………………………………………………………………………………………………………

#include<stdio.h>

#include<unistd.h>

main()

{

FILE *fp;

int i,j;

char ch;

fp=fopen(“text.txt”,”w”);

ch=’a';

for(i=o;i<16;i++)

{

fwrite(&ch,1,1,fp);

ch++;

}

fseek(fp,48L,1);

ch=’A';

for(j=0;j<16;j++)

{

fwrite(&ch,1,1,fp);

ch++;

}

fclose(fp);

fp=fopen(“text.text”,’r”);

while((fread(&ch,1,1,fp))!=0)

print(“%c”,ch);

fclose(fp);

}

[Request : If you are going to post this program on any other website please link back to original post ]


System Software Lab Programs ( UNIX Programs ) – 2a

// March 5th, 2009 // Comments // Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART -B

………………………………………………………………………………………………………………………….

2a.  Shell script that accepts two file names as arguments, checksif the permissions for these files are identical and if the permissions are identical, outputs the common permissions,  otherwise outputs each file name followed by its permissions.

……………………………………………………………………………………………………………………………

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

clear

if [ $# -ne 2]

then

echo -e “n Two arguments are not passed n”

else

{

ls -l $1>file1

ls -l $2>file2

cut -c 1-10 file1>file3

cut -c 1-10 file2>file4

cmp file3 file4>res

if test -s res

then

echo -e “n Files are having different permissionn”

cat file1

cat file2

else

{

echo -e “n Both files have same attributesn”

cat file1

cat file2

}

fi

}

fi

[Request : If you are going to post this program on any other website please link back to original post ]