Title: Operating Systems Lecture 2 UNIX and Shell Scripts
1Operating SystemsLecture 2UNIX and Shell
Scripts
2C Programs with Arguments
A C(C) program may be called with arguments
(like parameters for the whole program. Arguments
may be used for 1. Options used for decisions
in main( ) 2. Strings that change in output
statements 3. Filenames for input and output
files 4. Strings can be changed to values with
atoi( ) Arguments have built-in names argc
Argument count (the number of arguments
given) argv List of arguments (array of
strings)
3Calling a program with Arguments
Example gt myProgram inputFile outputFile argc
3 (myProgram counts as one of the
arguments) argv holds argv0
"myProgram" argv1 "inputFile" argv2
"outputFile"
4C Code using argc and argv
include ltiostream.hgt include ltfstream.hgt int
main(int argc, char argv ) ifstream
inFile ofstream outFile if (argc !
3) //Error checking cout ltlt "Usage myProgram
inFile outFile " ltlt endl else
inFile.open(argv1) if (!inFile) //Error
checking cout ltlt argv1 ltlt " not opened." ltlt
endl outFile.open(argv2) if (!outFile)
//Error checking cout ltlt argv2 ltlt " not
opened." ltlt endl //Rest of code for input and
output from files... return 0
5Using atoi
All arguments are input as strings. If you want
to use numeric values, you must convert from the
string to a number include ltstdlib.hgt ... int
number, square ... number
atoi(argv1) square number number ...
6UNIX Commands
Commands you should already know ls, pwd, cd,
mkdir, rmdir cp, mv, rm substitution
character Commands you should learn echo, cat,
more, grep pipe that connects stdout with
stdin e.g. ls -l grep Aug Use man to find
out about commands. A good UNIX tutorial can be
found here http//www.ee.surrey.ac.uk/Teaching/Un
ix/index.html
7Kernel vs. Utilities
The kernel is a process that is always
executing. A process is a program that is loaded
into memory and executing Utilitites reside on
the disk (e.g. grep, lpr, etc.) The shell
program is a utility. The shell knows some
built-in commands that don't have to be read off
the disk. (cd, pwd, etc)
8Shells
The shell is a command line interpreter. Its
function is to get and execute the next
statement. Common shells Bourne shell
(/bin/sh) Korn shell (/bin/ksh) C
shell (/bin/csh) T shell (/bin/tcsh) GNU
Bourne-Again Shell (/bin/bash) Z shell
(/bin/zsh) The Bourne shell is standard. The
shell is just a program. Anyone can write their
own custom taylored shell.
9The Shell Environment
The shell environment is a list of associated
strings in the shell PATH path or paths used
to look for programs or utilities. e.g.
/opt/local/bin HOME Location of home
directory e.g. /fac/ycjiang SHELL Current
shell e.g. /bin/csh Type setenv to see a list
of current environment variables. Use setenv to
change environment setenv TERM xterm Change
terminal type to xterm
10Unix shell
- shell is
- simply a macro processor that executes commands
- a command interpreter Which provides the user
interface to the rich set of GNU utilities - a programming language Allow these utilities to
be combined. - Shell scripts are
- Files containing commands can be created, and
become commands themselves. - These new commands have the same status as system
commands in directories such as '/bin', allowing
users or groups to establish custom
environments. - Ex.
- !/bin/bash saved as fred
- ls grep .sxw /home/ycjianggt ./fred
--------- unix1.sxw unix2.sxw ... - A shell allows execution of GNU commands,
- both synchronously and asynchronously.
- The shell waits for commands given synchronously
to complete before accepting more input - Commands given asynchronously continue to execute
in parallel with the shell while it reads and
executes additional commands.
11More intro on shell
- Shells may be used interactively or
non-interactively - they accept input typed from the keyboard or from
a file. - Shells also provide a small set of built-in
commands (builtins) - implementing functionality impossible or
inconvenient to obtain via separate utilities. - eg cd, break, continue, and exec
- cannot be implemented outside of the shell
because they directly manipulate the shell
itself. - The history, getopts, kill, or pwd builtins,
among others, could be implemented in separate
utilities, but they are more convenient to use as
builtin commands. - While executing commands is essential, most of
the power (and complexity) of shells is due to
their embedded programming languages. - Like any high-level language, the shell provides
variables, flow control constructs, quoting
(removing special meaning from a sequence of
characters see bash ref), and functions. - Shells offer features geared specifically for
interactive use. - These interactive features include job control,
command line editing, history and aliases.
12Examples
- http//www.gnu.org/software/bash/manual/bashref.ht
ml
13UNIX Security
- Each user has
- userid, password, home directory (can login many
times simultaneously) - Each user's files and dirs from tree rooted in
home dir - Usually want your files and dirs to be able to be
listed, displayed and modified only by you - Possible to give other users permissions to
access your files - in various ways - Owners and Groups
- Each file and dir has - an owner and a group
associated with it Owner - When you create a file, you become its owner
(usually) Group - Users can join "groups" of other users with whom
they can share files dirs - Users can join many groups, but have one primary
group - Users can "work" in different groups
- - to change groups newgrp newgroup sysadmin is
only person who can create groups and add you to
groups - When user creates file
- - file's group is group the user was in when the
file was created - - typically your primary group
- chgrp groupname filename
- - file's group can be changed can only be changed
by a current group member - owner must be in
group groupname - - after change, only members of new group have
power over file - chown userid filename
14File Permissions
- Files have permissions for the user/owner, group
and others. - You can view the permissions by typing ls -l
- -rwxr--r-- 1 ycjiang fac 81 Jan 3 2153 myFile
- The information given is
- file type (- plain file)
- permissions for user (rwx), group (r--) and
other (r--) - number of hard links (1)
- user/owner name (ycjiang)
- group name (fac)
- size of file (81)
- date and time last modified (Jan 3 2153)
- filename (myFile)
15File Permissions
- Permissions on a file dictate how owner, group
members and other users may operate on files - - read, write, and execute permissions for each
file, dir - - each of user (u), group members (g), and other
users (o) have a distinct set of read (r),
write (w), and execute (x) permissions on a
file/dir Numeric equivalents - u g o
- rwx rwx rwx
- 111 111 111
- 7 7 7
- 101 011 100
- 5 3 4
- chmod 534 dum
- r file file can be read and copied
- dir files names in directory can be listed
(cannot see file contents --need r on files and x
on dir) - w file file can be modified or deleted
- dir file names can be added/deleted to/from
dir (can modify files without w on dir) - x file file can be executed if "executable"
(binary, script) - dir If name known can read file if file is
r, or see into subdirectory dir but can't list
contents of dir - to see known file's contents,
need r on file, x on dir
16To rephrase
- r file can be read, copied (see file
contents) - w file can be modified, deleted
- x file can be executed (if "executable" binary,
script) - For directories
- r can list files in directory (r alone cannot
read file or get permissions) - w add/delete files from directory
- x allows reading IF YOU KNOW FILENAME can't
list files - Superuser has ALL permissions
- Some special bits (UNIX for the Impatient Pg
47) - set-uid bit s replacing x in user (owner's)
permissions - - set user id (set-uid) bit on your program
file - - "other" runs program which accesses a file
- - file now accessed with permissions of owner,
not "other normally, when "other" runs a program
and it accesses a file, the file permissions seen
are "other" permissions on that file - - allows others to access a file you own but only
through YOUR program and thus in limited ways
like private data and a get-method in OO - set-gid bit s replacing x in group
permissions - - set group id (set-gid) bit on your program
file - - "other" runs program which accesses a file
- - file now accessed with permissions of group,
not "other - sticky bit t replacing x in others permissions
17To rephrase (Contd)
- To check this (see chmod just below)
- - in home directory create subdirectory test
- - in test create file named dum (contains abc
def) To check this (see chmod just below) - - cd test
- - chmod 700 dum user has read, write ,
execute - - cd .. - pretend with chmod 000 test
- - try ls ./test gt Permission denied
- - try cat ./test/dum gt Permission denied change
to chmod 100 test user x-- - - try ls ./test gt Permission denied
- - can see permissions ls -al
- - try cat ./test/dum gt abc def
- To allow anyone to execute file
/home/smith/labs/myfile - - need x perms on myfile,
- - need x perms on dirs down path to
/home/smith/labs so others can get at the file in
the tree.
18Web Accessibility
- To make file.html in public_html "web accessible"
from a link on your page (file name "known") - - need x on public_html and x on its parent (your
home) - - need r on file.html
- - don't provide any more access than that !!
- Can see perms on files with -l option of ls
- -rwxr-xr-- 1 ycjiang nobody 1734 Dec 21 2002
lab1 - user has read,write and execute perms on
lab1, group has only read and execute perms on
lab1, all others have only read perms on lab1 - Changing permissions
- - owner (only) can change permissions on a
file/directory chmod command - chmod (ugoa)(-)(rwx) file/dir name(s) changes
to exactly those given perms /- add, remove
given perms - e.g., chmod gw lab1 -rwxrwxr--
- chmod ug-x lab1 -rw-rw-r--
- chmod arwx lab1 -rwxrwxrwx chmod gorx lab1
-rwxr-xr-x - chmod gx,ow lab1 -rwxxrwx
19Note
- - - -(000) 0
- - - x(001) 1
- - w -(010) 2
- - w x (011) 3
- r - -(100) 4
- r x (101) 5
- r w - (110) 6
- r wx (111) 7
- chmod 160 lab1 sets only x for user, only rw for
group and no perms for others - COMMON! Default permissions umask sets up default
permissions for all subsequently created
files/dirs tells which perms to EXCLUDE - e.g., umask 022 exclude write perms for group
and other don't exclude any perms from user
(excluding from what was set up as default by
sysadmin--so typically rw for user)
20Changing Permissions
File types - plain file, d
directory Permissions r read permission, w
write permission, x execute permission. Use
chmod to change permission for user, group, other
or all chmod ar filename everyone gets read
permission chmod gx filename group gets
execute permission chmod uw filename user gets
write permission chmod o-w filename others lose
write permission chmod og-rw filename group and
others lose read and write permission
21Exercise
- If you had a directory called dog that
- had read permissions for everyone, and write
permissions only for the owner, and execute
permissions for the group and others, - what would the permissions part of a "long
listing" of the directory look like? - Assuming you were in the parent directory of dog,
what is the command that would remove all
permissions for group and other? - what command would give execute permissions to
everyone, read permissions to the group, and
remove write permissions for the user? - You have default permissions for all files you
create. - create 3 files, called tst1, tst2 and tst3, with
vim (just enter one word or sentence in each
file). - Then from your home directory, use whatever
commands you need to discover what the default
permissions are on all files you create
22Shell Programming
- Shell scripts must be marked as executable
- chmod ax myScript
- 2. Use to start a comment.
- Comments run from to the end of the line.
- 3. All shell scripts begin with the interpreter
you want to use - !/bin/sh
- Example
- !/bin/sh
- who grep ycjiang
- exit 0
23Running a shell script
- To run a UNIX script
- Type the script into a file.
- Change the file permission to executable.
- Execute it (by typing the filename at the
prompt).
24Shell Variables
Shell variables are stored as strings Example
!/bin/sh x1 Note No spaces in
assignment. If space after x, thinks x is a
command echo The value of x is x x prints
the value of variable x echo The home directory
is HOME echo The current shell is SHELL (Note
to debug, use -x sh -x scriptFileName This
will list the commands as they are executed.)
25Using Quotes
Single quote Groups together characters until
end quote. is not processed. Example
!/bin/sh grep Constance ycjiang
/etc/passwd Tries to open ycjiang as a file
grep 'Constance ycjiang' /etc/passwd Search
es for Constance ycjiang in passwd file
x1 echo x echos 1 echo 'x' echos
x exit 0
26Double Quotes
Double quotes act like single quotes, except the
is processed !/bin/sh x1 echo x echos
the value of x echo "x" echos the value of
x address"College of the Holy Cross" echo
address echos College of the Holy Cross echo
"address" ditto exit 0
27More Quotes
Backslash (\) Places a single quote around a
character \gt is the same as 'gt' Back quote
() Tells shell to execute the enclosed command
and insert the output here !/bin/sh echo
There are who wc -l users logged on exit
0 Try these examples out for yourself!
28Devices
- device
- usually a piece of equipment for storing or
communicating data, e.g., printer, disk drive,
terminal, modem - In UNIX, we access devices as if they were
(special) files, typically in dir /dev e.g., a
printer might be "file" /dev/lp1 - - therefore cp myfile /dev/lp1 - prints myfile
on line printer - - echo "fred" gt /dev/usb/lp0 prints on my
printer (HPLJ3015) as superuser - set to convert
text to postscript stdin/stdout/stderr are
/dev/stdin, /dev/stdout, /dev/stderr - - echoing a line ONTO stderr could be done
- echo blah blah gt /dev/stderr - use stderr
- diff blah blat 2gt errorfile since 0,1,2
stdin/out/err - diff blah blat 1gt errorfile creates empty
errorfile stderr goes to screen
29Devices Contd
- When OS recognizes an operation on a special
file, it calls a pgm called a device driver to do
the op (e.g., cause data to be displayed on
screen) - character devices transfers info char by char
(printer, keyboard, modem) - block devices transf. info in batches of chars,
called blocks (disk, tape) - terminals special char. devices, since chars are
interpreted - tabs transformed into blanks -
don't see every char you ever typed -e.g.
those just before a "backspace" char - Null Device (/dev/null) the garbage can, black
hole -- data can go in but never come back out. -
sending output to /dev/null throwing it away - - sometimes commands produce output we want to
disregard When OS recognizes an operation on a
special file, it calls a pgm called a device
driver to do the op (e.g., cause data to be
displayed on screen) - terminals special char. devices, since chars are
interpreted - tabs transformed into blanks -
don't see every char you ever typed -e.g.
those just before a "backspace" char
30Exercise
- Create a file called tst1, and a directory called
dog, in your home directory - Copy tst1 to a file called cat in directory dog.
Move into dog. - Remove write and execute permissions for user
(that's you!) on file cat. - Now try to erase cat. What happens? Why?
31(I/O) Streams
- UNIX commands, and pgms, do I/O
- e.g., ls writes results to screen (output)
- passwd reads your old and new password from the
keyboard - How? The shell associates streams with the
command. - - stream like a tunnel (output sent down, input
received) - The shell assigns 3 standard streams to any
command stdin, stdout, stderr - When command needs to read input, it looks in
stdin, and reads whatever is there When command
produces output, it stdout stderr
- The shell normally attaches other end of the
streams to devices, such as the keyboard and
monitor (screen) sends it down stdout - When command produces error message, it sends it
down stderr. - e.g., passwd ----- stdin
- e.g., -keyboard passwd monitor
- -- stdin stdout
stderr monitor - When passwd wants input (e.g., the current
password) reads from stdin reading from
keyboard (passwd doesn't know or care that it is
the keyboard, just reads from stdin) - Output similar- when passwd encounters error,
writes error msg on stderr (it ends up appearing
on monitor, but passwd doesn't know or care)
32Redirecting I/O
- Normally, the shell will use standard input and
standard output for executing commands. - You can redirect the standard input and output
using lt and gt - ls -l gt filex
- Redirect the output of ls -l into the file named
filex. - Using gtgt allows you to append output to a file
- ls -l gtgt filex
- Append the output of ls -l to the end of file
named filex. - The redirection constructs permit fine-grained
control of the input and output of those
commands.
33I/O Redirection Examples
- for command cat myfile
- - shell attaches other end of stdin to the file
myfile read from stdin reading characters from
file myfile- the shell redirected stdin to come
from a file (stdout and stderr to monitor still) - You can tell shell to redirect std streams ls gt
myfile - shell attaches other end of stdout to
file myfile - - to see output of ls command - must cat, edit,
more, less, etc. - myfile gt myfile clobbers myfile appends Redirect
input similarly - e.g., if cat cmd is given no argument, stdin
is keyboard - /home/ycjianggt cat gt outfile my first line
my second line d lt end of file in UNIX (z
in Win) now file outfile contains my first
line my second line - /home/ycjianggt cat outfile gt my first line my
second line... - /home/ycjianggt cat lt outfile (same effect)file
outfile displayed on screen - /home/ycjianggt cat ltinfile gtoutfile
- /home/ycjianggt lt nothing displayedTo redirect
stderr use "2gt" (review!)e.g.,
/home/ycjianggt ls labs 2gt errfile
error msgs written to file errfile instead of
screen (if, for example, file labs did not
exist...) - Use redirect and /dev/null to throw output away
(review!) - e.g., /home/ycjianggt ls nonExistantFile
/bin/ls nonExistantFile No such file or
directory and errors printed on stderr. but, - /home/ycjianggt ls nonExistantFile 2gt/dev/null
- - each stream given a number by shell - stdin 0,
stdout 1, stderr 2 (ls gtoutfile ls 1gtoutfile)