initial entry

This commit is contained in:
gferg 2000-07-24 15:59:24 +00:00
parent d7f419a795
commit cd5d2bd870
84 changed files with 12205 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
# This is a data file loaded by a script.
# Files of this type may contain variables, functions, etc.
# It may be loaded with a 'source' or '.' command by a shell script.
# Let's initialize some variables.
variable1=22
variable2=474
variable3=5
variable4=97
message1="Hello, how are you?"
message2="Enough for now. Goodbye."
print_message ()
{
# Echoes any message passed to it.
if [ -z $1 ]
then
return 1
# Error, if argument missing.
fi
echo
until [ -z "$1" ]
do
# Step through arguments passed to function.
echo -n "$1"
# Echo args one at a time, suppressing line feeds.
echo -n " "
# Insert spaces between words.
shift
# Next one.
done
echo
return 0
}

View File

@ -0,0 +1,7 @@
# cleanup
# Run as root, of course.
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."

View File

@ -0,0 +1,35 @@
#!/bin/bash
if [ 0 ]
#zero
then
echo "0 is true."
else
echo "0 is false."
fi
if [ ]
#NULL (empty condition)
then
echo "NULL is true."
else
echo "NULL is false."
fi
if [ xyz ]
#string
then
echo "Random string is true."
else
echo "Random string is false."
fi
if [ $xyz ]
#string
then
echo "Undeclared variable is true."
else
echo "Undeclared variable is false."
fi
exit 0

View File

@ -0,0 +1,27 @@
#!/bin/bash
echo
if test -z $1
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
# Both code blocks are functionally identical.
if [ -z $1 ]
# if [ -z $1
# also works, but outputs an error message.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
exit 0

View File

@ -0,0 +1,19 @@
#!/bin/bash
# This line is a comment.
filename=sys.log
if [ ! -f $filename ]
then
touch $filename; echo "Creating file."
else
cat /dev/null > $filename; echo "Cleaning out file."
fi
# Of course, /var/log/messages must have
# world read permission (644) for this to work.
tail /var/log/messages > $filename
echo "$filename contains tail end of system log."
exit 0

View File

@ -0,0 +1,26 @@
#!/bin/bash
a=4
b=5
# Here a and b can be treated either as integers or strings.
# There is some blurring between the arithmetic and integer comparisons.
# Be careful.
if [ $a -ne $b ]
then
echo "$a is not equal to $b"
echo "(arithmetic comparison)"
fi
echo
if [ $a != $b ]
then
echo "$a is not equal to $b."
echo "(string comparison)"
fi
echo
exit 0

View File

@ -0,0 +1,38 @@
#!/bin/bash
#View gzipped files with 'most'
NOARGS=1
if [ $# = 0 ]
# same effect as: if [ -z $1 ]
then
echo "Usage: `basename $0` filename" >&2
# Error message to stderr.
exit $NOARGS
# Returns 1 as exit status of script
# (error code)
fi
filename=$1
if [ ! -f $filename ]
then
echo "File $filename not found!" >&2
# Error message to stderr.
exit 2
fi
if [ ${filename##*.} != "gz" ]
# Using bracket in variable substitution.
then
echo "File $1 is not a gzipped file!"
exit 3
fi
zcat $1 | most
exit 0
# Uses the file viewer 'most'
# (similar to 'less')

View File

@ -0,0 +1,19 @@
#!/bin/bash
#When is a variable "naked", i.e., lacking the '$' in front?
# Assignment
a=879
echo $a
# Assignment using 'let'
let a=16+5
echo $a
# In a 'for' loop (really, a type of disguised assignment)
for a in 7 8 9 11
do
echo $a
done
exit 0

View File

@ -0,0 +1,19 @@
#!/bin/bash
a=23
# Simple case
echo $a
b=$a
echo $b
# Now, getting a little bit fancier...
a=`echo Hello!`
# Assigns result of 'echo' command to 'a'
echo $a
a=`ls -l`
# Assigns result of 'ls -l' command to 'a'
echo $a
exit 0

View File

@ -0,0 +1,30 @@
#!/bin/bash
echo
echo The name of this script is $0
# Adds ./ for current directory
echo The name of this script is `basename $0`
# Strip out path name info (see 'basename')
echo
if [ $1 ]
then
echo "Parameter #1 is $1"
# Need quotes to escape #
fi
if [ $2 ]
then
echo "Parameter #2 is $2"
fi
if [ $3 ]
then
echo "Parameter #3 is $3"
fi
echo
exit 0

View File

@ -0,0 +1,30 @@
#!/bin/bash
# Does a 'whois domain-name' lookup
# on any of 3 alternate servers:
# ripe.net, cw.net, radb.net
# Place this script, named 'wh' in /usr/local/bin
# Requires symbolic links:
# ln -s /usr/local/bin/wh /usr/local/bin/wh-ripe
# ln -s /usr/local/bin/wh /usr/local/bin/wh-cw
# ln -s /usr/local/bin/wh /usr/local/bin/wh-radb
if [ -z $1 ]
then
echo "Usage: `basename $0` [domain-name]"
exit 1
fi
case `basename $0` in
# Checks script name and calls proper server
"wh" ) whois $1@whois.ripe.net;;
"wh-ripe") whois $1@whois.ripe.net;;
"wh-radb") whois $1@whois.radb.net;;
"wh-cw" ) whois $1@whois.cw.net;;
* ) echo "Usage: `basename $0` [domain-name]";;
esac
exit 0

