Вы находитесь на странице: 1из 18

# program to print the commandline arguments I reverse order echo the no of command line arguments is:$# echo the

command line arguments are:$@ len=$# str=$@ space=" " while test $len -ne 0 do temp=`echo $str | cut -d " " f $len` rev=$rev$temp$space len=`expr $len - 1` echo the reverse is $rev done echo the command in reverse order is:$rev ~ ~ ~ ~ ~ ~ ~ "reverse.sh" 14L, 283C @localhost ~]$ sh reverse.sh lin win dos the no of command line arguments is:3 the command line arguments are:lin win dos the reverse is dos the reverse is dos win the reverse is dos win lin the command in reverse order is:dos win lin [student6@localhost ~]$

8,1

All

#program to illustrate the given number is palindrome or not :echo enter the number read n len=`echo $n | wc -c` while test $len -ne 0 do temp=`echo $n | cut c $len` rev=$rev$temp len=`expr $len - 1` done if test $n -eq $rev then echo $n is pallindrome else echo $n is not pallindrome fi ~ "2.sh" 15L, 226C [student2@localhost ~]$ sh 2.sh enter the number 121 121 is pallindrome [student2@localhost ~]$sh 2.sh enter the number 325 325 is not palindrome [student2@localhost ~]$

#prgram to implment linear search or sequential search :echo enter the array element read n declare -a num[$n] i=0 while test $i -lt $n do read num[$i] i=`expr $i + 1` done echo enter the key read key flag=0 i=0 while test $i -lt $n do if test $key -eq ${num[$i]} then flag=1 break else i=`expr $i + 1` fi done if test $flag -eq 1 then echo successfull search else echo unsucessfull search fi

"linear.sh" 14L, 283C @localhost ~]$ sh linear.sh


Enter the number of element 5 4 5 6 7 8 Enter the key element 6 Search sucessfull

8,1

All

[student6@localhost ~]$

#program to illustrate the bubble sort:echo enter the number of element read n declare -a num[$n] i=0 while test $i -lt $n do read num[$i] i=`expr $i + 1` done i=1 j=0 while test $i -lt $n do while test $j -lt `expr $n - $i` do if test ${num[$j]} -gt ${num[`expr $n - $i`]} then temp=${num[$j]} num[$j]=${num[`expr $j + 1`]} num[`expr $j + 1`]=$temp fi j=`expr $j + 1` done i=`expr $i + 1` done echo sorted array is i=0 while test $i -lt $n do echo ${num[$i]} i=`expr $i + 1` done "bubble.sh" 32L, 283C @localhost ~]$ sh bubble.sh Enter the number of element 5 3 5 7 1 2 The sorted array is 1

32,1

All

2 3 5 7 [student6@localhost ~]$

#write a shell script to accept in two filename and to check file permission :for file in "$@" do if test ! -e $file then echo $file doesnt exit elif test ! -r $file then echo $file is not readable elif test ! -w $file then echo $file is not writeable else echo $file is both readable and writeable fi done ~ "5.sh" 16 lines, 231 characters @localhost ~]$ sh 5.sh 2 c 2 doesnt exit c doesnt exit @localhost ~]$

#write a shell script to accept valid login name if the login name is valid print it's home directory else print on appropriate message :if test $# -eq 2 then grep "$1" $2 || echo $1 is not found in $2 else echo not entred fi ~ ~ "6.sh" 13 lines, 96 characters @localhost ~]$ sh 6.sh student2 /etc/passwd @localhost ~]$

#write a shell script to read a file and change the existing file permission :echo enter the file name read file echo enter the content of $file cat >$file echo file permission $file before changing ls -l$file chmod ugo+r $file echo file permission $file after changing ls -l$file ~ ~ "7.sh" 11 lines, 205 characters @localhost ~]$ sh 7.sh enter the file name kk enter the content of kk hello shashi ctrl +d file permissionkk before changing total 64 -rw-r--r-- 1 vikash vikash 1 2012-03-23 15:58 2.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:22 5.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:27 6.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:30 7.sh -rw-r--r-- 1 vikash vikash 1 2012-03-25 22:29 c.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:31 kk -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:30 raj file permission kk after changing total 64 -rw-r--r-- 1 vikash vikash 1 2012-03-23 15:58 2.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:22 5.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:27 6.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:30 7.sh -rw-r--r-- 1 vikash vikash 1 2012-03-25 22:29 c.sh -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:31 kk -rw-r--r-- 1 vikash vikash 1 2012-03-28 16:30 raj @localhost ~]$

