mirror of https://github.com/tLDP/LDP
new
This commit is contained in:
parent
cc8c43613b
commit
4fa00f10c4
|
@ -0,0 +1,99 @@
|
|||
#! /bin/bash
|
||||
#
|
||||
# The Towers Of Hanoi
|
||||
# Bash script
|
||||
# Copyright (C) 2000 Amit Singh. All Rights Reserved.
|
||||
# http://hanoi.kernelthread.com
|
||||
#
|
||||
# Last tested under bash version 2.05b.0(13)-release
|
||||
#
|
||||
# Used in "Advanced Bash Scripting Guide"
|
||||
#+ with permission of script author.
|
||||
# Slightly modified and commented by ABS author.
|
||||
|
||||
#=================================================================#
|
||||
# The Tower of Hanoi is an old mathematical puzzle.
|
||||
# There are three vertical posts set in a base.
|
||||
# The first post has a set of annular rings stacked on it.
|
||||
# These rings are flat disks with a hole drilled out of the center,
|
||||
#+ so they can slip over the posts.
|
||||
# The rings have different diameters, and they stack in descending
|
||||
#+ order, according to size.
|
||||
# The smallest ring is on top, and the largest on the bottom.
|
||||
#
|
||||
# The task is to transfer the stack of rings
|
||||
#+ to one of the other posts.
|
||||
# You can move only one ring at a time to another post.
|
||||
# You are permitted to move rings back to the original post.
|
||||
# You may place a smaller ring atop a larger one,
|
||||
#+ but *not* vice versa.
|
||||
# Again, it is forbidden to place a larger ring atop a smaller one.
|
||||
#
|
||||
# For a small number of rings, only a few moves are required.
|
||||
#+ For each additional ring,
|
||||
#+ the required number of moves approximately doubles,
|
||||
#+ and the "strategy" becomes increasingly complicated.
|
||||
#
|
||||
# For more information, see http://hanoi.kernelthread.com.
|
||||
#
|
||||
#
|
||||
# ... ... ...
|
||||
# | | | | | |
|
||||
# _|_|_ | | | |
|
||||
# |_____| | | | |
|
||||
# |_______| | | | |
|
||||
# |_________| | | | |
|
||||
# |___________| | | | |
|
||||
# | | | | | |
|
||||
# .--------------------------------------------------------------.
|
||||
# |**************************************************************|
|
||||
# #1 #2 #3
|
||||
#
|
||||
#=================================================================#
|
||||
|
||||
|
||||
E_NOPARAM=66 # No parameter passed to script.
|
||||
E_BADPARAM=67 # Illegal number of disks passed to script.
|
||||
Moves= # Global variable holding number of moves.
|
||||
# Modifications to original script.
|
||||
|
||||
dohanoi() { # Recursive function.
|
||||
case $1 in
|
||||
0)
|
||||
;;
|
||||
*)
|
||||
dohanoi "$(($1-1))" $2 $4 $3
|
||||
echo move $2 "-->" $3
|
||||
let "Moves += 1" # Modification to original script.
|
||||
dohanoi "$(($1-1))" $4 $3 $2
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
case $# in
|
||||
1)
|
||||
case $(($1>0)) in # Must have at least one disk.
|
||||
1)
|
||||
dohanoi $1 1 3 2
|
||||
echo "Total moves = $Moves"
|
||||
exit 0;
|
||||
;;
|
||||
*)
|
||||
echo "$0: illegal value for number of disks";
|
||||
exit $E_BADPARAM;
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
*)
|
||||
echo "usage: $0 N"
|
||||
echo " Where \"N\" is the number of disks."
|
||||
exit $E_NOPARAM;
|
||||
;;
|
||||
esac
|
||||
|
||||
# Exercises:
|
||||
# ---------
|
||||
# 1) Would commands beyond this point ever be executed?
|
||||
# Why not? (Easy)
|
||||
# 2) Explain the workings of the workings of the "dohanoi" function.
|
||||
# (Difficult)
|
|
@ -0,0 +1,135 @@
|
|||
#!/bin/bash
|
||||
# ==> usb.sh
|
||||
# ==> Script for mounting and installing pen/keychain USB storage devices.
|
||||
# ==> Runs as root at system startup (see below).
|
||||
|
||||
# This code is free software covered by GNU GPL license version 2 or above.
|
||||
# Please refer to http://www.gnu.org/ for the full license text.
|
||||
#
|
||||
# Some code lifted from usb-mount by Michael Hamilton's usb-mount (LGPL)
|
||||
#+ see http://users.actrix.co.nz/michael/usbmount.html
|
||||
#
|
||||
# INSTALL
|
||||
# -------
|
||||
# Put this in /etc/hotplug/usb/diskonkey.
|
||||
# Then look in /etc/hotplug/usb.distmap, and copy all usb-storage entries
|
||||
#+ into /etc/hotplug/usb.usermap, substituting "usb-storage" for "diskonkey".
|
||||
# Otherwise this code is only run during the kernel module invocation/removal
|
||||
#+ (at least in my tests), which defeats the purpose.
|
||||
#
|
||||
# TODO
|
||||
# ----
|
||||
# Handle more than one diskonkey device at one time (e.g. /dev/diskonkey1
|
||||
#+ and /mnt/diskonkey1), etc. The biggest problem here is the handling in
|
||||
#+ devlabel, which I haven't yet tried.
|
||||
#
|
||||
# AUTHOR and SUPPORT
|
||||
# ------------------
|
||||
# Konstantin Riabitsev, <icon linux duke edu>.
|
||||
# Send any problem reports to my email address at the moment.
|
||||
#
|
||||
# ==> Comments added by ABS Guide author.
|
||||
|
||||
|
||||
|
||||
SYMLINKDEV=/dev/diskonkey
|
||||
MOUNTPOINT=/mnt/diskonkey
|
||||
DEVLABEL=/sbin/devlabel
|
||||
DEVLABELCONFIG=/etc/sysconfig/devlabel
|
||||
IAM=$0
|
||||
|
||||
##
|
||||
# Functions lifted near-verbatim from usb-mount code.
|
||||
#
|
||||
function allAttachedScsiUsb {
|
||||
find /proc/scsi/ -path '/proc/scsi/usb-storage*' -type f | xargs grep -l 'Attached: Yes'
|
||||
}
|
||||
function scsiDevFromScsiUsb {
|
||||
echo $1 | awk -F"[-/]" '{ n=$(NF-1); print "/dev/sd" substr("abcdefghijklmnopqrstuvwxyz", n+1,
|
||||
1) }'
|
||||
}
|
||||
|
||||
if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]; then
|
||||
##
|
||||
# lifted from usbcam code.
|
||||
#
|
||||
if [ -f /var/run/console.lock ]; then
|
||||
CONSOLEOWNER=`cat /var/run/console.lock`
|
||||
elif [ -f /var/lock/console.lock ]; then
|
||||
CONSOLEOWNER=`cat /var/lock/console.lock`
|
||||
else
|
||||
CONSOLEOWNER=
|
||||
fi
|
||||
for procEntry in $(allAttachedScsiUsb); do
|
||||
scsiDev=$(scsiDevFromScsiUsb $procEntry)
|
||||
# Some bug with usb-storage?
|
||||
# Partitions are not in /proc/partitions until they are accessed
|
||||
#+ somehow.
|
||||
/sbin/fdisk -l $scsiDev >/dev/null
|
||||
##
|
||||
# Most devices have partitioning info, so the data would be on
|
||||
#+ /dev/sd?1. However, some stupider ones don't have any partitioning
|
||||
#+ and use the entire device for data storage. This tries to
|
||||
#+ guess semi-intelligently if we have a /dev/sd?1 and if not, then
|
||||
#+ it uses the entire device and hopes for the better.
|
||||
#
|
||||
if grep -q `basename $scsiDev`1 /proc/partitions; then
|
||||
part="$scsiDev""1"
|
||||
else
|
||||
part=$scsiDev
|
||||
fi
|
||||
##
|
||||
# Change ownership of the partition to the console user so they can
|
||||
#+ mount it.
|
||||
#
|
||||
if [ ! -z "$CONSOLEOWNER" ]; then
|
||||
chown $CONSOLEOWNER:disk $part
|
||||
fi
|
||||
##
|
||||
# This checks if we already have this UUID defined with devlabel.
|
||||
# If not, it then adds the device to the list.
|
||||
#
|
||||
prodid=`$DEVLABEL printid -d $part`
|
||||
if ! grep -q $prodid $DEVLABELCONFIG; then
|
||||
# cross our fingers and hope it works
|
||||
$DEVLABEL add -d $part -s $SYMLINKDEV 2>/dev/null
|
||||
fi
|
||||
##
|
||||
# Check if the mount point exists and create if it doesn't.
|
||||
#
|
||||
if [ ! -e $MOUNTPOINT ]; then
|
||||
mkdir -p $MOUNTPOINT
|
||||
fi
|
||||
##
|
||||
# Take care of /etc/fstab so mounting is easy.
|
||||
#
|
||||
if ! grep -q "^$SYMLINKDEV" /etc/fstab; then
|
||||
# Add an fstab entry
|
||||
echo -e \
|
||||
"$SYMLINKDEV\t\t$MOUNTPOINT\t\tauto\tnoauto,owner,kudzu 0 0" \
|
||||
>> /etc/fstab
|
||||
fi
|
||||
done
|
||||
if [ ! -z "$REMOVER" ]; then
|
||||
##
|
||||
# Make sure this script is triggered on device removal.
|
||||
#
|
||||
mkdir -p `dirname $REMOVER`
|
||||
ln -s $IAM $REMOVER
|
||||
fi
|
||||
elif [ "${ACTION}" = "remove" ]; then
|
||||
##
|
||||
# If the device is mounted, unmount it cleanly.
|
||||
#
|
||||
if grep -q "$MOUNTPOINT" /etc/mtab; then
|
||||
# unmount cleanly
|
||||
umount -l $MOUNTPOINT
|
||||
fi
|
||||
##
|
||||
# Remove it from /etc/fstab if it's there.
|
||||
#
|
||||
if grep -q "^$SYMLINKDEV" /etc/fstab; then
|
||||
grep -v "^$SYMLINKDEV" /etc/fstab > /etc/.fstab.new
|
||||
mv -f /etc/.fstab.new /etc/fstab
|
||||
fi
|
||||
fi
|
|
@ -0,0 +1,44 @@
|
|||
#!/bin/bash
|
||||
# wf2.sh: Crude word frequency analysis on a text file.
|
||||
|
||||
# Uses 'xargs' to decompose lines of text into single words.
|
||||
# Compare this example to the "wf.sh" script that follows.
|
||||
|
||||
|
||||
# Check for input file on command line.
|
||||
ARGS=1
|
||||
E_BADARGS=65
|
||||
E_NOFILE=66
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
# Correct number of arguments passed to script?
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
if [ ! -f "$1" ] # Check if file exists.
|
||||
then
|
||||
echo "File \"$1\" does not exist."
|
||||
exit $E_NOFILE
|
||||
fi
|
||||
|
||||
|
||||
|
||||
#######################################################
|
||||
cat "$1" | xargs -n1 | \
|
||||
# List the file, one word per line.
|
||||
tr A-Z a-z | \
|
||||
# Shift characters to lowercase.
|
||||
sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
|
||||
/g' | \
|
||||
# Filter out periods and commas, and
|
||||
#+ change space between words to linefeed,
|
||||
sort | uniq -c | sort -nr
|
||||
# Finally prefix occurrence count and sort numerically.
|
||||
#######################################################
|
||||
|
||||
# This does the same job as the "wf.sh" example that follows,
|
||||
#+ but a bit more ponderously, and it runs more slowly.
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue