Some simple examples

52
Some simple examples • First, second, third(with errors) third2

description

Some simple examples. First, second, third(with errors) third2. echo Command. Use echo command to display text or value of variable. - PowerPoint PPT Presentation

Transcript of Some simple examples

Page 1: Some simple examples

Some simple examples

• First, second, third(with errors) third2

Page 2: Some simple examples

echo Command• Use echo command to display text or value of variable.• echo [options] [string, variables...]

Displays text or variables value on screen.Options-n Do not output the trailing new line.-e Enable interpretation of the following backslash escaped characters in the strings:\a alert (bell)\b backspace\c suppress trailing new line\n new line\r carriage return\t horizontal tab\\ backslash

Page 3: Some simple examples

Example

• $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

Page 4: Some simple examples

More about echo

• Display colorful messages

• echo “Hello, world!”

• echo -e "\033[34m   Hello Colorful  World."

Page 5: Some simple examples

• That echo statement uses ANSI escape sequence (\033[34m), above entire string ( i.e.  "\033[34m   Hello Colorful  World." ) is process as follows

• 1) First \033, is escape character, which causes to take some action.

• 2) Here it set screen foreground color to Blue using [34m escape code.

• 3) Then it prints our normal message Hello Colorful  World! in blue color.

Page 6: Some simple examples

If we want to display that echo statement in red

• echo -e "\033[31m   Hello Colorful  World.“

• In general:

• Syntaxecho   -e  "\033[escape-code    your-message"

• More detail, see file cs315\Echo_sequence

Page 7: Some simple examples

Shell Arithmetic

• Use to perform arithmetic operations.• Syntax:

expr op1 math-operator op2

Examples: $ expr 1 + 3$ expr 2 - 1$ expr 10 / 2$ expr 20 % 3$ expr 10 \* 3$ echo `expr 6 + 3`

Page 8: Some simple examples

Double quote, single quote, and back quote

• echo “Today is `date`”

• Double quote remove the meaning of the characters except “$” and “\”.

• echo ‘Today is `date`’

• Enclosed in single quotes remains unchanged

• echo `date` To execute command

Page 9: Some simple examples

The read Statement

• Use to get input (data from user) from keyboard and store (data) to variable.Syntax: read variable1, variable2,...variableN

Page 10: Some simple examples

Example

• ##Script to read your name from key-board#echo "Your first name please:"read fnameecho "Hello $fname, Lets be friend!"

Page 11: Some simple examples

More Unix Commands

• nohup + command will make it immune to the hanup and terminate signal.

• Example: nohup sleep 30&

• kill + pid to kill a process

• wait + pid will wait the process to finish.

• wait – without pid will wait all its child processes to finish.

Page 12: Some simple examples

env command• Displays your UNIX environment variables.

• env enter

Page 13: Some simple examples

Variable assignment

• Variables are assigned in a script program as follows:

• DONE=no

• They are used in this manner:

• while [ $DONE = no ] in your script

Page 14: Some simple examples

looping

• Example: looping –week13

Page 15: Some simple examples

• #! /bin/bash• DONE=no• ENTRIES="hello bye ls 1"• while [ $DONE = no ]• do• echo Valid entries are: $ENTRIES• read ENTRY # Read the variable ENTRY from the user• case $ENTRY in• 1)• pwd• ;;• hello)• echo How are you?• ;;• bye)• echo exiting...• DONE=yes• ;;• ls)• ls -al |more• ;;• *)• echo $ENTRY is an unrecognized command.• ;;• esac• done

Page 16: Some simple examples
Page 17: Some simple examples

/bin and Running Shell Script

• To run a script, we can:

• 1. ./script_Name

• 2 . script_Name

• 3. sh script_Name

• In fact, we can run the script with some other ways.

Page 18: Some simple examples

The difference between . Script_Name and ./script_Name

• You can also run a script by using:

• bash script_Name enter

• The bash expects input from a file, you do not need execute permissions; the same is true for . Script_Name

