Linux Shell Programs - PartB

download Linux Shell Programs - PartB

of 18

Transcript of Linux Shell Programs - PartB

  • 7/22/2019 Linux Shell Programs - PartB

    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 0dotemp=`echo $str | cut -d " "f $len`rev=$rev$temp$spacelen=`expr $len - 1`echo the reverse is $revdone

    echo the command in reverse order is:$rev

    ~~~~~~~"reverse.sh" 14L, 283C 8,1 All

    @localhost ~]$ sh reverse.sh lin win dosthe no of command line arguments is:3the command line arguments are:lin win dosthe reverse is dosthe reverse is dos winthe reverse is dos win linthe command in reverse order is:dos win lin[student6@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    2/18

    #program to illustrate the given number is palindrome or not :-echo enter the numberread n

    len=`echo $n | wc -c`while test $len -ne 0dotemp=`echo $n | cutc $len`rev=$rev$templen=`expr $len - 1`doneif test $n -eq $revthenecho $n is pallindromeelse

    echo $n is not pallindromefi~"2.sh" 15L, 226C[student2@localhost ~]$ sh 2.shenter the number121121 is pallindrome[student2@localhost ~]$sh 2.shenter the number325325 is not palindrome[student2@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    3/18

    #prgram to implment linear search or sequential search :-

    echo enter the array elementread ndeclare -a num[$n]

    i=0while test $i -lt $ndoread num[$i]i=`expr $i + 1`doneecho enter the keyread keyflag=0i=0while test $i -lt $ndo

    if test $key -eq ${num[$i]}thenflag=1breakelsei=`expr $i + 1`fidoneif test $flag -eq 1thenecho successfull searchelse

    echo unsucessfull searchfi

    "linear.sh" 14L, 283C 8,1 All

    @localhost ~]$ sh linear.shEnter the number of element545678

    Enter the key element6Search sucessfull

    [student6@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    4/18

    #program to illustrate the bubble sort:-

    echo enter the number of elementread ndeclare -a num[$n]

    i=0while test $i -lt $ndoread num[$i]i=`expr $i + 1`donei=1j=0while test $i -lt $ndowhile test $j -lt `expr $n - $i`

    doif test ${num[$j]} -gt ${num[`expr $n - $i`]}thentemp=${num[$j]}num[$j]=${num[`expr $j + 1`]}num[`expr $j + 1`]=$tempfij=`expr $j + 1`donei=`expr $i + 1`doneecho sorted array isi=0while test $i -lt $ndoecho ${num[$i]}i=`expr $i + 1`done"bubble.sh" 32L, 283C 32,1 All

    @localhost ~]$ sh bubble.shEnter the number of element535712The sorted array is1

  • 7/22/2019 Linux Shell Programs - PartB

    5/18

    2357[student6@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    6/18

    #write a shell script to accept in two filename and to check file permission :-

    for file in "$@"doif test ! -e $file

    thenecho $file doesnt exitelif test ! -r $filethenecho $file is not readableelif test ! -w $filethenecho $file is not writeableelseecho $file is both readable and writeablefi

    done~"5.sh" 16 lines, 231 characters@localhost ~]$ sh 5.sh 2 c2 doesnt exitc doesnt exit@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    7/18

    #write a shell script to accept valid login name if the login name is valid print it's home directoryelse print on appropriate message :-

    if test $# -eq 2then

    grep "$1" $2 || echo $1 is not found in $2elseecho not entredfi~~"6.sh" 13 lines, 96 characters@localhost ~]$ sh 6.sh student2 /etc/passwd

    @localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    8/18

    #write a shell script to read a file and change the existing file permission :-

    echo enter the file nameread fileecho enter the content of $file

    cat >$fileecho file permission $file before changingls -l$filechmod ugo+r $fileecho file permission $file after changingls -l$file~~"7.sh" 11 lines, 205 characters@localhost ~]$ sh 7.shenter the file name

    kkenter the content of kkhello shashictrl +dfile permissionkk before changingtotal 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 rajfile permission kk after changingtotal 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 ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    9/18

    #write a shell script to illustrate the case statement :-

    echo 1.user of the systemecho 2.todays dateecho 3.current month calender

    echo 4.list of filesecho 5.quitecho enter the choiceread choicecase "$choice" in1)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.sh1.user of the system2.todays date3.current month calender4.list of files5.quitenter the choice2Wed Mar 28 16:40:12 EDT 2012@localhost ~]$ sh 8.sh1.user of the system2.todays date3.current month calender4.list of files5.quitenter the choice3

    March 2012Su Mo Tu We Th Fr Sa

    1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    10/18

    #write a shell script to read a path name and create each element in that path :-

    echo $1 | cat >e1a1=`cut -d"/" -f1 e1`a2=`cut -d"/" -f2 e1`

    a3=`cut -d"/" -f3 e1`mkdir $a1cd $a1mkdir $a2cd $a2mkdir $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 kkMusic 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 ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    11/18

    #write a c program student name,reg_no,sem,age.find the elder of the age:-

    #include#includestruct 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

  • 7/22/2019 Linux Shell Programs - PartB

    12/18

    @localhost ~]$ $ ./a.out student.cenetre no of records2enter the recordsshashi454is

    420vikash434is45418the students recordsname=shashiregno=454issem=4age=20

    vikash@ubuntu:~$

  • 7/22/2019 Linux Shell Programs - PartB

    13/18

    #write a shell script program to implement the terminal locking and unlocking :-

    echo "enter the password"stty -echoread pass1

    echo "reenter the password"read pass2if test $pass1 = $pass2thentput clearecho "terminal is locked"echo "enter the password to unlock"read pass3while test $pass1 != $pass3doecho "enter the password to unlock"

    read pass3doneelseecho "terminal is locked"fistty echo~~"11.sh" 21 lines, 328 characters@localhost ~]$ sh 11.shenter the password

    reenter the password

    terminal is lockedenter the password to unlockenter the password to unlockenter the password to unlockenter the password to unlock@localhost ~]$

  • 7/22/2019 Linux Shell Programs - PartB

    14/18

    #write a shell script to display the menu consisting of following options :-1.diskspace 2.current user 3.memory usages.

    function diskspace{

    cleardf -k}function currentuser{clearwho am i}function memoryusages{clear

    free -t}function menu{clearecho "menu"echo 1.display diskspaceecho 2.current userecho 3.memory usagesecho 4.exit from menuread choice}while test 1 -eq 1domenucase "$choice" in1)diskspace;;2)currentuser;;3)memoryusages;;4)exit;;* )clearesacecho press any key to continueread optiondone~shashi13.sh 13l,404c 1,1 all@local host~]$sh shashi13.shmenu1.diskspace

  • 7/22/2019 Linux Shell Programs - PartB

    15/18

    2.currentuser3.memoryuser.4.exit2Student2 pts/1 Apr 5 11:07

    Press any key to be continue4@local host~]$

    #write a c program to prompt the user for name of the environmentvariable and check its validity and print on an appropriate message :-

    #include#include#include#include

    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.cvikash@ubuntu:~$ ./a.out 14.center the name of environment variableHOME/home/vikashwant to change ? 1 for yes $0 for no1enter the name of environment variableSHELL/bin/bashwant to change ? 1 for yes $0 for no1enter the name of environment variablePATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gameswantto change ? 1 for yes $0 fro no0vikash@ubuntu:~$

  • 7/22/2019 Linux Shell Programs - PartB

    16/18

    #write a c program to fork a child processand execute linux command :-

    #include#includemain(){char* arg_list[]={"ls","-l","/",NULL};int pid;pid=fork();if(pid==0){execvp("ls",arg_list);}}~~vikash@ubuntu:~$ gcc 15.cvikash@ubuntu:~$ ./a.out 15.c

    vikash@ubuntu:~$ total 108drwxr-xr-x 2 root root 4096 2010-10-07 05:15 mntdrwxr-xr-x 2 root root 4096 2010-10-07 11:56 optdr-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.outls -ltotal 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.shdrwxr-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.cdrwxr-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

  • 7/22/2019 Linux Shell Programs - PartB

    17/18

    -rw-r--r-- 1 vikash vikash 236 2012-04-01 09:31 swm2.sh-rw-r--r-- 1 vikash vikash 324 2012-04-01 10:21 swm3.sh-rw-r--r-- 1 vikash vikash 463 2012-04-01 10:19 swm4.sh-rw-r--r-- 1 vikash vikash 229 2012-04-01 09:27 swmm.shdrwxr-xr-x 2 vikash vikash 4096 2012-03-22 14:58 Templates

    vikash@ubuntu:~$

    #write a shell script program to implement binary search :-

    echo enter the numberread ndeclare -a num[$n]echo enter the array elementi=0while test $i -lt $n

    doread num[$i]i=`expr $i+ 1`donelow =0high=`expr $n - 1`echo enter the keyread keywhile test $high -ge $lowdoa=`expr $high + $low`

    mid=`expr $a / 2`if test ${num[$mid]} -eq $keythenecho element is found at `expr $mid + 1`breakfiif test $key -gt ${num[$mid]}thenlow=`expr $mid + 1`elsehigh=`expr $mid - 1`

    fidoneif test $key -ne ${num[$mid]}thenecho element is not foundfi~binary.sh 35l,500c 1,1 all

  • 7/22/2019 Linux Shell Programs - PartB

    18/18

    @local host~]$sh binary.sh

    enter the number3enter the array element

    302010enter the key20element is found at 2@local host~]$