Bash Lab
Basic bash programming
- Start with
#! /bin/bash
- Special variables:
- Name of script:
$0
- Arguments:
$1
, $2
, $3
, …
- Argument count:
$#
- Result of last command:
$?
(0 for success, >0 for failure)
- Quoting:
- Quote special characters to prevent them from acting specially:
echo All your files: *; echo "A literal star: *"
- Variables are evaluated inside double quotes:
echo "I am $USER"
- Variables are not evaluated inside single quotes:
echo 'Cost: $0.24'
- String variables:
- Use
$
to get the value of a variable; not when assigning it.
- e.g.,
alpha="foo bar"; echo "I say $alpha"
- Capture command output:
now=$(date); echo "Current time: $now"
- Numeric variables:
let beta=2+2; echo "beta is $beta"
- Control constructs:
if
condition then
statements fi
if
condition then
statements else
statements fi
if
condition then
statements elif
condition then
statements else
statements fi
while
condition do
statements done
for
variable in
list do
statements done
The list can be an explicit list:
for beatle in john paul george ringo
or a filename pattern:
for song in *.mp3
- A condition can be:
((
arithmetic condition ))
- E.g.,
if (( age < 24 || height == 32)) then …
[[
string condition ]]
while [[ $suffix = "txt" ]] do …
[[
file condition ]]
if [[ -x index.txt ]] then …
Is this an executable file?
if [[ -r $filename ]] then …
Is this a readable file?
if [[ -e $filename ]] then …
Does this file exist?
- Any command that succeeds or fails:
if grep -q foobar $datafile then …
- Routing output:
|
connects output of first command to input of second command:
grep "foo.*bar" filename | sort
>
sends standard output (non-error output) to a file:
grep "^foo" filename >results
>&2
sends standard output to standard error:
echo "$0: Don’t do that!" >&2
Script
Here is a small bash script. It changes file suffixes:
                
#! /bin/bash
old_suffix=$1
new_suffix=$2
for f in *.$old_suffix
do
new_name=${f%.*}.$new_suffix
echo Rename $f to $new_name
done
You might use it like this, to rename all of your C files
(ending in .c
) to be C++ files (ending in .cpp
):
                
./rn c cpp
Copy the script, above, to the file rn
. No suffix—just the
two-character filename rn
with nothing after. You will modify
rn
, and turn it in for credit.
                
For this lab, you should:
                
- Understand how the script works
- Understand why we said
./rn
as opposed to just rn
- Copy & paste this into your own file
rn
, and try it out
- Make it actually rename the file, rather than just saying so
- Add a usage message if too many or too few arguments are given
- Send the usage message to standard error
- Complain (to standard error) if a file can’t be renamed
- Not clobber existing files
For extra fame & glory, you could:
                
- Add a
-n
option that doesn’t rename,
but just says what it would do
- Add a
-v
option that renames verbosely
- Complain if no files have the given suffix
- Make it work for filenames and suffixes that contain spaces
- Make it work for suffixes that are wildcards (whatever that means)
How to submit your work:
In Canvas, check in the
file
rn
to the assignment
“Lab02”.
                
How to receive negative points:
Turn in someone else’s work.