UNIX System Programming
Source: csserver.evansville.edu
Topic: Shell Programming
Short Desciption:UNIX System Programming Lecture 4 BASH Shell Programming BLP: Chapter 2 BABS Outline: Redirection ... zsh, ksh Change your default shell to bash $ chsh -s /bin/bash 2 Our First Shell Script A ...
Content Inside:
UNIX System Programming
Lecture 4
BASH Shell Programming
BLP: Chapter 2
BABS
Outline:
Redirection
Shell Programming
Introduction
The shell is a command interpreter and a full-
featured programming language. It is especially
well-suited for system administration and for 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
2
Our First Shell Script
A hello world example
$ cat > hello
#! /bin/bash
# Prompt user for name
echo -n "Enter your name: "
read name
echo Hello there $name!
exit 0
$ chmod +x hello
$ . /hello
Enter your name: Elmer Fudd
Hello there Elmer Fudd!
$
3
Our First Shell Script
Start with a sha-bang (#! ) . The kernel will
automatically pass the script to the proper
interpreter. End with ìexit 0î (or non-zero exit to
indicate an error condition) .
Comments begin with a #
Make the script executable and run it like a
standard program 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)
4
Wildcards (globbing)
Wildcard expansion is done by the shell. Not by
the application. A * matches any character except
a leading dot (. ) . A ? matches any single
character.
$ echo *
a. 1 b. 1 c. 1 t2. sh test1. txt
$ ls t? . sh
t2. sh
$ ls [a-c]*
a. 1 b. 1 c. 1
$ ls {b*, c*, *est*}
b. 1 c. 1 test1. txt
$ ls *. {1, sh}
a. 1 b. 1 c. 1 t2. sh
5
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, and cerr.
By default, they are connected to the keyboard,
the display, and the display but they can be
redirected.
6
Redirection
Heres ...

