Lecture 3

advertisement
Lecture 3




Log into Linux
Questions about Homework 1? Reminder:
due today.
Homework 2 posted, due next Tuesday. Write
answers into textfile and submit using
submission system.
Reminder: Additional on-line references
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
1
Outline

BASH - Bourne Again SHell

Redirection

BASH programming

Variables and environment

Selection and repetition

Positional parameters
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
2
Introduction to the Shell


The shell is a command interpreter and a fullfeatured programming language. It is
especially suited for system administration and
file, directory and process management.
Several different shells are available:
sh, bash, csh, zsh, ksh
Change your default shell to bash


$ chsh ­s /bin/bash
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
3
"Hello World" Script
$ cat > hello
#!/bin/bash
# Prompt user for name
echo ­n "Enter your name: "
read name
echo Hello there $name!
exit 0
^D
$ chmod +x hello # make executable
$ ./hello
Enter your name: Fred Flintstone
Hello there Fred Flintstone!
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
4
Shell Scripts




Start with a she-bang (#!), then name of shell
executable file.
The kernel will pass the script to the proper
interpreter. End with “exit 0” (or non-zero to
indicate an error condition).
Comments begin with a #
Make the script executable OR you can also
run the program like this:
$ bash hello # run in new env (like ./hello)
$ . hello
# run in same env (also source hello)
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
5
Wildcards (globbing)

Wildcard expansion is done by the shell; not
the application. A * matches any character
except a leading dot (.). A ? matches a single
character. A [ ] defines a set to match a single
character.
$ echo * # same result as ls
a.1 b.1 c.1 t2.sh test1.txt
$ ls t?.sh
t2.sh
$ ls [a­c]*
a.1 b.1 c.1
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
6
Redirection




Every program automatically has three
files/streams available: standard input, standard
output, and standard error.
In C, the FILE streams are stdin, stdout, and
stderr.
In C++, the IO streams are cin, cout, cerr.
By default, they are connected to the keyboard,
the display, and the display, but they can be
redirected.
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
7
Redirection
// File: redirect.cpp
// This program reads a line from standard
// input and outputs the line to standard
// output and standard error
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
getline(cin, line);
cout << "stdout: " << line << endl;
cerr << "stderr: " << line << endl;
return 0;
}
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
8
Redirection
$ ./redirect
Hi there
stdout: Hi there
stderr: Hi there
$ ./redirect < /etc/passwd
stdout: root:x:0:0:root:/root:/bin/bash
stderr: root:x:0:0:root:/root:/bin/bash
$ ./redirect < /etc/passwd > /dev/null
stderr: root:x:0:0:root:/root:/bin/bash
$ ./redirect < /etc/passwd 2> /dev/null
stdout: root:x:0:0:root:/root:/bin/bash
$ ./redirect < /etc/passwd 2> /dev/null > output.txt
$ cat output.txt
stdout: root:x:0:0:root:/root:/bin/bash
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
9
Redirection

To redirect both standard output and error into
the same file:
./program &> output.txt


Shell script input and output is redirected in a
similar manner.
To redirect output from standard output to
standard error from within a shell script:
echo "usage: floof filename" 1>&2
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
10
Pipes


A pipe (|) connects the standard output of the
program on the left of the pipe to the standard
input of the program on the right of the pipe.
Here's an example that displays all usernames
in alphabetical order:
cut ­d: ­f1 /etc/passwd | sort | less
(see cut man page for option details)
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
11
Built-in vs. External



Many commands are built into the shell: cd,
for, while. Type “help” at a prompt to see a list
of built-in commands (or see the bash man
page.)
Many other commands are external programs:
ls, vi, grep, php.
There are both built-in and external versions of
many commands: echo, pwd, test.
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
12
Shell Variables

All shell variables are strings:
file=/tmp/myfile
list="apples oranges"
let a=3*4
# a will equal the string 12

Variable access is by prefixing $
echo $list > $file

