Number sig




Дата канвертавання26.04.2016
Памер30.1 Kb.

DA155

Lab. 5


Name: _____________________________


Here is a table of some of the common interrupts:

Number

SIG

Meaning

0

0

On exit from shell

1

SIGHUP

Clean tidyup

2

SIGINT

Interrupt

3

SIGQUIT

Quit

6

SIGABRT

Abort

9

SIGKILL

Die Now (cannot be trapped)

14

SIGALRM

Alarm Clock

15

SIGTERM

Terminate

Create the script as file lab5a.sh in your bin directory

#!/bin/bash


trap 'increment' 2
increment()

{

echo "Caught SIGINT ..."



X=`expr ${X} + 500`

if [ "${X}" -gt "2000" ]

then

echo "Okay, I'll quit ..."



exit 1

fi

}


### main script

X=0


while :

do

echo "X=$X"



X=`expr ${X} + 1`

sleep 1


done

What does this script do? _______________________________________________

How can one hasten the exit of this script? __________________________________
Create the following script in your bin directory and call it lab5b.sh
trap 'echo "Control-C disabled."' SIGINT

trap 'echo "Control-Z disabled."' SIGTSTP

while true

do

clear



echo "Server Name - $(hostname)"

echo "-------------------------------"

echo " M A I N - M E N U"

echo "-------------------------------"

echo "1. Display date and time."

echo "2. Display what users are doing."

echo "3. Display network connections."

echo "4. Exit"

read -p "Enter your choice [ 1 -4 ] " choice

case $choice in

1)

echo "Today is $(date)"



read -p "Press [Enter] key to continue..." readEnterKey

;;

2)



w

read -p "Press [Enter] key to continue..." readEnterKey

;;

3)

netstat -nat



read -p "Press [Enter] key to continue..." readEnterKey

;;

4)



echo "Bye!"

exit 0


;;

*)

echo "Error: Invalid option..."



read -p "Press [Enter] key to continue..." readEnterKey

;;

esac



done
Try to interrupt the script in action by using ctrl-c and ctrl-z signals.

Describe the result Ctrl-c __________________________

Ctrl-z __________________________

Create the following script in your bin directory as lab5c.sh

#!/bin/bash

times=0


n=0

warning(){

echo -e "\n*** CTRL+C and CTRL+Z keys are disabled. Please enter number only. Enter integer then hit [Enter] key to continue..."

}

trap 'warning' SIGINT SIGQUIT SIGTSTP



while true

do

read -p "Enter number (-9999 to exit) : " n



[ $n -eq -9999 ] && { echo "Bye!"; break; }

[ $n -eq 0 ] && continue


# % is the modulus operatore it returns the remainder of integer division

ans=$(( n % 2 ))

[ $ans -eq 0 ] && echo "$n is an even number." || echo "$n is an odd number."

times=$(( ++times ))

done

trap - SIGINT SIGQUIT SIGTSTP



echo "You played $times times."

exit 0


Describe the operation of this script: ____________________________________________

Create the script as file lab5d.sh in your bin directory. This is another version of the recursive function factoral.

#!/bin/bash

factorial() {

local i=$1

local f


[ $i -le 2 ] && echo $i || { f=$(( i - 1 )); f=`factorial $f`; f=$(( f * i )); echo $f; } }

[ $# -eq 0 ] && { echo "Usage: $0 number"; exit 1; }

result=`factorial $1`

echo Factorial of $1 = $result


Test the script on the following numbers:

15! = ____________________________

75! = ___________________________

Create a script using the following function definition in your bin directory call it lab5e.sh

is_file_dir(){

# $f is local variable

local f="$1"

# file attributes comparisons using test i.e. [ ... ]

[ -f "$f" ] && { echo "$f is a regular file."; }

[ -d "$f" ] && { echo "$f is a directory."; }

[ -L "$f" ] && { echo "$f is a symbolic link."; }

[ -x "$f" ] && { echo "$f is an executable file."; }

}
add extra code to process all the files in the current directory when the script file is run i.e. ./lab5e.sh

Create a script using the following function definition in your bin directory, call it lab5f.sh.


hello() { echo "Hello $1, let us be a friend." ; }

add code to prompt for a person's name then pass it to the function and have the message appear on the screen.


База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка