This commit is contained in:
gferg 2005-03-21 13:52:30 +00:00
parent 07176b0865
commit 0afd58f25a
6 changed files with 654 additions and 0 deletions

View File

@ -0,0 +1,69 @@
#!/bin/bash
# bashpodder.sh:
# By Linc 10/1/2004
# Find the latest script at http://linc.homeunix.org:8080/scripts/bashpodder
# Last revision 12/14/2004 - Many Contributors!
# If you use this and have made improvements or have comments
# drop me an email at linc dot fessenden at gmail dot com
# I'd appreciate it!
# ==> ABS Guide extra comments.
# ==> Author of this script has kindly granted permission
# ==>+ for inclusion in ABS Guide.
# ==> ################################################################
#
# ==> What is "podcasting"?
# ==> It's broadcasting "radio shows" over the Internet.
# ==> These shows can be played on iPods and other music file players.
# ==> This script makes it possible.
# ==> See documentation at the script author's site, above.
# ==> ################################################################
# Make script crontab friendly:
cd $(dirname $0)
# ==> Change to directory where this script lives.
# datadir is the directory you want podcasts saved to:
datadir=$(date +%Y-%m-%d)
# ==> Will create a directory with the name: YYYY-MM-DD
# Check for and create datadir if necessary:
if test ! -d $datadir
then
mkdir $datadir
fi
# Delete any temp file:
rm -f temp.log
# Read the bp.conf file and wget any url not already in the podcast.log file:
while read podcast
do # ==> Main action follows.
file=$(wget -q $podcast -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*url="\([^"]*\)".*/\1/p')
for url in $file
do
echo $url >> temp.log
if ! grep "$url" podcast.log > /dev/null
then
wget -q -P $datadir "$url"
fi
done
done < bp.conf
# Move dynamically created log file to permanent log file:
cat podcast.log >> temp.log
sort temp.log | uniq > podcast.log
rm temp.log
# Create an m3u playlist:
ls $datadir | grep -v m3u > $datadir/podcast.m3u
exit 0

View File

@ -0,0 +1,81 @@
#!/bin/bash
# dict-lookup.sh
# This script looks up definitions in the 1913 Webster's Dictionary.
# This Public Domain dictionary is available for download
#+ from various sites, including
#+ Project Gutenberg (http://www.gutenberg.org/etext/247).
#
# Convert it from DOS to UNIX format (only LF at end of line)
#+ before using it with this script.
# Store the file in plain, uncompressed ASCII.
# Set DEFAULT_DICTFILE variable below to filename and path.
E_BADARGS=65
MAXCONTEXTLINES=50 # Maximum number of lines to show.
DEFAULT_DICTFILE="webster1913-dict.txt" # Default dictionary file name/path.
# Change this as necessary.
# Note:
# ----
# The particular edition of the 1913 Webster's
#+ begins each entry with an uppercase letter
#+ (lowercase for the remaining characters).
# Only the very first line of an entry begins this way,
#+ and that's why the search algorithm below works.
if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]]
# Must at least specify word to look up, and
#+ it must start with an uppercase letter.
then
echo "Usage: `basename $0` Word-to-define [dictionary-file]"
echo
echo "Note: Word to look up must start with capital letter,"
echo "with the rest of the word in lowercase."
echo "--------------------------------------------"
echo "Examples: Abandon, Dictionary, Marking, etc."
exit $E_BADARGS
fi
if [ -z "$2" ] # May specify different dictionary.
then
dictfile=$DEFAULT_DICTFILE
else
dictfile="$2"
fi
# ---------------------------------------------------------
Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile")
# Definitions in form "Word \..."
#
# And, yes, "fgrep" is fast enough to search
#+ even a very large text file.
# Now, snip out just the definition block.
echo "$Definition" |
sed -n '1,/^[A-Z]/p' |
# Print from first line of output
#+ to the first line of the next entry.
sed '$d' | sed '$d'
# Delete last two lines of output
#+ (blank line and first line of next entry).
# ---------------------------------------------------------
exit 0
# Exercises:
# ---------
# 1) Modify the script to accept any type of alphabetic input
# + (uppercase, lowercase, mixed case), and convert it
# + to an acceptable format for processing.
# See lines 18-20.
#
# 2) Convert the script to a GUI application,
# + using something like "gdialog" . . .
# The script will then no longer take its argument(s)
# + from the command line.

