This commit is contained in:
gferg 2004-02-16 17:21:33 +00:00
parent 2947233ee6
commit f9557f6c3c
7 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#!/bin/bash
# archiveweblogs.sh v1.0
# Troy Engel <tengel@fluid.com>
# Slightly modified by document author.
# Used with permission.
#
# This script will preserve the normally rotated and
#+ thrown away weblogs from a default RedHat/Apache installation.
# It will save the files with a date/time stamp in the filename,
#+ bzipped, to a given directory.
#
# Run this from crontab nightly at an off hour,
#+ as bzip2 can suck up some serious CPU on huge logs:
# 0 2 * * * /opt/sbin/archiveweblogs.sh
PROBLEM=66
# Set this to your backup dir.
BKP_DIR=/opt/backups/weblogs
# Default Apache/RedHat stuff
LOG_DAYS="4 3 2 1"
LOG_DIR=/var/log/httpd
LOG_FILES="access_log error_log"
# Default RedHat program locations
LS=/bin/ls
MV=/bin/mv
ID=/usr/bin/id
CUT=/bin/cut
COL=/usr/bin/column
BZ2=/usr/bin/bzip2
# Are we root?
USER=`$ID -u`
if [ "X$USER" != "X0" ]; then
echo "PANIC: Only root can run this script!"
exit $PROBLEM
fi
# Backup dir exists/writable?
if [ ! -x $BKP_DIR ]; then
echo "PANIC: $BKP_DIR doesn't exist or isn't writable!"
exit $PROBLEM
fi
# Move, rename and bzip2 the logs
for logday in $LOG_DAYS; do
for logfile in $LOG_FILES; do
MYFILE="$LOG_DIR/$logfile.$logday"
if [ -w $MYFILE ]; then
DTS=`$LS -lgo --time-style=+%Y%m%d $MYFILE | $COL -t | $CUT -d ' ' -f7`
$MV $MYFILE $BKP_DIR/$logfile.$DTS
$BZ2 $BKP_DIR/$logfile.$DTS
else
# Only spew an error if the file exits (ergo non-writable).
if [ -f $MYFILE ]; then
echo "ERROR: $MYFILE not writable. Skipping."
fi
fi
done
done
exit 0

View File

@ -0,0 +1,46 @@
#!/bin/bash
# bad-op.sh: Trying to use a string comparison on integers.
echo
number=1
# The following "while loop" has two errors:
#+ one blatant, and the other subtle.
while [ "$number" < 5 ] # Wrong! Should be: while [ "$number" -lt 5 ]
do
echo -n "$number "
let "number += 1"
done
# Attempt to run this bombs with the error message:
#+ bad-op.sh: line 10: 5: No such file or directory
# Within single brackets, "<" must be escaped,
#+ and even then, it's still wrong for comparing integers.
echo "---------------------"
while [ "$number" \< 5 ] # 1 2 3 4
do #
echo -n "$number " # This *seems to work, but . . .
let "number += 1" #+ it actually does an ASCII comparison,
done #+ rather than a numerical one.
echo; echo "---------------------"
# This can cause problems. For example:
lesser=5
greater=105
if [ "$greater" \< "$lesser" ]
then
echo "$greater is less than $lesser"
fi # 105 is less than 5
# In fact, "105" actually is less than "5"
#+ in a string comparison (ASCII sort order).
echo
exit 0

View File

@ -0,0 +1,22 @@
#!/bin/bash
# dereference.sh
# Dereferencing parameter passed to a function.
# Script by Bruce W. Clare.
dereference ()
{
y=\$"$1" # Name of variable.
echo $y # $Junk
x=`eval "expr \"$y\" "`
echo $1=$x
eval "$1=\"Some Different Text \"" # Assign new value.
}
Junk="Some Text"
echo $Junk "before" # Some Text before
dereference Junk
echo $Junk "after" # Some Different Text after
exit 0

View File

@ -0,0 +1,36 @@
#!/bin/bash
# dev-tcp.sh: /dev/tcp redirection to check Internet connection.
# Script by Troy Engel.
# Used with permission.
TCP_HOST=www.slashdot.org
TCP_PORT=80 # Port 80 is http.
# Try to connect. (Somewhat similar to a 'ping.')
echo "HEAD / HTTP/1.0" >/dev/tcp/${TCP_HOST}/${TCP_PORT}
MYEXIT=$?
: << EXPLANATION
If bash was compiled with --enable-net-redirections, it has the capability of
using a special character device for both TCP and UDP redirections. These
redirections are used identically as STDIN/STDOUT/STDERR. The device entries
are 30,36 for /dev/tcp:
mknod /dev/tcp c 30 36
>From the bash reference:
/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is an integer
port number or service name, Bash attempts to open a TCP connection to the
corresponding socket.
EXPLANATION
if [ "X$MYEXIT" = "X0" ]; then
echo "Connection successful. Exit code: $MYEXIT"
else
echo "Connection unsuccessful. Exit code: $MYEXIT"
fi
exit $MYEXIT

View File

@ -0,0 +1,23 @@
#!/bin/bash
# ind-func.sh: Passing an indirect reference to a function.
echo_var ()
{
echo "$1"
}
message=Hello
Hello=Goodbye
echo_var "$message" # Hello
# Now, let's pass an indirect reference to the function.
echo_var "${!message}" # Goodbye
echo "-------------"
# What happens if we change the contents of "hello" variable?
Hello="Hello, again!"
echo_var "$message" # Hello
echo_var "${!message}" # Hello, again!
exit 0

View File

@ -0,0 +1,33 @@
#!/bin/bash
# kill-byname.sh: Killing processes by name.
# Compare this script with kill-process.sh.
# For instance,
#+ try "./kill-byname.sh xterm" --
#+ and watch all the xterms on your desktop disappear.
# Warning:
# -------
# This is a fairly dangerous script.
# Running it carelessly (especially as root)
#+ can cause data loss and other undesirable effects.
E_BADARGS=66
if test -z "$1" # No command line arg supplied?
then
echo "Usage: `basename $0` Process(es)_to_kill"
exit $E_BADARGS
fi
# ---------------------------------------------------------
# Notes:
# -i is the "replace strings" option to xargs.
# The curly braces are the placeholder for the replacement.
# 2&>/dev/null suppresses unwanted error messages.
# ---------------------------------------------------------
PROCESS_NAME="$1"
ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&>/dev/null
exit $?

View File

@ -0,0 +1,42 @@
#!/bin/bash
# setnew-password.sh: Not a good idea.
# This script must be run as root,
#+ or better yet, not run at all.
ROOT_UID=0 # Root has $UID 0.
E_WRONG_USER=65 # Not root?
E_NOSUCHUSER=70
SUCCESS=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo; echo "Only root can run this script."; echo
exit $E_WRONG_USER
else
echo; echo "You should know better than to run this script, root."
fi
username=bozo
NEWPASSWORD=security_violation
# Check if bozo lives here.
cat /etc/passwd | grep -q "$username"
if [ $? -ne $SUCCESS ]
then
echo "User $username does not exist."
echo "No password changed."
exit $E_NOSUCHUSER
fi
echo "$NEWPASSWORD" | passwd --stdin "$username"
# The '--stdin' option to 'passwd' permits
#+ getting new password from stdin (or a pipe).
echo; echo "User $username's password changed!"
# Using the 'passwd' command in a script is dangerous.
exit 0