View File

@ -0,0 +1,19 @@
#!/bin/bash
# Name this script something like shift000,
# and invoke it with some parameters, for example
# ./shift000 a b c def 23 skidoo
# Demo of using 'shift'
# to step through all the positional parameters.
until [ -z "$1" ]
do
echo -n "$1 "
shift
done
echo
# Extra line feed.
exit 0

View File

@ -0,0 +1,28 @@
#!/bin/bash
# cleanup, version 2
# Run as root, of course.
if [ -n $1 ]
# Test if command line argument present.
then
lines=$1
else
lines=50
# default, if not specified on command line.
fi
cd /var/log
tail -$lines messages > mesg.temp
# Saves last section of message log file.
mv mesg.temp messages
# cat /dev/null > messages
# No longer needed, as the above method is safer.
cat /dev/null > wtmp
echo "Logs cleaned up."
exit 0
# A zero return value from the script upon exit
# indicates success to the shell.

View File

@ -0,0 +1,32 @@
#!/bin/bash
declare -f
# Lists the function below.
func1 ()
{
echo This is a function.
}
declare -r var1=13.36
echo "var1 declared as $var1"
# Attempt to change readonly variable.
var1=13.37
# Generates error message.
echo "var1 is still $var1"
echo
declare -i var2
var2=2367
echo "var2 declared as $var2"
var2=var2+1
# Integer declaration eliminates the need for 'let'.
echo "var2 incremented by 1 is $var2."
# Attempt to change variable declared as integer
echo "Attempting to change var2 to floating point value, 2367.1."
var2=2367.1
# results in error message, with no change to variable.
echo "var2 is still $var2"
exit 0

View File

@ -0,0 +1,9 @@
#!/bin/bash
# Prints different random integer
# at each invocation.
a=$RANDOM
echo $a
exit 0

View File

@ -0,0 +1,16 @@
#!/bin/bash
for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
echo $planet
done
echo
# Entire 'list' enclosed in quotes creates a single variable.
for planet in "Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"
do
echo $planet
done
exit 0

View File

@ -0,0 +1,15 @@
#!/bin/bash
# Invoke both with and without arguments,
# and see what happens.
for a
do
echo $a
done
# 'in list' missing, therefore
# operates on '$#'
# (command-line argument list)
exit 0

View File