View File

@ -0,0 +1,384 @@
#!/bin/bash
# horserace.sh: very simple horserace simulation.
# Author: Stefano Palmeri
# Used with permission.
################################################################
# Goals of the script:
# playing with escape sequences and terminal colors.
#
# Exercise:
# Edit the script to make it run less randomly,
#+ set up a fake betting shop . . .
# Um . . . um . . . it's starting to remind me of a movie . . .
#
# The script gives each horse a random handicap.
# The odds are calculated upon horse handicap
#+ and are expressed in European(?) style.
# E.g.: odds=3.75 means that if you bet $1 and win,
#+ you receive $3.75.
#
# The script has been tested with a GNU/Linux OS,
#+ using xterm and rxvt, and konsole.
# On a machine with an AMD 900 MHz processor,
#+ the average race time is 75 seconds.
# On faster computers the race time would be lower.
# So, if you want more suspense, reset the USLEEP_ARG variable.
#
# Script by Stefano Palmeri.
################################################################
E_RUNERR=65
# Check if md5sum and bc are installed.
if ! which bc &> /dev/null; then
echo bc is not installed.
echo "Can\'t run . . . "
exit $E_RUNERR
fi
if ! which md5sum &> /dev/null; then
echo md5sum is not installed.
echo "Can\'t run . . . "
exit $E_RUNERR
fi
# Set the following variable to slow down script execution.
# It will be passed as the argument for usleep (man usleep)
#+ and is expressed in microseconds (500000 = half a second).
USLEEP_ARG=0
# Clean up the temp directory, restore terminal cursor and
#+ terminal colors -- if script interrupted by Ctl-C.
trap 'echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\
tput cup 20 0; rm -fr $HORSE_RACE_TMP_DIR' TERM EXIT
# See the chapter on debugging for an explanation of 'trap.'
# Set a unique (paranoid) name for the temp directory the script needs.
HORSE_RACE_TMP_DIR=$HOME/.horserace-`date +%s`-`head -c10 /dev/urandom | md5sum | head -c30`
# Create the temp directory and move right in.
mkdir $HORSE_RACE_TMP_DIR
cd $HORSE_RACE_TMP_DIR
# This function moves the cursor to line $1 column $2 and then prints $3.
# E.g.: "move_and_echo 5 10 linux" is equivalent to
#+ "tput cup 4 9; echo linux", but with one command instead of two.
# Note: "tput cup" defines 0 0 the upper left angle of the terminal,
#+ echo defines 1 1 the upper left angle of the terminal.
move_and_echo() {
echo -ne "\E[${1};${2}H""$3"
}
# Function to generate a pseudo-random number between 1 and 9.
random_1_9 () {
head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1
}
# Two functions that simulate "movement," when drawing the horses.
draw_horse_one() {
echo -n " "//$MOVE_HORSE//
}
draw_horse_two(){
echo -n " "\\\\$MOVE_HORSE\\\\
}
# Define current terminal dimension.
N_COLS=`tput cols`
N_LINES=`tput lines`
# Need at least a 20-LINES X 80-COLUMNS terminal. Check it.
if [ $N_COLS -lt 80 ] || [ $N_LINES -lt 20 ]; then
echo "`basename $0` needs a 80-cols X 20-lines terminal."
echo "Your terminal is ${N_COLS}-cols X ${N_LINES}-lines."
exit $E_RUNERR
fi
# Start drawing the race field.
# Need a string of 80 chars. See below.
BLANK80=`seq -s "" 100 | head -c80`
clear
# Set foreground and background colors to white.
echo -ne '\E[37;47m'
# Move the cursor on the upper left angle of the terminal.
tput cup 0 0
# Draw six white lines.
for n in `seq 5`; do
echo $BLANK80 # Use the 80 chars string to colorize the terminal.
done
# Sets foreground color to black.
echo -ne '\E[30m'
move_and_echo 3 1 "START 1"
move_and_echo 3 75 FINISH
move_and_echo 1 5 "|"
move_and_echo 1 80 "|"
move_and_echo 2 5 "|"
move_and_echo 2 80 "|"
move_and_echo 4 5 "| 2"
move_and_echo 4 80 "|"
move_and_echo 5 5 "V 3"
move_and_echo 5 80 "V"
# Set foreground color to red.
echo -ne '\E[31m'
# Some ASCII art.
move_and_echo 1 8 "..@@@..@@@@@...@@@@@.@...@..@@@@..."
move_and_echo 2 8 ".@...@...@.......@...@...@.@......."
move_and_echo 3 8 ".@@@@@...@.......@...@@@@@.@@@@...."
move_and_echo 4 8 ".@...@...@.......@...@...@.@......."
move_and_echo 5 8 ".@...@...@.......@...@...@..@@@@..."
move_and_echo 1 43 "@@@@...@@@...@@@@..@@@@..@@@@."
move_and_echo 2 43 "@...@.@...@.@.....@.....@....."
move_and_echo 3 43 "@@@@..@@@@@.@.....@@@@...@@@.."
move_and_echo 4 43 "@..@..@...@.@.....@.........@."
move_and_echo 5 43 "@...@.@...@..@@@@..@@@@.@@@@.."
# Set foreground and background colors to green.
echo -ne '\E[32;42m'
# Draw eleven green lines.
tput cup 5 0
for n in `seq 11`; do
echo $BLANK80
done
# Set foreground color to black.
echo -ne '\E[30m'
tput cup 5 0
# Draw the fences.
echo "++++++++++++++++++++++++++++++++++++++\
++++++++++++++++++++++++++++++++++++++++++"
tput cup 15 0
echo "++++++++++++++++++++++++++++++++++++++\
++++++++++++++++++++++++++++++++++++++++++"
# Set foreground and background colors to white.
echo -ne '\E[37;47m'
# Draw three white lines.
for n in `seq 3`; do
echo $BLANK80
done
# Set foreground color to black.
echo -ne '\E[30m'
# Create 9 files to stores handicaps.
for n in `seq 10 7 68`; do
touch $n
done
# Set the first type of "horse" the script will draw.
HORSE_TYPE=2
# Create position-file and odds-file for every "horse".
#+ In these files, store the current position of the horse,
#+ the type and the odds.
for HN in `seq 9`; do
touch horse_${HN}_position
touch odds_${HN}
echo \-1 > horse_${HN}_position
echo $HORSE_TYPE >> horse_${HN}_position
# Define a random handicap for horse.
HANDICAP=`random_1_9`
# Check if the random_1_9 function returned a good value.
while ! echo $HANDICAP | grep [1-9] &> /dev/null; do
HANDICAP=`random_1_9`
done
# Define last handicap position for horse.
LHP=`expr $HANDICAP \* 7 + 3`
for FILE in `seq 10 7 $LHP`; do
echo $HN >> $FILE
done
# Calculate odds.
case $HANDICAP in
1) ODDS=`echo $HANDICAP \* 0.25 + 1.25 | bc`
echo $ODDS > odds_${HN}
;;
2 | 3) ODDS=`echo $HANDICAP \* 0.40 + 1.25 | bc`
echo $ODDS > odds_${HN}
;;
4 | 5 | 6) ODDS=`echo $HANDICAP \* 0.55 + 1.25 | bc`
echo $ODDS > odds_${HN}
;;
7 | 8) ODDS=`echo $HANDICAP \* 0.75 + 1.25 | bc`
echo $ODDS > odds_${HN}
;;
9) ODDS=`echo $HANDICAP \* 0.90 + 1.25 | bc`
echo $ODDS > odds_${HN}
esac
done
# Print odds.
print_odds() {
tput cup 6 0
echo -ne '\E[30;42m'
for HN in `seq 9`; do
echo "#$HN odds->" `cat odds_${HN}`
done
}
# Draw the horses at starting line.
draw_horses() {
tput cup 6 0
echo -ne '\E[30;42m'
for HN in `seq 9`; do
echo /\\$HN/\\" "
done
}
print_odds
echo -ne '\E[47m'
# Wait for a enter key press to start the race.
# The escape sequence '\E[?25l' disables the cursor.
tput cup 17 0
echo -e '\E[?25l'Press [enter] key to start the race...
read -s
# Disable normal echoing in the terminal.
# This avoids key presses that might "contaminate" the screen
#+ during the race.
stty -echo
# --------------------------------------------------------
# Start the race.
draw_horses
echo -ne '\E[37;47m'
move_and_echo 18 1 $BLANK80
echo -ne '\E[30m'
move_and_echo 18 1 Starting...
sleep 1
# Set the column of the finish line.
WINNING_POS=74
# Define the time the race started.
START_TIME=`date +%s`
# COL variable needed by following "while" construct.
COL=0
while [ $COL -lt $WINNING_POS ]; do
MOVE_HORSE=0
# Check if the random_1_9 function has returned a good value.
while ! echo $MOVE_HORSE | grep [1-9] &> /dev/null; do
MOVE_HORSE=`random_1_9`
done
# Define old type and position of the "randomized horse".
HORSE_TYPE=`cat horse_${MOVE_HORSE}_position | tail -1`
COL=$(expr `cat horse_${MOVE_HORSE}_position | head -1`)
ADD_POS=1
# Check if the current position is an handicap position.
if seq 10 7 68 | grep -w $COL &> /dev/null; then
if grep -w $MOVE_HORSE $COL &> /dev/null; then
ADD_POS=0
grep -v -w $MOVE_HORSE $COL > ${COL}_new
rm -f $COL
mv -f ${COL}_new $COL
else ADD_POS=1
fi
else ADD_POS=1
fi
COL=`expr $COL + $ADD_POS`
echo $COL > horse_${MOVE_HORSE}_position # Store new position.
# Choose the type of horse to draw.
case $HORSE_TYPE in
1) HORSE_TYPE=2; DRAW_HORSE=draw_horse_two
;;
2) HORSE_TYPE=1; DRAW_HORSE=draw_horse_one
esac
echo $HORSE_TYPE >> horse_${MOVE_HORSE}_position # Store current type.
# Set foreground color to black and background to green.
echo -ne '\E[30;42m'
# Move the cursor to new horse position.
tput cup `expr $MOVE_HORSE + 5` `cat horse_${MOVE_HORSE}_position | head -1`
# Draw the horse.
$DRAW_HORSE
usleep $USLEEP_ARG
# When all horses have gone beyond field line 15, reprint odds.
touch fieldline15
if [ $COL = 15 ]; then
echo $MOVE_HORSE >> fieldline15
fi
if [ `wc -l fieldline15 | cut -f1 -d " "` = 9 ]; then
print_odds
: > fieldline15
fi
# Define the leading horse.
HIGHEST_POS=`cat *position | sort -n | tail -1`
# Set background color to white.
echo -ne '\E[47m'
tput cup 17 0
echo -n Current leader: `grep -w $HIGHEST_POS *position | cut -c7`" "
done
# Define the time the race finished.
FINISH_TIME=`date +%s`
# Set background color to green and enable blinking text.
echo -ne '\E[30;42m'
echo -en '\E[5m'
# Make the winning horse blink.
tput cup `expr $MOVE_HORSE + 5` `cat horse_${MOVE_HORSE}_position | head -1`
$DRAW_HORSE
# Disable blinking text.
echo -en '\E[25m'
# Set foreground and background color to white.
echo -ne '\E[37;47m'
move_and_echo 18 1 $BLANK80
# Set foreground color to black.
echo -ne '\E[30m'
# Make winner blink.
tput cup 17 0
echo -e "\E[5mWINNER: $MOVE_HORSE\E[25m"" Odds: `cat odds_${MOVE_HORSE}`"\
" Race time: `expr $FINISH_TIME - $START_TIME` secs"
# Restore cursor and old colors.
echo -en "\E[?25h"
echo -en "\E[0m"
# Restore echoing.
stty echo
# Remove race temp directory.
rm -rf $HORSE_RACE_TMP_DIR
tput cup 19 0
exit 0