#write a shell script to illustrate the case statement :echo 1.user of the system echo 2.todays date echo 3.current month calender echo 4.list of files echo 5.quit echo enter the choice read choice case "$choice" in 1)who am i;; 2)date;; 3)cal;; 4)ls -l;; 5)exit;; *)echo you have entered wrong choice;; esac ~ ~ "8.sh" 18 lines, 257 characters @localhost ~]$ sh 8.sh 1.user of the system 2.todays date 3.current month calender 4.list of files 5.quit enter the choice 2 Wed Mar 28 16:40:12 EDT 2012 @localhost ~]$ sh 8.sh 1.user of the system 2.todays date 3.current month calender 4.list of files 5.quit enter the choice 3 March 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @localhost ~]$

#write a shell script to read a path name and create each element in that path :echo $1 | cat >e1 a1=`cut -d"/" -f1 e1` a2=`cut -d"/" -f2 e1` a3=`cut -d"/" -f3 e1` mkdir $a1 cd $a1 mkdir $a2 cd $a2 mkdir $a3 ~ ~ "9.sh" 10 lines, 129 characters @localhost ~]$ sh 9.sh student2/a1/a2 @localhost ~]$ ls -R .: 2.sh 5.sh 6.sh 7.sh 8.sh 9.sh c.sh Desktop Documents Downloads e1 examples.desktop kk Music Pictures Public raj student2 Templates Videos ./Desktop: pallindrome.png program 5 program 6 program 7 program 8 ./Documents: ./Downloads: ./Music: ./Pictures: ./Public: ./student2: a1 ./student2/a1: a2 ./student2/a1/a2: @localhost ~]$

#write a c program student name,reg_no,sem,age.find the elder of the age:#include<stdio.h> #include<malloc.h> struct student { char name[10]; char regno[10]; int sem; int age; }; int main() { int i,n,max,p; struct student s[10]; printf("enetre no of records"); scanf("%d",&n); printf("enter the records \n"); for(i=0;i<n;i++) { scanf("%s%s%d%d",s[i].name,s[i].regno,&s[i].sem,&s[i].age); } max=s[0].age; p=0; for(i=2;i<n;i++) { if(s[i].age>max) { max=s[i].age; p=i; } } printf("the students records"); printf("\n name=%s",s[p].namr); printf("\n regno=%s",s[p].regno); printf("\n sem=%d",s[p].sem); printf("\n age=%d",s[p].age); } ~ ~ ~ "student.c" 45 lines, 566 characters @localhost ~]$ gcc student.c

@localhost ~]$ $ ./a.out student.c enetre no of records2 enter the records shashi 454is 4 20 vikash 434is45 4 18 the students records name=shashi regno=454is sem=4 age=20 vikash@ubuntu:~$

#write a shell script program to implement the terminal locking and unlocking :echo "enter the password" stty -echo read pass1 echo "reenter the password" read pass2 if test $pass1 = $pass2 then tput clear echo "terminal is locked" echo "enter the password to unlock" read pass3 while test $pass1 != $pass3 do echo "enter the password to unlock" read pass3 done else echo "terminal is locked" fi stty echo ~ ~ "11.sh" 21 lines, 328 characters @localhost ~]$ sh 11.sh enter the password reenter the password terminal is locked enter the password to unlock enter the password to unlock enter the password to unlock enter the password to unlock @localhost ~]$

#write a shell script to display the menu consisting of following options :1.diskspace 2.current user 3.memory usages. function diskspace { clear df -k } function currentuser { clear who am i } function memoryusages { clear free -t } function menu { clear echo "menu" echo 1.display diskspace echo 2.current user echo 3.memory usages echo 4.exit from menu read choice } while test 1 -eq 1 do menu case "$choice" in 1)diskspace;; 2)currentuser;; 3)memoryusages;; 4)exit;; * )clear esac echo press any key to continue read option done ~ shashi13.sh 13l,404c @local host~]$sh shashi13.sh menu 1.diskspace

1,1 all

2.currentuser 3.memoryuser. 4.exit 2 Student2 pts/1 Apr 5 11:07 <192.168.1.200> Press any key to be continue 4 @local host~]$

#write a c program to prompt the user for name of the environment variable and check its validity and print on an appropriate message :#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> int main() { char a[10],*p; int ch=1; while(ch!=0) { printf("enter the name of environment variable"); scanf("%s",a); p=getenv(a); printf("%s",p); printf("want to change ? 1 for yes $0 for no"); scanf("%d",&ch); } return 0; } ~ ~ vikash@ubuntu:~$ gcc 14.c vikash@ubuntu:~$ ./a.out 14.c enter the name of environment variable HOME /home/vikashwant to change ? 1 for yes $0 for no 1 enter the name of environment variable SHELL /bin/bashwant to change ? 1 for yes $0 for no 1 enter the name of environment variable PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gameswant to change ? 1 for yes $0 fro no 0 vikash@ubuntu:~$