@ -0,0 +1,31 @@
#!/bin/bash
if [ $# -ne 2 ]
# Check for proper no. of command line args.
then
echo "Usage: `basename $0` phone# text-file"
exit 1
fi
if [ ! -f $2 ]
then
echo "File $2 is not a text file"
exit 2
fi
# Create fax formatted files from text files.
fax make $2
for file in $(ls $2.0*)
# Concatenate the converted files.
# Uses wild card in variable list.
do
fil="$fil $file"
done
# Do the work.
efax -d /dev/ttyS3 -o1 -t "T$1" $fil
exit 0

View File

@ -0,0 +1,15 @@
#!/bin/bash
var0=0
while [ "$var0" -lt 10 ]
do
echo -n "$var0 "
# -n suppresses newline.
var0=`expr $var0 + 1`
# var0=$(($var0+1)) also works.
done
echo
exit 0

View File

@ -0,0 +1,18 @@
#!/bin/bash
while [ "$var1" != end ]
do
echo "Input variable #1 "
echo "(end to exit)"
read var1
# It's not 'read $var1'
# because value of var1 is set.
echo "variable #1 = $var1"
# Need quotes because of #
done
# Note: Echoes 'end' because
# termination condition
# tested for at top of loop.
exit 0

View File

@ -0,0 +1,12 @@
#!/bin/bash
until [ "$var1" = end ]
# Tests condition at top of loop.
do
echo "Input variable #1 "
echo "(end to exit)"
read var1
echo "variable #1 = $var1"
done
exit 0

View File

@ -0,0 +1,53 @@
#!/bin/bash
echo
echo Printing Numbers 1 through 20.
a=0
while [ $a -le 19 ]
do
a=$(($a+1))
if [ $a -eq 3 ] || [ $a -eq 11 ]
# Excludes 3 and 11
then
continue
# Skip rest of this particular loop iteration.
fi
echo -n "$a "
done
# Exercise for reader:
# Why does loop print up to 20?
echo
echo
echo Printing Numbers 1 through 20, but something happens after 2.
##################################################################
# Same loop, but substituting 'break' for 'continue'.
a=0
while [ $a -le 19 ]
do
a=$(($a+1))
if [ $a -gt 2 ]
then
break
# Skip entire rest of loop.
fi
echo -n "$a "
done
echo
echo
exit 0

View File

@ -0,0 +1,15 @@
#!/bin/bash
echo
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[a-z] ) echo "Lowercase letter";;
[A-Z] ) echo "Uppercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac
# Allows ranges of characters in [square brackets].
exit 0

View File

@ -0,0 +1,15 @@
#!/bin/bash
# This is a simple script
# that removes blank lines
# from a file.
# No argument checking.
# Same as
# sed -e '/^$/d $1' filename
# invoked from the command line.
sed -e /^$/d $1
# '^' is beginning of line,
# '$' is end,
# and 'd' is delete.

View File

@ -0,0 +1,62 @@
#!/bin/bash
# Crude rolodex-type database
clear
# Clear the screen.
echo " Contact List"
echo " ------- ----"
echo "Choose one of the following persons:"
echo
echo "[E]vans, Roland"
echo "[J]ones, Mildred"
echo "[Smith], Julie"
echo "[Z]ane, Morris"
echo
read person
case "$person" in
# Note variable is quoted.
"E" | "e" )
# Accept upper or lowercase input.
echo
echo "Roland Evans"
echo "4321 Floppy Dr."
echo "Hardscrabble, CO 80753"
echo "(303) 734-9874"
echo "(303) 734-9892 fax"
echo "revans@zzy.net"
echo "Business partner & old friend"
;;
# Note double semicolon to terminate
# each option.
"J" | "j" )
echo
echo "Mildred Jones"
echo "249 E. 7th St., Apt. 19"
echo "New York, NY 10009"
echo "(212) 533-2814"
echo "(212) 533-9972 fax"
echo "milliej@loisaida.com"
echo "Girlfriend"
echo "Birthday: Feb. 11"
;;
# Add info for Smith & Zane later.
* )
# Default option.
echo
echo "Not yet in database."
;;
esac
echo
exit 0

View File

@ -0,0 +1,18 @@
#!/bin/bash
PS3='Choose your favorite vegetable: '
# Sets the prompt string.
echo
select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break
# if no 'break' here, keeps looping forever.
done
exit 0

View File

@ -0,0 +1,24 @@
#!/bin/bash
PS3='Choose your favorite vegetable: '
echo
choice_of()
{
select vegetable
# [in list] omitted, so 'select' uses arguments passed to function.
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break
done
}
choice_of beans rice carrots radishes tomatoes spinach
# $1 $2 $3 $4 $5 $6
# passed to choice_of() function
exit 0

View File