View File

@ -0,0 +1,67 @@
#! /bin/sh
# letter-count2.sh: Counting letter occurrences in a text file.
#
# Script by nyal [nyal@voila.fr].
# Used with permission.
# Recommented by document author.
# Version 1.1: Modified to work with gawk 3.1.3.
# (Will still work with earlier versions.)
INIT_TAB_AWK=""
# Parameter to initialize awk script.
count_case=0
FILE_PARSE=$1
E_PARAMERR=65
usage()
{
echo "Usage: letter-count.sh file letters" 2>&1
# For example: ./letter-count2.sh filename.txt a b c
exit $E_PARAMERR # Not enough arguments passed to script.
}
if [ ! -f "$1" ] ; then
echo "$1: No such file." 2>&1
usage # Print usage message and exit.
fi
if [ -z "$2" ] ; then
echo "$2: No letters specified." 2>&1
usage
fi
shift # Letters specified.
for letter in `echo $@` # For each one . . .
do
INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \"$letter\"; final_tab[${count_case}] = 0; "
# Pass as parameter to awk script below.
count_case=`expr $count_case + 1`
done
# DEBUG:
# echo $INIT_TAB_AWK;
cat $FILE_PARSE |
# Pipe the target file to the following awk script.
# ----------------------------------------------------------------------------------
# Earlier version of script used:
# awk -v tab_search=0 -v final_tab=0 -v tab=0 -v nb_letter=0 -v chara=0 -v chara2=0 \
awk \
"BEGIN { $INIT_TAB_AWK } \
{ split(\$0, tab, \"\"); \
for (chara in tab) \
{ for (chara2 in tab_search) \
{ if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \
END { for (chara in final_tab) \
{ print tab_search[chara] \" => \" final_tab[chara] } }"
# ----------------------------------------------------------------------------------
# Nothing all that complicated, just . . .
#+ for-loops, if-tests, and a couple of specialized functions.
exit $?
# Compare this script to letter-count.sh.

