LINUX System : Lecture 5 (English-Only Lecture) - PowerPoint PPT Presentation

About This Presentation
Title:

LINUX System : Lecture 5 (English-Only Lecture)

Description:

... shell commands can be stored in a file called a shell script. ... C shell (csh) and variants (tcsh) Bourne shell (sh) and variants (bash, ksh) Invoking scripts ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 16
Provided by: cau2
Category:

less

Transcript and Presenter's Notes

Title: LINUX System : Lecture 5 (English-Only Lecture)


1
LINUX System Lecture 5(English-Only Lecture)
  • Bong-Soo Sohn
  • Assistant Professor
  • School of Computer Science and Engineering
  • Chung-Ang University

Acknowledgement (i) wikipedia.org ,
(ii) http//www.doc.ic.ac.uk/wj
k/UnixIntro
2
Shell Variables
  • Environment Variables
  • Used to provide information to programs
  • (Global) environment variable
  • New programs and shells inherit environment
    variables from their parent shell
  • Local shell variable
  • Used only by that shell
  • Not passed to other processes

3
Environment Variables
  • env or printenv command
  • Display current environment variables
  • DISPLAY The graphical display to use, e.g.
    nyssa0.0
  • EDITOR The path to your default editor, e.g.
    /usr/bin/vi
  • GROUP Your login group, e.g. staff
  • HOME Path to your home directory, e.g.
    /home/frank
  • HOST The hostname of your system, e.g. nyssa
  • IFS Internal field separators, usually any white
    space (defaults to tab, space and ltnewlinegt)
  • LOGNAME The name you login with, e.g. frank
  • PATH Paths to be searched for commands, e.g.
    /usr/bin/usr/ucb/usr/local/bin
  • PS1 The primary prompt string, Bourne shell only
    (defaults to )
  • PS2 The secondary prompt string, Bourne shell
    only (defaults to gt)
  • SHELL The login shell you're using, e.g.
    /usr/bin/csh
  • TERM Your terminal type, e.g. xterm
  • USER Your username, e.g. frank

4
Set Shell Variables
  • Mostly set automatically when log in
  • setenv
  • setenv NAME value in C Shell
  • set
  • set name value in C Shell

5
Shells scripts
  • Any collection of shell commands can be stored in
    a file called a shell script. Scripts have
    variables and flow control statements like other
    programming languages.
  • There are two popular classes of shells
  • C shell (csh) and variants (tcsh)
  • Bourne shell (sh) and variants (bash, ksh)

6
Invoking scripts
  • There are two ways to launch scripts
  • 1) Direct interpretation
  • csh scriptfile args
  • 2) Indirect interpretation
  • The first line of the file must be
  • !/bin/csh
  • and the file must be executable.

C Shell
7
Variables
  • To set variables
  • set X value
  • Variable contents are accessed using
  • echo PATH
  • To count the number of variable elements
  • echo Y

C Shell
8
Variables contd
  • To create lists
  • set Y (abc 1 123)
  • To set a list element
  • set Y2 3
  • To view a list element
  • echo Y2

C Shell
9
Command arguments
  • A shell script to swap files
  • ! /bin/csh f
  • set tmp argv1
  • cp argv2 argv1
  • cp tmp argv2
  • The number of arguments to a script
  • argv

C Shell
10
if-then-else
  • if ( expr ) simple-command
  • if ( expr ) then
  • commandlist-1
  • else
  • commandlist-2
  • endif

C Shell
11
if-then-else contd
  • An example
  • if (argv ltgt 2) then
  • echo we need two parameters!
  • else
  • set name1 argv1
  • set name2 argv2
  • endif

C Shell
12
Loops
  • while ( expr )
  • commandlist
  • end
  • foreach var ( worddlist )
  • commandlist
  • end

C Shell
13
switch
  • switch ( str )
  • case string1
  • commandlist1
  • breaksw
  • case string2
  • commandlist2
  • breaksw
  • default
  • commandlist
  • endsw

C Shell
14
goto (Considered harmful!)
  • To jump unconditionally
  • goto label
  • A label is a line such as
  • label
  • The classic paper on why not to use goto
  • Go To Statement Considered HarmfulEdsger W.
    Dijkstra, CACM, March 1968

C Shell
15
An example script
  • ! /bin/csh -f
  • foreach name (argv)
  • if ( -f name ) then
  • echo -n "delete the file 'name' (y/n/q)?"
  • else
  • echo -n "delete the entire dir 'name'
    (y/n/q)? "
  • endif
  • set ans lt lt means read a line
  • switch (ans)
  • case n continue
  • case q exit
  • case y rm -r name continue
  • endsw
  • end

C Shell
Write a Comment
User Comments (0)
About PowerShow.com