@ -0,0 +1,40 @@
#!/bin/bash
# 'getopts' processes command line args to script.
# Usage: scriptname -options
# Note: dash (-) necessary
# Try invoking this script with
# 'scriptname -mn'
# 'scriptname -oq qOption'
# (qOption can be some arbitrary string.)
OPTERROR=33
if [ -z $1 ]
# Exit and complain if no argument(s) given.
then
echo "Usage: `basename $0` options (-mnopqrs)"
exit $OPTERROR
fi
while getopts ":mnopq:rs" Option
do
case $Option in
m ) echo "Scenario #1: option -m-";;
n | o ) echo "Scenario #2: option -$Option-";;
p ) echo "Scenario #3: option -p-";;
q ) echo "Scenario #4: option -q-, with argument \"$OPTARG\"";;
# Note that option 'q' must have an additional argument,
# otherwise nothing happens.
r | s ) echo "Scenario #5: option -$Option-"'';;
* ) echo "Unimplemented option chosen.";;
esac
done
shift $(($OPTIND - 1))
# Decrements the argument pointer
# so it points to next argument.
exit 0

View File

@ -0,0 +1,27 @@
#!/bin/bash
# script "set-test"
# Invoke this script with three command line parameters,
# for example, "./set-test one two three".
echo
echo "Positional parameters before set \`uname -a\` :"
echo "Command-line argument #1 = $1"
echo "Command-line argument #2 = $2"
echo "Command-line argument #3 = $3"
echo
set `uname -a`
# Sets the positional parameters to the output
# of the command `uname -a`
echo "Positional parameters after set \`uname -a\` :"
# $1, $2, $3, etc. reinitialized to result of `uname -a`
echo "Field #1 of 'uname -a' = $1"
echo "Field #2 of 'uname -a' = $2"
echo "Field #3 of 'uname -a' = $3"
echo
exit 0

View File

@ -0,0 +1,8 @@
#!/bin/bash
a=/home/heraclius/daily-journal.txt
echo "Basename of /home/heraclius/daily-journal.txt = `basename $a`"
echo "Dirname of /home/heraclius/daily-journal.txt = `dirname $a`"
exit 0

View File

@ -0,0 +1,12 @@
#!/bin/bash
echo -n "Enter the value of variable 'var1': "
# -n option to echo suppresses newline
read var1
# Note no '$' in front of var1,
# since it is being set.
echo "var1 = $var1"
exit 0

View File

@ -0,0 +1,22 @@
#!/bin/bash
dir1=/usr/local
dir2=/var/spool
pushd $dir1
# Will do an automatic 'dirs'
# (list directory stack to stdout).
echo "Now in directory `pwd`."
# Uses back-quoted 'pwd'.
# Now, do some stuff in directory 'dir1'.
pushd $dir2
echo "Now in directory `pwd`."
# Now, do some stuff in directory 'dir2'.
echo "The top entry in the DIRSTACK array is $DIRSTACK."
popd
echo "Now back in directory `pwd`."
# Now, do some more stuff in directory 'dir1'.
popd
echo "Now back in original working directory `pwd`."
exit 0

View File

@ -0,0 +1,23 @@
#!/bin/bash
# Load a data file.
. data-file
# Same effect as "source data-file"
# Note that the file "data-file", given below
# must be present in working directory.
# Now, reference some data from that file.
echo "variable1 (from data-file) = $variable1"
echo "variable3 (from data-file) = $variable3"
let "sum = $variable2 + $variable4"
echo "Sum of variable2 + variable4 (from data-file) = $sum"
echo "message1 (from data-file) is \"$message1\""
# Note: escaped quotes
print_message This is the message-print function in the data-file.
exit 0

View File

@ -0,0 +1,23 @@
#!/bin/bash
if [ -z $1 ]
then
echo "Usage: `basename $0` find-string"
exit 1
fi
echo "Updating 'locate' database..."
echo "This may take a while."
updatedb /usr &
# Must be run as root.
wait
# Don't run the rest of the script
# until 'updatedb' finished.
# In this case, you want the the database updated
# before looking up the file name.
locate $1
exit 0

View File

