From 39f12d6ccc9c22f3926b62dcdb87759c182506e4 Mon Sep 17 00:00:00 2001 From: gferg <> Date: Mon, 15 Mar 2004 13:48:46 +0000 Subject: [PATCH] *** empty log message *** --- LDP/guide/docbook/abs-guide/exercising-dd.sh | 20 ++++++ .../docbook/abs-guide/multiple-processes.sh | 71 +++++++++++++++++++ LDP/guide/docbook/abs-guide/quote-fetch.sh | 32 +++++++++ LDP/guide/docbook/abs-guide/self-copy.sh | 11 +++ 4 files changed, 134 insertions(+) create mode 100644 LDP/guide/docbook/abs-guide/exercising-dd.sh create mode 100644 LDP/guide/docbook/abs-guide/multiple-processes.sh create mode 100644 LDP/guide/docbook/abs-guide/quote-fetch.sh create mode 100644 LDP/guide/docbook/abs-guide/self-copy.sh diff --git a/LDP/guide/docbook/abs-guide/exercising-dd.sh b/LDP/guide/docbook/abs-guide/exercising-dd.sh new file mode 100644 index 00000000..db963518 --- /dev/null +++ b/LDP/guide/docbook/abs-guide/exercising-dd.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# exercising-dd.sh + +# Script by Stephane Chazelas. +# Somewhat modified by document author. + +input_file=$0 # This script. +output_file=log.txt +n=3 +p=5 + +dd if=$input_file of=$output_file bs=1 skip=$((n-1)) count=$((p-n+1)) 2> /dev/null +# Extracts characters n to p from this script. + +# ------------------------------------------------------- + +echo -n "hello world" | dd cbs=1 conv=unblock 2> /dev/null +# Echoes "hello world" vertically. + +exit 0 diff --git a/LDP/guide/docbook/abs-guide/multiple-processes.sh b/LDP/guide/docbook/abs-guide/multiple-processes.sh new file mode 100644 index 00000000..ea73388f --- /dev/null +++ b/LDP/guide/docbook/abs-guide/multiple-processes.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# multiple-processes.sh: Run multiple processes on an SMP box. + +# Script written by Vernia Damiano. +# Used with permission. + +# Must call script with at least one integer parameter +#+ (number of concurrent processes). +# All other parameters are passed through to the processes started. + + +INDICE=8 # Total number of process to start +TEMPO=5 # Maximum sleep time per process +E_BADARGS=65 # No arg(s) passed to script. + +if [ $# -eq 0 ] # Check for at least one argument passed to script. +then + echo "Usage: `basename $0` number_of_processes [passed params]" + exit $E_BADARGS +fi + +NUMPROC=$1 # Number of concurrent process +shift +PARAMETRI=( "$@" ) # Parameters of each process + +function avvia() { + local temp + local index + temp=$RANDOM + index=$1 + shift + let "temp %= $TEMPO" + let "temp += 1" + echo "Starting $index Time:$temp" "$@" + sleep ${temp} + echo "Ending $index" + kill -s SIGRTMIN $$ +} + +function parti() { + if [ $INDICE -gt 0 ] ; then + avvia $INDICE "${PARAMETRI[@]}" & + let "INDICE--" + else + trap : SIGRTMIN + fi +} + +trap parti SIGRTMIN + +while [ "$NUMPROC" -gt 0 ]; do + parti; + let "NUMPROC--" +done + +wait +trap - SIGRTMIN + +exit $? + +: << SCRIPT_AUTHOR_COMMENTS +I had the need to run a program, with specified options, on a number of +different files, using a SMP machine. So I thought [I'd] keep running +a specified number of processes and start a new one each time . . . one +of these terminates. + +The "wait" instruction does not help, since it waits for a given process +or *all* process started in background. So I wrote [this] bash script +that can do the job, using the "trap" instruction. + --Vernia Damiano +SCRIPT_AUTHOR_COMMENTS diff --git a/LDP/guide/docbook/abs-guide/quote-fetch.sh b/LDP/guide/docbook/abs-guide/quote-fetch.sh new file mode 100644 index 00000000..1a8d98a7 --- /dev/null +++ b/LDP/guide/docbook/abs-guide/quote-fetch.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# quote-fetch.sh: Download a stock quote. + + +E_NOPARAMS=66 + +if [ -z "$1" ] # Must specify a stock (symbol) to fetch. + then echo "Usage: `basename $0` stock-symbol" + exit $E_NOPARAMS +fi + +stock_symbol=$1 + +file_suffix=.html +# Fetches an HTML file, so name it appropriately. +URL='http://finance.yahoo.com/q?s=' +# Yahoo finance board, with stock query suffix. + +# ----------------------------------------------------------- +wget -O ${stock_symbol}${file_suffix} "${URL}${stock_symbol}" +# ----------------------------------------------------------- + +exit $? + +# Exercises: +# --------- +# +# 1) Add a test to ensure the user running the script is on-line. +# (Hint: parse the output of 'ps -ax' for "ppp" or "connect." +# +# 2) Modify this script to fetch the local weather report, +#+ taking the user's zip code as an argument. diff --git a/LDP/guide/docbook/abs-guide/self-copy.sh b/LDP/guide/docbook/abs-guide/self-copy.sh new file mode 100644 index 00000000..49a8e16b --- /dev/null +++ b/LDP/guide/docbook/abs-guide/self-copy.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# self-copy.sh + +# This script copies itself. + +file_subscript=copy + +dd if=$0 of=$0.$file_subscript 2>/dev/null +# Suppress messages from dd: ^^^^^^^^^^^ + +exit $?