Unix Shell Scripts: 1a) Non Recursive Script, Which Prints Reversed Order of Args
Unix Shell Scripts: 1a) Non Recursive Script, Which Prints Reversed Order of Args
Unix Shell Scripts: 1a) Non Recursive Script, Which Prints Reversed Order of Args
output
1b) c program to create child process to read command from standard input and execute
them
#include<unistd.h>
#include<string.h>
#include<stdio.h>
#include<sys/types.h>
#define maxline 20
int main()
{
pid_t pid;
int status;
char buf[maxline];
pid=fork();
if(pid==0)
{
printf("Enter a valid UNIX command\n");
if(fgets(buf,maxline,stdin)!=NULL)
buf[strlen(buf)-1]=0;
system(buf);
}
pid=waitpid(pid,&status,0);
output:
2a) c program to create file with 16 bytes of ordinary data rom the beginning and other 16
bytes of ordinary data from an offset of 48
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
char s1[16]="0123456789012345";
char s2[16]="abcdefghijklmnop";
int fp;
fp=creat("a.dat",0);
write(fp,s1,16);
lseek(fp,48,SEEK_SET);
write(fp,s2,16);
system("chmod 777 a.dat");
system("od -bc a.dat");
output:
2b) c program that accepts valid filename as command line argument and print the type of
the file
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char*argv[])
{
struct stat buf;
int I=1;
if(argc==1)
{
printf("No Arguments");
}
else
{
do
{
lstat(argv[I],&buf);
if(S_ISREG(buf.st_mode))
printf("%s is Regular File\n",argv[I]);
else if(S_ISBLK(buf.st_mode))
printf("%s is Block File\n",argv[I]);
else if(S_ISCHR(buf.st_mode))
printf("%s is Charecter File\n",argv[I]);
else if(S_ISDIR(buf.st_mode))
printf("%s is Directory\n",argv[I]);
else if(S_ISFIFO(buf.st_mode))
printf("%s is FIFO File\n",argv[I]);
else if(S_ISLNK(buf.st_mode))
printf("%s is symbolic Link File\n",argv[I]);
else
printf("%s is Unknown File\n");
I++;
}while(I<argc);
}
}
output:
[user@localhost unix2]$ vi 2b.c
[user@localhost unix2]$ cc 2b.c
[user@localhost unix2]$ ./a.out m.c
m.c is Regular File
[user@localhost unix2]$ ./a.out 1b.c
1b.c is Regular File
3a)Script to echo args 1-per line, translating lower to upper case.
if [ $# -eq 0 ]
then
echo "Error - No args!"
exit
fi
for i
do
echo $i|tr '[a-z]' '[A-Z]'
done
output:
[user@localhost unix2]$ vi 3a.sh
[user@localhost unix2]$ sh 3a.sh
Error - No args!
[user@localhost unix2]$ sh a.sh bangalore
BANGALORE
#include<stdio.h>
#include<sys/types.h>
#include<sys/times.h>
#include<unistd.h>
output:
4a)Shell script to check file permission, process status, date & current user using case
conditional statement.
echo "Menu
1: list of files
2: process status
3: date
4: users
5: quit to terminal
enter ur choice:"
read choice
case "$choice" in
1) ls -l;;
2) ps;;
3) date;;
4) who;;
5) exit;;
*) echo "invalid entry";;
esac
output:
END{
{
for(i=1;i<=3;i++)
print a[i]
}
}
output
output:
if [ $# -ne 2 ]
then
echo "Error - 2 args required!";
exit;
fi
sort -o temp1 $1
sort -o temp2 $2
sort -m temp1 temp2
rm temp?
Output:
output
output:
[user@localhost unix2]$ vi 6b.sh
[user@localhost unix2]$ sh 6b.sh
Enter a Password