@ -0,0 +1,34 @@
#!/bin/bash
# "subst", a script that substitutes one pattern for
# another in a file,
# i.e., "subst Smith Jones letter.txt".
if [ $# -ne 3 ]
# Test number of arguments to script
# (always a good idea).
then
echo "Usage: `basename $0` old-pattern new-pattern filename"
exit 1
fi
old_pattern=$1
new_pattern=$2
if [ -f $3 ]
then
file_name=$3
else
echo "File \"$3\" does not exist."
exit 2
fi
# Here is where the heavy work gets done.
sed -e "s/$old_pattern/$new_pattern/" $file_name
# 's' is, of course, the substitute command in sed,
# and /pattern/ invokes address matching.
# Read the literature on 'sed' for a more
# in-depth explanation.
exit 0
# Successful invocation of the script returns 0.

View File

@ -0,0 +1,30 @@
#!/bin/bash
# Script to automate burning a CDR.
# Uses Joerg Schilling's "cdrecord" package
# (http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html)
# If this script invoked as an ordinary user, need to suid cdrecord
# (chmod u+s /usr/bin/cdrecord, as root).
if [ -z $1 ]
then
IMAGE_DIRECTORY=/opt
# Default directory, if not specified on command line.
else
IMAGE_DIRECTORY=$1
fi
ls -lR $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/contents
echo "Creating table of contents."
mkisofs -r -o cdimage.iso $IMAGE_DIRECTORY
echo "Creating ISO9660 file system image (cdimage.iso)."
cdrecord -v -isosize speed=2 dev=0,0 cdimage.iso
# Change speed parameter to speed of your burner.
echo "Burning the disk."
echo "Please be patient, this will take a while."
exit 0

View File

@ -0,0 +1,16 @@
#!/bin/bash
# Generates a log file in current directory
# from the tail end of /var/log messages.
# Note: /var/log/messages must be readable by ordinary users
# if invoked by same (#root chmod 755 /var/log/messages).
( date; uname -a ) >>logfile
# Time and machine name
echo --------------------------------------------------------------------- >>logfile
tail -5 /var/log/messages | xargs | fmt -s >>logfile
echo >>logfile
echo >>logfile
exit 0

View File

@ -0,0 +1,17 @@
#!/bin/bash
# Copy (verbose) all files in current directory
# to directory specified on command line.
if [ -z $1 ]
# Exit if no argument given.
then
echo "Usage: `basename $0` directory-to-copy-to"
exit 1
fi
ls . | xargs -i -t cp ./{} $1
# This is the exact equivalent of
# cp * $1
exit 0

View File

@ -0,0 +1,10 @@
#!/bin/bash
y=`eval ls -l`
echo $y
y=`eval df`
echo $y
# Note that LF's not preserved
exit 0

View File

@ -0,0 +1,19 @@
#!/bin/bash
y=`eval ps ax | sed -n '/ppp/p' | awk '{ print $1 }'`
# Finding the process number of 'ppp'
kill -9 $y
# Killing it
# Restore to previous state...
chmod 666 /dev/ttyS3
# Doing a SIGKILL on ppp changes the permissions
# on the serial port. Must be restored.
rm /var/lock/LCK..ttyS3
# Remove the serial port lock file.
exit 0

View File

@ -0,0 +1,87 @@
#!/bin/bash
# Demonstrating some of the uses of 'expr'
# +++++++++++++++++++++++++++++++++++++++
echo
# Arithmetic Operators
echo Arithmetic Operators
echo
a=`expr 5 + 3`
echo 5 + 3 = $a
a=`expr $a + 1`
echo
echo a + 1 = $a
echo \(incrementing a variable\)
a=`expr 5 % 3`
# modulo
echo
echo 5 mod 3 = $a
echo
echo
# Logical Operators
echo Logical Operators
echo
a=3
echo a = $a
b=`expr $a \> 10`
echo 'b=`expr $a \> 10`, therefore...'
echo "If a > 10, b = 0 (false)"
echo b = $b
b=`expr $a \< 10`
echo "If a < 10, b = 1 (true)"
echo b = $b
echo
echo
# Comparison Operators
echo Comparison Operators
echo
a=zipper
echo a is $a
if [ `expr $a = snap` ]
# Force re-evaluation of variable 'a'
then
echo "a is not zipper"
fi
echo
echo
# String Operators
echo String Operators
echo
a=1234zipper43231
echo The string being operated upon is $a.
# index: position of substring
b=`expr index $a 23`
echo Numerical position of first 23 in $a is $b.
# substr: print substring, starting position & length specified
b=`expr substr $a 2 6`
echo Substring of $a, starting at position 2 and 6 chars long is $b.
# length: length of string
b=`expr length $a`
echo Length of $a is $b.
# 'match' operations similarly to 'grep'
b=`expr match $a [0-9]*`
echo Number of digits at the beginning of $a is $b.
b=`expr match $a '\([0-9]*\)'`
echo The digits at the beginning of $a are $b.
echo
exit 0

View File

@ -0,0 +1,26 @@
#!/bin/bash
echo
let a=11
# Same as 'a=11'
let a=a+5
# Equivalent to let "a = a + 5"
# (double quotes makes it more readable)
echo "a = $a"
let "a <<= 3"
# Equivalent of let "a = a << 3"
echo "a left-shifted 3 places = $a"
let "a /= 4"
# Equivalent to let "a = a / 4"
echo $a
let "a -= 5"
# Equivalent to let "a = a - 5"
echo $a
let "a = a * 10"
echo $a
let "a %= 8"
echo $a
exit 0

View File

@ -0,0 +1,27 @@
#!/bin/bash
# printf demo
PI=3.14159265358979
DecimalConstant=31373
Message1="Greetings,"
Message2="Earthling."
echo
printf "Pi to 2 decimal places = %1.2f" $PI
echo
printf "Pi to 9 decimal places = %1.9f" $PI
# Note correct round off.
printf "\n"
# Prints a line feed, equivalent to 'echo'.
printf "Constant = \t%d\n" $DecimalConstant
# Insert tab (\t)
printf "%s %s \n" $Message1 $Message2
echo
exit 0

View File

@ -0,0 +1,16 @@
#!/bin/bash
# Copying a directory tree using cpio.
if [ $# -ne 2 ]
then
echo Usage: `basename $0` source destination
exit 1
fi
source=$1
destination=$2
find "$source" -depth | cpio -admvp "$destination"
exit 0

View File

@ -0,0 +1,14 @@
#!/bin/bash
# Changes a file to all uppercase.
if [ -z $1 ]
# Standard check whether command line arg is present.
then
echo "Usage: `basename $0` filename"
exit 1
fi
tr [a-z] [A-Z] <$1
exit 0

View File

@ -0,0 +1,20 @@
#!/bin/bash
echo hello
echo $?
# exit status 0 returned
# because command successful.
lskdf
# bad command
echo $?
# non-zero exit status returned.
echo
exit 143
# Will return 143 to shell.
# To verify this, type $? after script terminates.
# By convention, an 'exit 0' shows success,
# while a non-zero exit value indicates an error.

View File

@ -0,0 +1,13 @@
#!/bin/bash
# Get a file listing...
b=`ls /usr/local/bin`
# ...40 columns wide.
echo $b | fmt -w 40
# Could also have been done by
# echo $b | fold - -s -w 40
exit 0

View File

@ -0,0 +1,24 @@
#!/bin/bash
#Using the 'date' command
# Needs a leading '+' to invoke formatting.
echo "The number of days since the year's beginning is `date +%j`."
# %j gives day of year.
echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
# %s yields number of seconds since "UNIX epoch" began,
# but how is this useful?
prefix=temp
suffix=`eval date +%s`
filename=$prefix.$suffix
echo $filename
# It's great for creating "unique" temp filenames,
# even better than using $$.
# Read the 'date' man page for more formatting options.
exit 0

View File

@ -0,0 +1,23 @@
#!/bin/bash
lines=35
# Allow 35 lines for the header (very generous).
for File in *
# Test all the files in the current working directory...
do
search1=`head -$lines $File | grep begin | wc -w`
search2=`tail -$lines $File | grep end | wc -w`
# Files which are uuencoded have a "begin" near the beginning,
# and an "end" near the end.
if [ $search1 -gt 0 ]
then
if [ $search2 -gt 0 ]
then
echo "uudecoding - $File -"
uudecode $File
fi
fi
done
exit 0

View File

@ -0,0 +1,12 @@
#!/bin/bash
for a in `seq 80`
# Same as for a in 1 2 3 4 5 ... 80 (saves much typing!).
# May also use 'jot' (if present on system).
do
echo -n "$a "
done
echo
exit 0

View File

@ -0,0 +1,9 @@
#!/bin/bash
exec echo "Exiting $0."
# Exit from script.
# The following lines never execute.
echo "Still here?"
exit 0

View File

@ -0,0 +1,38 @@
#!/bin/sh
# --> Comments added by the author of this HOWTO marked by "-->".
# --> This is part of the 'rc' script package
# --> by Miquel van Smoorenburg, &lt;miquels@drinkel.nl.mugnet.org>
# Bring down all unneeded services that are still running (there shouldn't
# be any, so this is just a sanity check)
for i in /var/lock/subsys/*; do
# --> Standard for/in loop, but since "do" is on same line,
# --> it is necessary to add ";".
# Check if the script is there.
[ ! -f $i ] && continue
# --> This is a clever use of an "and list", equivalent to:
# --> if [ ! -f $i ]; then continue
# Get the subsystem name.
subsys=${i#/var/lock/subsys/}
# --> Match variable name, which, in this case, is the file name.
# --> This is the exact equivalent of subsys=`basename $i`.
# --> It gets it from the lock file name, and since if there
# --> is a lock file, that's proof the process has been running.
# --> See the "lockfile" entry, above.
# Bring the subsystem down.
if [ -f /etc/rc.d/init.d/$subsys.init ]; then
/etc/rc.d/init.d/$subsys.init stop
else
/etc/rc.d/init.d/$subsys stop
# --> Suspend running jobs and daemons
# --> using the 'stop' shell builtin.
fi
done

View File

@ -0,0 +1,7 @@
#!/bin/bash
perl -e 'print "This is an embedded Perl script\n"'
# Some shell commands may follow.
exit 0

View File

@ -0,0 +1,18 @@
#!/bin/bash
# Delete filenames in current directory containing bad characters.
for filename in *
do
badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p`
# Files containing those nasties: + { ; " \ = ? ~ ( ) < > & * | $
rm $badname 2>/dev/null
# So error messages deep-sixed.
done
# Now, take care of files containing all manner of whitespace.
find . -name "* *" -exec rm -f {} \;
# The "{}" references the paths of all the files that "find" finds.
# The '\' ensures that the ';' is interpreted literally, as end of command.
exit 0

View File

@ -0,0 +1,16 @@
#!/bin/bash
# Backs up all files in current directory
# modified within last 24 hours
# in a tarred and gzipped file.
if [ $# = 0 ]
then
echo "Usage: `basename $0` filename"
exit 1
fi
tar cvf - `find . -mtime -1 -type f -print` > $1.tar
gzip $1.tar
exit 0

View File

@ -0,0 +1,15 @@
#!/bin/bash
funky ()
{
echo This is a funky function.
echo Now exiting funky function.
}
# Note: function must precede call.
# Now, call the function.
funky
exit 0

View File

@ -0,0 +1,9 @@
#!/bin/bash
: ${HOSTNAME?} {USER?} {MAIL?}
echo $HOSTNAME
echo $USER
echo $MAIL
echo Critical env. variables set.
exit 0

View File

@ -0,0 +1,31 @@
#!/bin/bash
func2 () {
if [ -z $1 ]
# Checks if any params.
then
echo "No parameters passed to function."
return 0
else
echo "Param #1 is $1."
fi
if [ $2 ]
then
echo "Parameter #2 is $2."
fi
}
func2
# Called with no params
echo
func2 first
# Called with one param
echo
func2 first second
# Called with two params
echo
exit 0

View File

@ -0,0 +1,67 @@
#!/bin/bash
# Arabic number to Roman numeral conversion
# Range 0 - 200
# It's crude, but it works.
# Extending the range and otherwise improving the script
# is left as an exercise for the reader.
# Usage: roman number-to-convert
ARG_ERR=1
OUT_OF_RANGE=200
if [ -z $1 ]
then
echo "Usage: `basename $0` number-to-convert"
exit $ARG_ERR
fi
num=$1
if [ $num -gt $OUT_OF_RANGE ]
then
echo "Out of range!"
exit $OUT_OF_RANGE
fi
to_roman ()
{
number=$1
factor=$2
rchar=$3
let "remainder = number - factor"
while [ $remainder -ge 0 ]
do
echo -n $rchar
let "number -= factor"
let "remainder = number - factor"
done
return $number
}
# Note: must declare function
# before first call to it.
to_roman $num 100 C
num=$?
to_roman $num 90 LXXXX
num=$?
to_roman $num 50 L
num=$?
to_roman $num 40 XL
num=$?
to_roman $num 10 X
num=$?
to_roman $num 9 IX
num=$?
to_roman $num 5 V
num=$?
to_roman $num 4 IV
num=$?
to_roman $num 1 I
echo
exit 0