Page 19: Some simple examples
Page 20: Some simple examples

Create a bin directory

mkdir bin under your home directory. From your current directory, Type: cp script_name ../bin to copy the script into /bin directory. Or, cp * ../bin to copy all your files to the

bin directory.Now, you can run them directly by type in the

script_Name.

Page 21: Some simple examples

Example

• Run looping script under current directory

• cp looping ../bin enter

• Type looping enter

• This time, you can type in the script name and run it directly.

Page 22: Some simple examples

chsh –Change Shell

• chsh enter

• Type in your password enter

• Type /bin/ksh will change your login shell to Korn shell

Page 23: Some simple examples

What Is Korn Shell?

– The Korn shell (/bin/ksh) is the most advanced 'standard' UNIX shell. It extends the Bourne shell with lots of nice features, and is a lot faster.

Page 24: Some simple examples

Korn Shell

• It was written by David Korn.

• It is a powerful superset of the Bourne shell.

• The improvements include :

• 1. Job control

• 2. Command line editing

• 3. Programming features.

Page 25: Some simple examples

Korn Shell

Korn is superset of Bourne. zsh claims to be an enhanced Korn shell, while bash has added some parts of it

Page 26: Some simple examples

Aliases in Korn shell

• The corn shell allows you to create your own commands by using the alias command.

• Example:• $ alias d= `ls –l` in standard Unix • Example in In our Linux• alias l=ls [Enter]• alias AA=“cal 2004”

Page 27: Some simple examples

Exception in using alias in Korn

• All built-in commands may be aliased except for:

• case, do, done, elif, else, esac, fi, for, function, if, select, then, time, until, while, {,}

Page 28: Some simple examples

Remove an alias-unalias

• The unalias will remove all of the specified aliases.

format: unalias aliasName [Enter]

Page 29: Some simple examples

Functions in Korn shell

• Korn shell allows one to define functions that may be invoked as shell commands.

• Two basic format to define a function in Korn shell:

• 1. Function name

• {

• list

• }

Page 30: Some simple examples

Function --- cont.

• 2. Name ( )

• {

• list

• }

• This format looks similar to C language format.

Page 31: Some simple examples

Example • f ( )

• {

• echo parameter l = $1

• echo parameter list = $*

• }

• # main program

• f l

• f cat dog goat

• (week13 func6)

Page 32: Some simple examples

Enhanced job control

• Command :

• jobs --- Display current jobs

• bg %job# --- resumes the specified job as

• a background process.

• fg %job# --- resumes the specified job as

• the foreground process.

• kill

Page 33: Some simple examples

TILDE “~” substitution

• ~ --- $HOME

• ~user --- home directory of user

• ~/pathname --- $HOME pathname

• ~+ --- $PWD (current working directory)

• ~- --- $OLDPWD (previous working directory)

Page 34: Some simple examples

More

• let x=y+z written as ( ( x = y + z ))

• select - allows use of simple menus

• functions can be written with parameters

• f( )

• {

• echo $1 $2 $3

• } called by f cat dog cow

Page 35: Some simple examples

Kermel

• 1. The kernel itself is not a process but a

• process manager.

• 2. System calls are some specific program constructs that perform the kernel service required by processes.

• 3. Each system call sets up the group of parameters that identifies the process request and then executes the hardware-dependent CPU instructions to switch from User Mode to Kernel Mode.

Page 36: Some simple examples

Kernel thread---Privileged processes

• Besides user processes, Unix systems include a few privileged processes

• ---Kernel thread.

• 1. They run Kernel Mode in the kernel address space.

• 2. They do not interact with users, and thus do not require terminal devices.

• 3. They usually created during startup and remain alive until the system is shut down.

Page 37: Some simple examples

Transition between User Mode and Kernel Mode

User Mode

Kernel Mode

• .

Process1 Process1 Process2 Process2

System call Timer interrupt

Deviceinterrupt

System callhandler

SchedulerInterrupt handler

Page 38: Some simple examples

Process Implementation

