Lecture 3 Log into Linux Questions about Homework 1? Reminder: Additional on-line references

advertisement
Lecture 3
●
Log into Linux
●
Questions about Homework 1?
●
Reminder: Additional on-line references
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
1
Outline
●
find command
●
BASH - Bourne Again SHell
●
Redirection
●
BASH programming
●
Variables and environment
●
Selection and repetition
●
Positional parameters
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
2
find
●
Syntax:
find [path] [options] [tests] [actions]
●
Search for files starting at path. Often / or .
●
Main options; always return true
-follow
-maxdepths N
-mount
Tuesday, September 1
Follow symbolic links
Search at most N levels
Don't search other file systems
CS 375 UNIX System Programming - Lecture 2
3
find
●
Tests return true or false. If false, stops. If true,
does next test/action
-atime N
last accessed N days ago
+N for greater than
-N for less than
-mtime N
last modified N days ago
-name pattern file name matches pattern
must be in quotes
-newer otherfile file is newer than otherfile
-type C
file is type C – e.g. f for regular file
-user username file is owned by username
Tuesday, September 1
CS 375 UNIX System Programming - Lecture 2
4
find
●
Combine tests with logical connectives
!, -not
-a, -and
-o, -or
●
●
invert test
both test must be true
one test must true
Use escaped parentheses to group
Actions are at the end and say what to do with
each file that matches
-print
-ls
Tuesday, September 1
print the name of matching file
use ls -dils on matching file
CS 375 UNIX System Programming - Lecture 2
5
find
●
Any command can be executed
-exec command
execute command on match
-ok command
ask user confirmation before
executing command on match
●
●
These actions must end with \; sequence in
order for find to know where the embedded
command ends
Use { } to represent the matching file
Tuesday, September 1
CS 375 UNIX System Programming - Lecture 2
6
find
●
Examples:
$ find / -name test -print # find all files named "test"
$ find . -mtime 2 -print # modified exactly 2 days ago
$ find . -mtime -2 -type f -print
# regular files modified in last 2 days
$ find . \( -mtime -2 -o -atime -2 \) -print
# modified or accessed in last 2 days
$ find . \( -mtime -7 -a -name "*.cpp" \) -print
# C++ source files modified in last week
$ find . \( -mtime -7 -a -name "*.cpp" \) -exec ls -l {} \;
# execute ls -l on them
$ find . \( -mtime -7 -a -name "*.cpp" \) \
-exec grep ^int {} \;
# print out lines starting with "int"
Tuesday, September 1
CS 375 UNIX System Programming - Lecture 2
7
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
8
"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!
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
9
Shell Scripts
●
●
●
●
Start with a sh-bang (#!), then name of shell
executable file (e.g. /bin/bash).
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)
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
10
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
11
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.
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
12
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;
}
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
13
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
14
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
15
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 | more
(see cut man page for option details)
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
16
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, emacs, g++, grep, php.
There are both built-in and external versions of
many commands: echo, pwd, test.
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
17
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
Thursday, September 3
# displays 12
# displays $a
# also displays $a
CS 375 UNIX System Programming - Lecture 3
18
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
$#
$?
$$
Thursday, September 3
# 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
19
Conditions
●
●
Program exit status can be used as a condition
expression. 0 implies true; 1 (or non-0) implies
false. (Note this is backwards from C/C++, etc.)
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 ].
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
20
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 ]
Thursday, September 3
# count <= 10?
# total != 5
CS 375 UNIX System Programming - Lecture 3
21
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
22
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
23
Positional Parameters
●
Shell variables $1, $2, ..., ${10}, etc. refer to
script arguments. $0 refers to script name.
if [ ­f "$1" ]; then # do something
fi
●
# Note ; # Why "$1" and not $1?
The set command can be used to assign to the
positional parameters.
set $(date)
today="$2 $3, $6"
echo $today
Thursday, September 3
# command substitution
CS 375 UNIX System Programming - Lecture 3
24
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.
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
25
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 Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
26
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
27
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
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
28
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.
Thursday, September 3
CS 375 UNIX System Programming - Lecture 3
29
Related documents
Download