Shell - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Shell

Description:

Shell: An interpreter that accept input from the user and executes the command that are given ... Command line shell (CLI) bash, tcsh, ksh, csh (Linux), cmd (windows) ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 28
Provided by: You6164
Category:
Tags: cli | grep | shell

less

Transcript and Presenter's Notes

Title: Shell


1
Shell
  • Youngwoo Park
  • (ywpark_at_core.kaist.ac.kr, 5425)
  • September 10th, 2009
  • Computer Engineering Research Laboratory
  • Korea Advanced Institute of Science and Technology

2
Shell ?
  • Shell An interpreter that accept input from the
    user and executes the command that are given
  • Interface between user and the OS
  • Shell is not a OS component
  • Shell is application for OS

3
Which shell?
  • Command line shell (CLI)
  • bash, tcsh, ksh, csh (Linux),
  • cmd (windows)
  • Graphical user interface (GUI)
  • Gnome, KDE (Linux),
  • Windows, Mac

4
What does the shell do?
  • Command processing
  • The basic role of shell
  • I/O redirection
  • Change the source and destination of I/O
  • Piping
  • Piping two command
  • Environment control
  • Control execution path, shell prompt, alias
  • Job control
  • Foreground, Background job
  • Shell scripts(programming)

5
Command line format
  • Command -option arguments
  • Ls h myfile
  • tokens

6
Command processing
  • Read what you type at the prompt
  • Parse it (command/options/args )
  • Use space/tab/cr for parsing
  • Optionally expands(transforms) it
  • Execute the command
  • Program Execution or Emitting error message

7
Command processing example
  • cp i /home/ywpark/myfile yourfile
  • cp command ? copy file
  • -ioption ? prompt before overwrite
  • /home/ywpark/myfile argument ? source file name
  • yourfile argument ? destination file name
  • ls h myfile gt filename
  • ls command ? list-up file name and file
    characteristics
  • -h option ? print sizes in human readable format
  • myfile argument (need expansion) ? print file
    whose names start as myfile
  • gt redirection ? redirect I/O
  • filename argument for redirection ? redirected
    file I/O name

8
Expansion
  • Filename expansion
  • stands in any 0 or more characters
  • ls /home/ywpark/ list-up all files in
    /home/ywpark directory
  • ? stands in any single character
  • ls ?o list-up file go, do, ho
  • 1-6 stands in for one of 1,2,3,4,5,6
  • ls lab1-6 list-up file lab1, lab2, lab3, lab4,
    lab5, lab6
  • oa stands in for any char except o or a
  • ls .oa list-up file .b, .c, .d, , .n,
    .p,
  • / stands in for your home directory
  • ls /bin list-up file in bin of your home
    directory
  • ywpark stands in for ywparks home directory
  • Ls ywpark/bin list-up file in bin of ywparks
    home directory

9
Filename expansion example
10
Transformation example
  • Transformation Use metacharacters ()ltgt
  • It is very useful for shell programming

Ref.) homepage.smc.edu/morgan_david/linux/a01-shel
l.pdf
11
Transformation example
Ref.) homepage.smc.edu/morgan_david/linux/a01-shel
l.pdf
12
IO redirection
  • Four I/O candidate
  • Standard input, Standard output, Standard error,
    File
  • Default get an input from standard input device,
    put an output to standard output device, put an
    error message to standard error device
  • Redirection
  • Use file for input lt,
  • Use file for output gt, 2gt (stderr), gt
    (stdoutstderr), gtgt(append to the file)
  • Example
  • cat source gtgt results
  • Append the contents of source to file results
  • cp abc 2gt err
  • Print error message to err file

13
I/O redirection example
14
Pipe
  • Allow multiple command to be connected
  • Send the output from one command to the input of
    the next
  • Example
  • ls -h grep abc

15
Pipe example
16
Environment control
  • Variables
  • Environment variables
  • Get passed to child processes
  • Local variables
  • Dont get passed
  • Environment control
  • Setup variables
  • SHELL decide shell
  • PATH command search path
  • PS1 shell prompt setting
  • Execute built-in commands after login
  • alias set up the aliases

17
(No Transcript)
18
Job control
  • Foreground job
  • A program which has control of the terminal
  • Background job
  • Runs concurrently with the parent shell and does
    not take control of the keyboard
  • Job control command
  • jobs list-up active jobs
  • bg resume job in the background
  • fg resume job in the foreground
  • kill kill the job(process)
  • execute job in the background
  • User can interact with shell after background job
    is executed

19
Job control example
  • Example
  • ls h
  • sort bigfile

20
Shell script?
  • File containing a list of shell command
  • A great way to automate a repeated sequence of
    commands
  • Generate Shell script
  • Make and edit script file
  • File should be start with !/bin/sh
  • absolute path to the shell program
  • Not the same on every machine
  • Add execution mode for shell script
  • Chmod x scriptname

21
Command and variable
  • Command
  • You can run any program in a shell by calling it
    as you would on the command line
  • There are also some built-in command
  • Echo, test, read, exit, wait shift
  • Refer to bash manual (man bash)
  • Variable
  • Local variable spaces matter
  • namevalue (Assignment)
  • name (Replaced by value of name)
  • Single value example
  • bindir /usr/bin
  • List of values (separated by spaces)
  • searchdirstests HOME/test2 .

22
String replacement function
  • String replacement (positional parameters)
  • Variables are placeholders where we will
    substitute the value of the variable
  • Function

iters1 2 3 4 for i in iters do echo i done
For i in 1 2 3 4 do echo i done
Function functionname shell commands
23
Example
  • Variable example
  • String replacement example

24
Example
  • Variable function example

script ascript   function afunc echo in
function 0 1 2 var1in function echo
var1 var1 var1outside function echo
var1 var1 echo 0 1 2 afunct funcarg1
funcarg2 echo var1 var1 echo 0 1 2
script ascript   function afunc echo in
function 0 1 2 var1in function echo
var1 var1 var1outside function echo
var1 var1 echo 0 1 2 afunct funcarg1
funcarg2 echo var1 var1 echo 0 1 2
25
Flow control
  • Bash support flow control
  • If/else, for, while, until, case, select

for x 1 to 10 do begin statements end ------
--------------- for dir in PATH do ls ld
dir done
case expression in pattern1) statements patt
ern2)_ statements .. esac
if/else if condition then statements elif
condition then statements else statements f
i
while condition do statements done
select name in list do statements that can use
name done
until command do statements done
26
Flow control example
!/bin/bash renames.sh ?? ?? ??? ????
criteria1 re_match2 replace3 for i in
( ls criteria ) do srci tgt(echo i
sed -e "s/re_match/replace/") mv src tgt
done
27
Reference
  • man bash (man page for bash)
  • http//en.wikipedia.org/wiki/Bourne-Again_shell
  • http//tldp.org/LDP/Bash-Beginners-Guide/html/
  • www.cs.toronto.edu/krueger/.../f05/.../Week2-Shel
    l-1up.pdf 
  • homepage.smc.edu/morgan_david/linux/a01-shell.pdf
Write a Comment
User Comments (0)
About PowerShow.com