• To let the kernel manage processes, each process is represented by a process descriptor that includes information about the current state of the process.

• When kernel stops the execution of a process, it saves the current contents of several processor registers in the process descriptor. These include: PC, SP, General-purpose registers, floating point registers, processor control registers,

• and the memory management registers.

Page 39: Some simple examples

Device Drivers• The kernel interacts with I/O devices by

means of device drivers and each driver interacts with the remaining part of the kernel (even with other drivers) through a specific interface. The advantages are:

• 1. Device-specific code can be encapsulated in a specific module.

• 2. Vendors can add new devices without knowing the kernel source code: only the interface specifications must be know.

• 3. The kernel deals with all devices in a uniform way and accesses them through the same interface.

• 4. Dynamically load and unload device drivers to minimize the kernel image stored in RAM.

Page 40: Some simple examples

Kernel

Kernel Basics

• 1. Sharing the CPU and RAM between competing progresses

• 2. Processing all system calls• 3. Handling peripherals• It mostly written in C, some parts of the

kernel are written in assembly language• Users only interact with the kernel by

system calls interface

Page 41: Some simple examples

Device Driver Interface

tty tty Tape Disk

System call interface

Virtual File System

Character device files Block device filesKernel

ttydriver

Tapedriver

Disk driver

P P PP

Disk

Page 42: Some simple examples

Kernel --- cont.

Kernel Subsystems

• 1. Memory Management

• 2. Process Management

• 3. Inter-process Communication

• 4. Input/Output

• 5. File Management

Page 43: Some simple examples

Kernel, hardware, and software• .

Kernel

Peripheral Peripheral Peripheral

Process Process Process

System calls

Hardware interrupt

Page 44: Some simple examples

User Mode and Kernel Mode

• Normally, when a user process is running, it operates in a special machine mode called user mode.

• The only way for a user process to enter kernel mode is to execute a system call.

Page 45: Some simple examples

System calls

• .Open a file open

Close a file close

Perform I/O read/write

Send a signal kill

Create a pipe pipe

Create a socket socket

Duplicate a process fork

Terminate a process exit

Page 46: Some simple examples

Hardware interrupt

• . Interrupt vector table

0

1

2

3

4

Highestpriority

Lowestpriority

Hardware errors

Clock

Disk I/O

Keyboard

Traps (software interrupt)

Pointers to kernelInterruptHandlers

Page 47: Some simple examples

Interrupt can be interrupted!

• Because of the priority, a lower priority interrupt can be interrupted by a higher priority interrupt.

Page 48: Some simple examples

Screen saver example• # name: lock

• # function: lock the screen until a correct password is entered

• #

• clear

• echo -e "\n\nEnter your password>"

• read pword_1

• clear

• echo -e "\n\n This system is locked ..... "

• pword_2=

• until [ "$pword_1" = "$pword_2" ]

• do

• read pword_2

• done

• clear

• echo "The password you typed in is correct! You can use the computer now!“ week14 lock1

Page 49: Some simple examples

Problems with the lock1

• 1. Password being displayed while enter it.

• 2. There are some ways one can enter the system without password.

• Such as ctrol+c …

• The reason is it can cause an interrupt to the kernel and the kernel knows about the device. The kernel interrupt has higher priority than your process.

Page 50: Some simple examples

Some of the shell signals

• Signal # Name Meaning

• 1 hang up Terminal connection lost

• 2 interrupt One of the interrupt key

• 3 quit One of the quit key

• 9 kill The kill –9 has been issued

• 15 terminator The kill command has been

• issued.

Page 51: Some simple examples

trap command

• We can use the trap command to disable the signals created by the kernel.

• Basically, it change the process default action to whatever you specify.

• Example:

• trap “optional commands” signal numbers

• Trap “echo I refuse to die!” 15 display the message instead of terminate the process.

• (week14 lock3 use the trap command)

Page 52: Some simple examples

Trap cont…

• If you write trap “ “ [signal] numbers

• The system just ignore the signals.