Download presentation
Presentation is loading. Please wait.
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)
2
Shell Variables Environment Variables (Global) environment variable
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. nyssa:0.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 <newline>) 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 >) 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 set
$ 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 cont’d To create lists: set Y = (abc 1 123)
To set a list element: set Y[2] = 3 To view a list element: echo $Y[2] C Shell
9
Command arguments A shell script to swap files: #! /bin/csh –f
set tmp = $argv[1] cp $argv[2] $argv[1] cp $tmp $argv[2] The number of arguments to a script: $#argv C Shell
10
if-then-else if ( expr ) simple-command if ( expr ) then commandlist-1
endif C Shell
11
if-then-else cont’d An example: if ($#argv <> 2) then
echo “we need two parameters!“ else set name1 = $argv[1] set name2 = $argv[2] endif C Shell
12
Loops while ( expr ) commandlist end foreach var ( worddlist ) C Shell
13
switch C Shell switch ( str ) case string1: commandlist1 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 Harmful Edsger W. Dijkstra, CACM, March 1968 C Shell
15
An example script C Shell #! /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 = $< # $< means “read a line” switch ($ans) case n: continue case q: exit case y: rm -r $name; continue endsw end C Shell
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.