Variable substitution occurs within double
quotes, but not within single quotes.
echo "$a"
echo '$a'
echo \$a
Tuesday, January 21
# displays 12
# displays $a
# also displays $a
CS 375 UNIX System Programming - Lecture 3
13
The Environment

Each program owns an area of memory called
the environment. Exported variables are
copied from the parent's environment to the
child's.
$HOME
$PATH
$TERM
$PS1
$0
$#
$?
$$
Tuesday, January 21
# User's home dir
# Colon separated directory list
# Terminal type
# Command prompt
# The name of the script
# Number of arguments
# Exit status of last program
# Process ID
CS 375 UNIX System Programming - Lecture 3
14
Conditions


Program exit status can be used as a condition
expression. 0 implies true; 1 (or non-0) implies
false. All programs should return an exit status.
The test command (there are built-in and
external versions) can be used to test file
conditionals, string comparison, and arithmetic
comparison. The [ command is equivalent, but
requires a space after the [ and a final
argument of ].
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
15
Conditions

File conditionals are unary. E.g.,
test ­e ~/readme.txt # does file exist?
[ ­x hello ]
# is file executable?

String comparison
test "$user" = "fred" # string equality
[ ­z $word ]
# is string empty?

Arithmetic comparison
test $count ­le 10
[ $total ­ne 5 ]
Tuesday, January 21
# count <= 10?
# total != 5
CS 375 UNIX System Programming - Lecture 3
16
Conditions



Use “help test” to find out more about the test
built-in command.
The commands “true” and “false” have exit
status of 0 and 1, respectively.
Here is the C++ code equivalent for true and
false:
int main() { return 0; } // true
int main() { return 1; } // false

true is especially useful as a loop condition
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
17
Selection

The commands if and case are available
selection constructs. Here is an if example.
(The case command is shown in a later
example.)
if grep ­qi fred /etc/passwd # exit status
then
# do something
elif test ­d /home/fred # file test
then
# do something else
else
# do something
fi
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
18
Positional Parameters

Shell variables $1, $2, ..., ${10}, etc. refer to
script arguments. $0 refers to script name.
if [ ­f "$1" ]; then # Why "$1" and not $1?
# do something
fi

The set command can be used to assign to the
positional parameters.
set $(date)
today="$2 $3, $6"
echo $today
Tuesday, January 21
# command substitution
CS 375 UNIX System Programming - Lecture 3
19
Positional Parameters

$* and $@ represent all of the positional
parameters. $# is the number of parameters.
if [ $# ­ne 2 ]
then
echo "usage: floof arg1 arg2" 1>&2
exit 1
fi

The shift command shifts all of the parameters
left one position (2 -> 1, 3 -> 2). The getopts
command is useful for parsing parameters to
distinguish between options and arguments.
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
20
Repetition

The commands while and until are available
conditional repetition constructs. The while
command checks for a true condition. The
until command checks for a false condition.
while true # infinite loops
do
clear; ls ­l; sleep 10
done
until false; do
clear; ls ­l; sleep 10
done Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
21
Repetition

The for command executes for each value in a
list.
for f in * # copy html files to backup dir
do
case $f in # executes first pattern match
*.htm | *.html)
cp $f ~/html_files
;;
*)
# match everything else
echo $f HAS NOT been backed up
;;
esac
done
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
22
Repetition

Without a list, the for command defaults to the
parameter list ($@)
for arg
# loop through pos. parameters
do
lpr $arg
done

There is also a C-style for loop. Note header
has extra ( )'s
for ((i=0; i<10; i++))
do
echo ­n "$i "
done
echo
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
23
In-class Exercise

Write a shell script named lastfirst that takes a
single filename argument and writes the file to
standard output in complete reverse order (last
line, last character first).


Use “man -k reverse” to find relevant utilities. Do not use
brute force coding.
Write appropriate error messages if an incorrect number of
arguments is used or if the input file does not exist.

Exit with appropriate status.

Read from standard input if no arguments are given.
Tuesday, January 21
CS 375 UNIX System Programming - Lecture 3
24
Related documents
Download