Sie sind auf Seite 1von 22

P E S COLLEGE OF ENGINEERING, MANDYA-571401.

[An Autonomous Institution Under VTU, Belguam]

Department of Electronics and Communication Engineering.

A Lab manual for

LINUX AND EMBEDDED SYSTEM PROGRAMMING LABORATORY

Prepared by, GOKUL B S th 7 semester (2011-12) Dept. of E and C

SHELL
The shell is not a part of the operating system, it is just a C program to provide the interface between the user and the operating system. The shell commands given at the $ prompt can be grouped together and executed in batch mode. This is known as shell scripting. The shell allows C like structures such as if-then-else, while-do done, for-do-done, etc.

SOME IMPORTANT INSTRICTIONS BEFORE START-UP


1. 2. 3. 4. 5. 6. 7. To execute a shell script one has to follow the following steps. Open Terminal Type vi <file name>.sh VI editor will be opened. Press 'i' to go to insert mode. Type the shell script program. Press 'esc'. Type :w to save. Press :q to quit. Type sh <filename.sh>.sh in terminal to execute along with arguments if present.

1. Non-recursive shell script that accepts any number of arguments and prints them in the reverse order. (For example, if the script rargs, then executing args A B C should produce C B A on the standard output) echo "no of arguments are : $#" a=$# while [ $a -ne 0 ] do eval echo \$$a a=`expr $a - 1` done
RESULT: File name nonrecu.sh

2.a) Shell script to find the largest of two numbers. echo "largest of 2nos." echo "enter a no." read x echo "enter another" read y if [ $x -gt $y ];then echo "$ x is large" elif [ $y -gt $x ];then echo "$y is large" else echo "$x is equal to $y" fi Result: File name great.sh

2.b) Shell script to find the largest of three numbers. echo "enter a number" read x echo "enter a number" read y echo "enter third number" read z if [ $x -gt $y ]; then if [ $x -gt $z ]; then echo "$x is large" fi elif [ $y -gt $z ]; then echo "$y is large" else

echo "$z is large" fi Result: File name is large3.sh

3. Shell script to delete a name from the given file. cat file1 echo "Enter the name to be deleted:" read a grep -v $a file1 > file3 mv file3 file1 cat file1 Result: File name is q1.sh

4. Write a Shell script display the contents of directories/bin and /user/ bin.

for name in /bin /usr/bin do ls $name done Result: It displays the contents of directories /bin and /usr/bin

5. a) Write a Shell script prints whether each file in a directory is readable or writable. for word in `ls` do if test -r $word then echo "$word is readable" fi if test -w $word then echo "$word is writable" fi done Result: File name is readw.sh

5. b) Write a Shell script for file test whether the file is readable/ writable/executable. if [ ! -e $1 ];then echo "File does not exist" elif [ ! -r $1 ];then echo "File is not readable" elif [ ! -w $1 ];then echo "File is not writeable" elif [ ! -x $1 ];then echo "File is not executable" else echo "File is redable writeable and executable fi Result: File name is erw.sh

6. Write C program that creates a child process to read commands from the standard input and execute them ( a minimal implementation of a shell- like program ). you can assume that no arguments will be passed to the commands to be executed.

#include<stdio.h> #include<sys/types.h> int main() { char cmd[20]; pid_t pid; int p,p1; int ch; pid=fork(); p=getpid(); p1=getppid(); if(pid==0) { do { printf("\n enter the cmd to be executed: \n"); scanf("%s",cmd); system(cmd); printf("\n enter 1 to continue and 0 to exit: "); scanf("%d",&ch); }while(ch!=0); } printf("processid=%d\n",p); printf("parentid=%d\n",p1);

wait(); }
Result: File name is chexe.c

7. a) Shell scripts that accepts two file names as arguments, checks if the permissions for these files are identical, outputs the common permissions, otherwise outputs each file name followed by its permissions. ls -l $1 | cut -d " " -f 1 > x ls -l $2 | cut -d " " -f 1 > y if cmp x y then echo "both are equal" cat x else echo "differ" echo "first file $1" cat x echo "second file $2" cat y fi Result: File name is perm.sh

7. b) Shell function that takes a valid directory names as an argument and 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. echo "the max size of the file in directory $1 is: " ls -lR|grep '^-'|cut -c 25-26|sort -n|tail -1 Result: File name is maxsz.sh

8. Write a shell script to print the following shapes and other shapes.

FOR2:
echo "Can you see the following:" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "$i" done echo "" done

Result: File name is for2.sh

FOR3:
echo "Can you see the following:" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "$j" done echo ""

done Result: File name is for3.sh

FOR4:
echo "Stars" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " |" done echo "_"

done Result: File name is for4.sh

FOR5:
echo "Stars" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo ""

