ITSC 1329 Module 6 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

ITSC 1329 Module 6

Description:

Tilde Character with a Username (~user1) Represents home directory of specified user. The tilde is not available in the Bourne shell. Dash Character ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 30
Provided by: kno83
Category:
Tags: itsc | module | tilde

less

Transcript and Presenter's Notes

Title: ITSC 1329 Module 6


1
ITSC 1329 Module 6
  • Using Commands Within the Shell

2
Module 6 Objectives
  • Use shell metacharacters
  • Describe the Korn shell variables
  • Display the command history
  • Redirect commands
  • Describe the command-line interpreter
  • Work with user initialization files

3
Three Types of Shell Metacharacters
  • Korn shell metacharacters are specific
    characters, usually symbols, that have special
    meaning to the shell
  • There are three types
  • Path Name
  • File Name Substitution
  • Redirection
  • Note Never use any of the shell metacharacters
    when naming files and directories

4
Pathname Metacharacters
  • Tilde Character ()
  • Represents home directory of current user
  • Tilde Character with a Username (user1)
  • Represents home directory of specified user
  • The tilde is not available in the Bourne shell
  • Dash Character (-)
  • Represents the previous working directory

5
File Name Substitution Metacharacters
  • File name substitution metacharacters are also
    known as Wildcards
  • Metacharacters allow the user to specify parts of
    filenames and let the shell find the rest
  • The metacharacters we will cover
  • Asterisk
  • Question Mark ?
  • Square Brackets
  • Exclamation Point !

6
The Asterisk Character
  • Used to represent zero or more characters (except
    the leading period . of a hidden file)
  • Used with various commands to represent all files
  • cp dir2 Copies all files from pwd to dir2
  • ls f Lists all files that begin with f
  • ls f1 Lists all files that begin with f and
    end with 1
  • DOS used . to represent all files

7
The Question Mark ? Character
  • Used to represent any single character (except
    the leading period . of a hidden file)
  • ls dir?
  • ls empty?
  • ls ?a
  • ls j???a
  • ls .
  • ls dir2/b
  • ls /etc/d

8
The Square Bracket Characters
  • The square bracket characters represent a set or
    range of characters for a single character
    position
  • ls ejt
  • ls a-f
  • ls A-Z0-9
  • ls e0-9
  • ls j???ac14
  • ls ?ua0-9

9
The Exclamation Point ! Character
  • The exclamation point (also called bang) is used
    to exclude the pattern. It means not
  • ls e!0-9
  • ls !A-Z
  • ls !e
  • ls junk!0-9
  • ls j???!ac14
  • ls !A-Z0-9
  • ls ?!m1

10
Introducing Korn Shell Variables
  • A variable refers to a temporary storage area in
    memory. Variables contain
  • Information needed for customizing the shell
  • Information needed by other processes to function
    properly
  • Korn shell programming uses two types of
    variables
  • Environment variables
  • Variables that are passed to child processes
  • Local variables
  • Variables that are not passed to child processes

11
Korn Shell Commands for Variables
12
Displaying Variables
  • To list all shell variables with their current
    values, use set
  • set
  • Use echo or print to display the value that is
    stored inside a variable
  • print PS1
  • echo PWD
  • echo PWD
  • The sign tells the shell that what follows is a
    variable

13
Setting and Unsetting Variables
  • Syntax for setting variables
  • varvalue or VARvalue
  • rockdir/export/home/user1/Music/Types/Rock
  • set
  • cd rockdir
  • Syntax for unsetting variables
  • unset var or unset VAR
  • unset rockdir
  • set

14
Exporting Variables to Sub-Shells
  • All shells have built-in commands, such at the cd
    and ls commands
  • Any process in UNIX that is not a built-in
    command (like a shell script) is forked into a
    child process (sub-shell) and runs independently
    of the parent shell
  • Local variables are not used by child processes
  • Environmental variables are used by all processes

15
Exporting Variables Example
  • VAR1first_variable Sets the variable
  • set Shows set variables
  • echo VAR1 Shows value of VAR1
  • sh Changes to Bourne Shell
  • echo VAR1 Doesnt work
  • exit Exits child shell
  • export VAR1 Exports VAR1 to child processes
  • sh
  • echo VAR1 Success!

16
Default Korn Shell Variables
17
More Default Variables
18
Ignoring Metacharacters
  • There are several ways to turn off metacharacters
  • The most reliable is the backslash \
  • echo p
  • echo p
  • echo p\
  • echo SHELL
  • echo \SHELL
  • echo SHELL Uses forward single quote

19
Calling Other Commands
  • You may need to execute a command within another
    command
  • echo Today's date is date.
  • echo Today's date is date.
  • Uses backtics to enclose command
  • Syntax command command
  • echo Today's date is (date).
  • Uses ( ) to enclose command
  • Syntax command (command)

20
Using the History Command
  • Syntax for history command history option
  • By default, the history command displays the last
    16 commands
  • history
  • Your history file (.sh_history) contains up to
    999 commands
  • cat .sh_history
  • Specify how many commands to display, not
    counting the history command itself
  • history -10
  • Actually displays last 11 commands

21
Displaying Previous Commands
  • Finding a previous command
  • history ls
  • history cat ls
  • Using SSH
  • ltctrlgt p
  • Using the r command
  • r cat
  • r ls
  • r c
  • r history

22
Understanding Command Redirection
  • Typically, the shell receives or reads command
    input from the keyboard and displays or writes
    the command output to the screen
  • There are two redirection metacharacters
  • Greater than gt
  • Less than lt
  • Both of these can be used with the pipe

23
The File Descriptors
File descriptors determine where the input to the
command originates and where the output and error
messages are sent
24
Redirection Examples
  • cat
  • Takes you to a blank line
  • Hi!
  • Input received from stdin and sent to stdout
  • cat lt dante
  • Tells cat command to receive input from dante
    file instead of keyboard
  • ls l gt lsfile
  • Redirects stdout from screen to a file
  • echo This is a long list gtgt lsfile cat lsfile
  • Appends to lsfile instead of overwriting

25
Redirecting Error Messages
  • You can redirect error message using the stderr
    file descriptor number (2) and the greater than
    symbol (gt)
  • ls lR /etc
  • There are a few error messages due to file
    permissions
  • ls lR /etc 2gt etc-errors
  • The command still runs, but no error messages are
    displayed
  • cat etc-errors
  • The file containing our redirected error messages

26
The Pipe Character
  • The pipe command redirects the stdout of one
    command to the stdin of another
  • ls lR more
  • cat dante wc w

27
User Initialization Files
28
Korn Shell Initialization Files
  • Initialization files are used to customize your
    working environment
  • The /.profile file is run by the login process
  • The /.kshrc file contains shell variables and
    aliases. To use it, the ENV variable must be
    defined in the .profile file
  • Changes to .profile and .kshrc take effect upon
    next login.
  • Run . /.profile or . /.kshrc to update
    immediately

29
Module 6 Objectives Review
  • Use shell metacharacters
  • Describe the Korn shell variables
  • Display the command history
  • Redirect commands
  • Describe the command-line interpreter
  • Work with user initialization files
Write a Comment
User Comments (0)
About PowerShow.com