#write a c program to fork a child process and execute linux command :#include<stdio.h> #include<sys/types.h> main() { char* arg_list[]={"ls","-l","/",NULL}; int pid; pid=fork(); if(pid==0) { execvp("ls",arg_list); } } ~ ~ vikash@ubuntu:~$ gcc 15.c vikash@ubuntu:~$ ./a.out 15.c vikash@ubuntu:~$ total 108 drwxr-xr-x 2 root root 4096 2010-10-07 05:15 mnt drwxr-xr-x 2 root root 4096 2010-10-07 11:56 opt dr-xr-xr-x 185 root root 0 2012-04-04 10:31 proc -rw-r--r-- 1 vikash vikash 305 2012-04-04 15:02 14.c -rw-r--r-- 1 vikash vikash 153 2012-04-04 15:11 15.c -rw-r--r-- 1 vikash vikash 232 2012-04-04 07:45 2.sh -rw-r--r-- 1 vikash vikash 248 2012-04-02 08:33 a8.sh -rw-r--r-- 1 vikash vikash 219 2012-04-02 08:34 a9.sh -rwxr-xr-x 1 vikash vikash 7190 2012-04-04 15:11 a.out ls -l total 264 -rw-r--r-- 1 vikash vikash 534 2012-03-28 16:57 10.c -rw-r--r-- 1 vikash vikash 0 2012-03-29 19:40 10.sh -rw-r--r-- 1 vikash vikash 328 2012-03-28 17:10 11.sh -rw-r--r-- 1 vikash vikash 447 2012-04-04 14:38 12.sh -rw-r--r-- 1 vikash vikash 255 2012-04-04 14:52 13.c -rw-r--r-- 1 vikash vikash 574 2012-04-03 21:26 n10.c -rw-r--r-- 1 vikash vikash 103 2012-04-01 22:53 n6.sh -rw-r--r-- 1 vikash vikash 212 2012-04-01 22:48 n7.sh -rw-r--r-- 1 vikash vikash 250 2012-04-01 22:41 n8.sh -rw-r--r-- 1 vikash vikash 0 2012-04-03 21:11 r10.sh -rw-r--r-- 1 vikash vikash 301 2012-04-04 00:01 r1.sh -rw-r--r-- 1 vikash vikash 337 2012-04-04 00:22 r2.sh -rw-r--r-- 1 vikash vikash 6 2012-04-04 00:38 r4.sh -rw-r--r-- 1 vikash vikash 105 2012-04-03 20:49 r6.sh -rw-r--r-- 1 vikash vikash 0 2012-04-03 20:55 rej -rw-r--r-- 1 vikash vikash 254 2012-03-29 19:49 r.sh -rw-r--r-- 1 vikash vikash 3 2012-04-03 21:01 shashi -rw-r--r-- 1 vikash vikash 0 2012-04-01 22:49 shashi3 -rw-r--r-- 1 vikash vikash 335 2012-04-01 10:11 shashi3.sh -rw-r--r-- 1 vikash vikash 441 2012-04-01 10:10 shashi4.sh drwxr-xr-x 4 vikash vikash 4096 2012-04-04 07:40 student -rw-r--r-- 1 vikash vikash 560 2012-03-28 17:18 student1.c drwxr-xr-x 3 vikash vikash 4096 2012-03-28 16:45 student2 -rw-r--r-- 1 vikash vikash 566 2012-04-01 22:21 student.c -rw-r--r-- 1 vikash vikash 291 2012-04-01 09:34 swm1.sh

-rw-r--r--rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x

1 1 1 1 2

vikash vikash vikash vikash vikash

vikash 236 vikash 324 vikash 463 vikash 229 vikash 4096

2012-04-01 2012-04-01 2012-04-01 2012-04-01 2012-03-22

09:31 10:21 10:19 09:27 14:58

swm2.sh swm3.sh swm4.sh swmm.sh Templates

vikash@ubuntu:~$

#write a shell script program to implement binary search :echo enter the number read n declare -a num[$n] echo enter the array element i=0 while test $i -lt $n do read num[$i] i=`expr $i+ 1` done low =0 high=`expr $n - 1` echo enter the key read key while test $high -ge $low do a=`expr $high + $low` mid=`expr $a / 2` if test ${num[$mid]} -eq $key then echo element is found at `expr $mid + 1` break fi if test $key -gt ${num[$mid]} then low=`expr $mid + 1` else high=`expr $mid - 1` fi done if test $key -ne ${num[$mid]} then echo element is not found fi ~ binary.sh 35l,500c

1,1 all

@local host~]$sh binary.sh enter the number 3 enter the array element 30 20 10 enter the key 20 element is found at 2 @local host~]$

Вам также может понравиться