done Result: File name is for5.sh

FOR6:
echo "Stars" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done for (( i=5; i>=1; i-- )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo ""

done Result: File name is for6.sh

FOR7:
clear for (( i=1; i<=3; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "|Linux" done echo "______" done for(( i=3; i>=1; i-- )) do for (( j=1; j<=i; j++ )) do echo -n "|Linux" done

if [ $i -eq 3 ]; then echo -n "______" echo -n -e ">> Powerd Server.\n" else echo "~~~~~" fi done Result: File name is for7.sh

FOR8: MAX_NO=0 echo -n "Enter Number between (5 to 9) : " read MAX_NO if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "I ask to enter number between 5 and 9, Okay" exit 1 fi clear for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " $i" done echo "" done for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " " done for (( j=1; j<=i; j++ )) do

echo -n " ." done echo "" done Result: File name is for8.sh

FOR 8{ STARS }
MAX_NO=0 echo -n "Enter Number between (5 to 9) : " read MAX_NO if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "I ask to enter number between 5 and 9, Okay" exit 1 fi clear for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " $i" done echo "" done for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " "

done for (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done Result: File name is for82.sh

FOR 9:
MAX_NO=0 echo -n "Enter Number between (5 to 9) : " read MAX_NO if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "I ask to enter number between 5 and 9, Okay" exit 1 fi clear for (( i=1; i<=MAX_NO; i++ )) do for (( s=MAX_NO; s>=i; s-- )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done ###### Second stage ######################

## ## for (( i=MAX_NO; i>=1; i-- )) do for (( s=i; s<=MAX_NO; s++ )) do echo -n " " done for (( j=1; j<=i; j++ )) do echo -n " ." done echo "" done

Result: File name is for9.sh

9. Shell script that accepts path names and creates all the components in that path names as directives. 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. if [ $# -lt 1 ] then echo " NO ARGUMENTS" else echo $1 | tr "/" " " > temp for i in $temp do mkdir $i cd $i done echo "ALL DIRECTORY ARE CREATED" fi Result: File name is createpath.sh

10. Shell scripts that accepts valid log-in names as arguments and prints their corresponding home directories. If no arguments are specified, print suitable error message. if [ $# -lt 1 ] then echo "no arguments" else for i in $* do x=`cat /etc/passwd | cut -d ":" -f6 | grep -w "$i"` if [ -z $x ] then echo "there is no user of the name "$i else echo "home directory of $i is "$x fi done fi Result: File name is log.sh

11. Shell script to display the calendar for current month with current data replaced by * or ** depending on
whether has one digits.

n=`date +%d` cal >temp if [ $n -lt 10 ] then sed s/"$n"/*/g temp else sed s/"$n"/**/g temp fi Result: File name is datedisp.sh

12. Shell script that accepts file names specified as arguments and create a shell script that contains this file as well
as 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. Kerighan and Rob Pike in The Unix Programming Environment, Prentice Hall India).

echo '#to bundle,sh this file' for i in $* do echo "echo $i 1>&2" echo "cat>$i cat $i

STUDY PROGRAMS Q.1.Write Script to find out biggest number from given three nos. Nos are supplies as command line argument. Print error if sufficient arguments are not supplied. Q2. Script to find out biggest number # # Algo: # 1) START: Take three nos as n1,n2,n3. # 2) Is n1 is greater than n2 and n3, if yes # print n1 is biggest no goto step 5, otherwise goto next step # 3) Is n2 is greater than n1 and n3, if yes # print n2 is biggest no goto step 5, otherwise goto next step # 4) Is n3 is greater than n1 and n2, if yes # print n3 is biggest no goto step 5, otherwise goto next step # 5) END # # if [ $# -ne 3 ] then echo "$0: number1 number2 number3 are not given" >&2 exit 1 fi n1=$1 n2=$2 n3=$3 if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ] then echo "$n1 is Bigest number" elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ] then echo "$n2 is Bigest number" elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ] then echo "$n3 is Bigest number" elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ] then echo "All the three numbers are equal" else echo "I can not figure out which number is biger" fi

Q.2. How to write shell script that will add two nos, which are supplied as command line argument, and if this two nos are not given show error and its usage
Q1.Script to sum to nos if [ $# -ne 2 ] then

echo "Usage - $0 x y" echo " Where x and y are two nos for which I will print sum" exit 1 fi echo "Sum of $1 and $2 is `expr $1 + $2`"

Q.3.Write script to print given numbers sum of all digit, For eg. If no is 123 it's sum of all digit will be 1+2+3 = 6. Algo: # 1) Input number n # 2) Set sum=0, sd=0 # 3) Find single digit in sd as n % 10 it will give (left most digit) # 4) Construct sum no as sum=sum+sd # 5) Decrment n by 1 # 6) Is n is greater than zero, if yes goto step 3, otherwise next step # 7) Print sum # if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find sum of all digit for given number" echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)" exit 1 fi n=$1 sum=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` sum=`expr $sum + $sd` n=`expr $n / 10` done echo "Sum of digit for numner is $sum"

Q.4.Write Script to see current date, time, username, and current directory

echo "Hello, $LOGNAME" echo "Current date is `date`" echo "User is `who i am`" echo "Current direcotry `pwd`"

Q.5. Write shell script to implement menus using dialog utility. Menu-items and action according to select menu-item is as follows: Menu-Item Date/time Calendar Delete Purpose To see current date time To see current calendar To delete selected file Action for Menu-Item Date and time must be shown using infobox of dialog utility Calendar must be shown using infobox of dialog utility First ask user name of directory where all files are present, if no

Exit

To Exit this shell script

name of directory given assumes current directory, then show all files only of that directory, Files must be shown on screen using menus of dialog utility, let the user select the file, then ask the confirmation to user whether he/she wants to delete selected file, if answer is yes then delete the file , report errors if any while deleting file to user. Exit/Stops the menu driven program i.e. this script

Note: Create function for all action for e.g. To show date/time on screen create function show_datetime(). show_datetime()
{ dialog --backtitle "Linux Shell Tutorial" --title "System date and Time" --infobox "Date is `date`" 3 40 read return } show_cal() { cal > menuchoice.temp.$$ dialog --backtitle "Linux Shell Tutorial" --title "Calender" --infobox "`cat menuchoice.temp.$$`" 9 25 read rm -f menuchoice.temp.$$ return } delete_file() { dialog --backtitle "Linux Shell Tutorial" --title "Delete file"\ --inputbox "Enter directory path (Enter for Current Directory)"\ 10 40 2>/tmp/dirip.$$ rtval=$? case $rtval in 1) rm -f /tmp/dirip.$$ ; return ;; 255) rm -f /tmp/dirip.$$ ; return ;; esac mfile=`cat /tmp/dirip.$$` if [ -z $mfile ] then mfile=`pwd`/* else grep "*" /tmp/dirip.$$ if [ $? -eq 1 ] then mfile=$mfile/* fi fi for i in $mfile do if [ -f $i ] then echo "$i Delete?" >> /tmp/finallist.$$ fi done dialog --backtitle "Linux Shell Tutorial" --title "Select File to Delete"\ --menu "Use [Up][Down] to move, [Enter] to select file"\

20 60 12 `cat /tmp/finallist.$$` 2>/tmp/file2delete.tmp.$$ rtval=$? file2erase=`cat /tmp/file2delete.tmp.$$` case $rtval in 0) dialog --backtitle "Linux Shell Tutorial" --title "Are you shur"\ --yesno "\n\nDo you want to delete : $file2erase " 10 60 if [ $? -eq 0 ] ; then rm -f $file2erase if [ $? -eq 0 ] ; then dialog --backtitle "Linux Shell Tutorial"\ --title "Information: Delete Command" --infobox "File: $file2erase is Sucessfully deleted,Press a key" 5 60 read else dialog --backtitle "Linux Shell Tutorial"\ --title "Error: Delete Command" --infobox "Error deleting File: $file2erase, Press a key" 5 60 read fi else dialog --backtitle "Linux Shell Tutorial"\ --title "Information: Delete Command" --infobox "File: $file2erase is not deleted, Action is canceled, Press a key" 5 60 read fi ;; 1) rm -f /tmp/dirip.$$ ; rm -f /tmp/finallist.$$ ; rm -f /tmp/file2delete.tmp.$$; return;; 255) rm -f /tmp/dirip.$$ ; rm -f /tmp/finallist.$$ ; rm -f /tmp/file2delete.tmp.$$; return;; esac rm -f /tmp/dirip.$$ rm -f /tmp/finallist.$$ rm -f /tmp/file2delete.tmp.$$ return } while true do dialog --clear --title "Main Menu" \ --menu "To move [UP/DOWN] arrow keys \n\ [Enter] to Select\n\ Choose the Service you like:" 20 51 4 \ "Date/time" "To see System Date & Time" \ "Calender" "To see Calaender"\ "Delete" "To remove file"\ "Exit" "To exit this Program" 2> menuchoice.temp.$$ retopt=$? choice=`cat menuchoice.temp.$$` rm -f menuchoice.temp.$$ case $retopt in 0) case $choice in Date/time) show_datetime ;; Calender) show_cal ;; Delete) delete_file ;;

Exit) exit 0;; esac ;; 1) exit ;; 255) exit ;; esac done clear

Das könnte Ihnen auch gefallen