View File

@ -0,0 +1,22 @@
#!/bin/bash
# Generic shell wrapper that performs an operation
#+ and logs it.
# Must set the following two variables.
OPERATION=
# Can be a complex chain of commands,
#+ for example an awk script or a pipe . . .
LOGFILE=
# Command-line arguments, if any, for the operation.
OPTIONS="$@"
# Log it.
echo "`date` + `whoami` + $OPERATION "$@"" >> $LOGFILE
# Now, do it.
exec $OPERATION "$@"
# It's necessary to do the logging before the operation.
# Why?

View File

@ -0,0 +1,31 @@
#!/bin/bash
# spawn.sh
PIDS=$(pidof sh $0) # Process IDs of the various instances of this script.
P_array=( $PIDS ) # Put them in an array (why?).
echo $PIDS # Show process IDs of parent and child processes.
let "instances = ${#P_array[*]} - 1" # Count elements, less 1.
# Why subtract 1?
echo "$instances instance(s) of this script running."
echo "[Hit Ctl-C to exit.]"; echo
sleep 1 # Wait.
sh $0 # Play it again, Sam.
exit 0 # Not necessary; script will never get to here.
# Why not?
# After exiting with a Ctl-C,
#+ do all the spawned instances of the script die?
# If so, why?
# Note:
# ----
# Be careful not to run this script too long.
# It will eventually eat up too many system resources.
# Is having a script spawn multiple instances of itself
#+ an advisable scripting technique.
# Why or why not?