mirror of https://github.com/tLDP/LDP
new
This commit is contained in:
parent
99d1c5de72
commit
e32f343590
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,38 @@
|
|||
/*********************************************/
|
||||
/* ascii.c */
|
||||
/* Generate ASCII table */
|
||||
/* To build: gcc -O2 ascii.c -o ascii-table */
|
||||
/* */
|
||||
/* This utterly trivial program written by */
|
||||
/* Mendel Cooper, 04/07 */
|
||||
/* I'm not proud of it, but it does the job. */
|
||||
/* License: Public Domain */
|
||||
/*********************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX 255 /* FF hex */
|
||||
#define FILENAME "ASCII.txt" /* Outfile name */
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen (FILENAME, "a" );
|
||||
|
||||
for( i = 1; i <= MAX; i++ ) {
|
||||
fprintf( fp, "%5d ", i );
|
||||
fputc( i, fp );
|
||||
fprintf( fp, " " );
|
||||
if ( i % 5 == 0 )
|
||||
fprintf( fp, "\n" );
|
||||
}
|
||||
|
||||
fprintf( fp, "\n" );
|
||||
|
||||
return (0);
|
||||
} /* Outfile needs a bit of hand-editing for tidying up. */
|
||||
|
||||
/* Try rewriting this as a shell script. */
|
||||
/* Not so easy, huh? */
|
|
@ -0,0 +1,115 @@
|
|||
#!/bin/bash
|
||||
# brownian.sh
|
||||
# Author: Mendel Cooper
|
||||
# Reldate: 10/26/07
|
||||
# License: GPL3
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# This script models Brownian motion:
|
||||
#+ the random wanderings of tiny particles in a fluid,
|
||||
#+ as they are impacted by random currents and collisions.
|
||||
#+ This is also known as the "Drunkard's Walk."
|
||||
|
||||
# It can also be considered as a highly simplified simulation of a
|
||||
#+ Galton Board, a slanted board with a pattern of pegs,
|
||||
#+ down which rolls a succession of marbles, one at a time.
|
||||
#+ At the bottom is a row of slots or catch basins in which
|
||||
#+ the marbles finally come to rest.
|
||||
# Think of it as a kind of bare-bones Pachinko game.
|
||||
# As you see by running the script,
|
||||
#+ most of the marbles cluster around the center slot.
|
||||
#+ This mirrors the expected "Normal Distribution."
|
||||
# As a Galton Board simulation, this script
|
||||
#+ disregards such parameters as
|
||||
#+ board tilt-angle, rolling friction of the marbles,
|
||||
#+ angles of impact, and elasticity of the pegs.
|
||||
# How much of a difference does that make?
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
PASSES=500 # Number of particle interactions / marbles.
|
||||
ROWS=10 # Number of "collisions" (or horizontal peg rows).
|
||||
RANGE=3 # We want 0 - 2 output range from $RANDOM.
|
||||
POS=0 # Left/right position.
|
||||
|
||||
declare -a Slots # Array holding cumulative results of passes.
|
||||
NUMSLOTS=21 # How many slots at bottom of board?
|
||||
|
||||
|
||||
Initialize_Slots () { # Zero out all elements of array.
|
||||
for i in $( seq $NUMSLOTS )
|
||||
do
|
||||
Slots[$i]=0
|
||||
done
|
||||
|
||||
echo # Blank line at beginning of run.
|
||||
}
|
||||
|
||||
|
||||
Show_Slots () {
|
||||
echo -n " "
|
||||
for i in $( seq $NUMSLOTS ) # Pretty-print array elements.
|
||||
do
|
||||
printf "%3d" ${Slots[$i]} # Three spaces per result.
|
||||
done
|
||||
|
||||
echo # Row of slots:
|
||||
echo " |__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|__|"
|
||||
echo " ^^"
|
||||
echo # Note that if the count within any particular slot exceeds 99,
|
||||
#+ it messes up the display.
|
||||
# Running only(!) 500 passes usually avoids this.
|
||||
}
|
||||
|
||||
|
||||
Move () { # Move one unit right / left, or stay put.
|
||||
Move=$RANDOM # How random is $RANDOM? Let's see. ...
|
||||
let "Move %= RANGE" # Normalize into range of 0 - 2.
|
||||
case "$Move" in
|
||||
0 ) ;; # Do nothing, i.e., stay in place.
|
||||
1 ) ((POS--));; # Left.
|
||||
2 ) ((POS++));; # Right.
|
||||
* ) echo -n "Error ";; # Should never happen.
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
Play () { # Single pass (inner loop).
|
||||
i=0
|
||||
while [ $i -lt $ROWS ] # One event per row.
|
||||
do
|
||||
Move
|
||||
((i++));
|
||||
done
|
||||
|
||||
SHIFT=11 # Why 11, and not 10?
|
||||
let "POS += $SHIFT" # Shift "zero position" to center.
|
||||
(( Slots[$POS]++ )) # DEBUG: echo $POS
|
||||
}
|
||||
|
||||
|
||||
Run () { # Outer loop.
|
||||
p=0
|
||||
while [ $p -lt $PASSES ]
|
||||
do
|
||||
Play
|
||||
(( p++ ))
|
||||
POS=0 # Reset to zero. Why?
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
# --------------
|
||||
# main ()
|
||||
Initialize_Slots
|
||||
Run
|
||||
Show_Slots
|
||||
# --------------
|
||||
|
||||
exit $?
|
||||
|
||||
# Exercises:
|
||||
# ---------
|
||||
# 1) Show the results in a vertical bar graph, or as an alternative,
|
||||
#+ a scattergram.
|
||||
# 2) Alter the script to use /dev/urandom instead of $RANDOM.
|
||||
# Does this make the results more random?
|
|
@ -0,0 +1,36 @@
|
|||
#!/bin/bash
|
||||
# Optimized Sieve of Eratosthenes
|
||||
# Script by Jared Martin, with very minor changes by ABS Guide author.
|
||||
# Used in ABS Guide with permission (thanks!).
|
||||
|
||||
# Based on script in Advanced Bash Scripting Guide.
|
||||
# http://tldp.org/LDP/abs/html/arrays.html#PRIMES0 (ex68.sh).
|
||||
|
||||
# http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf (reference)
|
||||
# Check results against http://primes.utm.edu/lists/small/1000.txt
|
||||
|
||||
# Necessary but not sufficient would be, e.g.,
|
||||
# (($(sieve 7919 | wc -w) == 1000)) && echo "7919 is the 1000th prime"
|
||||
|
||||
UPPER_LIMIT=${1:?"Need an upper limit of primes to search."}
|
||||
|
||||
Primes=( '' $(seq ${UPPER_LIMIT}) )
|
||||
|
||||
typeset -i i t
|
||||
Primes[i=1]='' # 1 is not a prime.
|
||||
until (( ( i += 1 ) > (${UPPER_LIMIT}/i) )) # Need check only ith-way.
|
||||
do # Why?
|
||||
if ((${Primes[t=i*(i-1), i]}))
|
||||
# Obscure, but instructive, use of numeric eval in subscript.
|
||||
then
|
||||
until (( ( t += i ) > ${UPPER_LIMIT} ))
|
||||
do Primes[t]=; done
|
||||
fi
|
||||
done
|
||||
|
||||
# echo ${Primes[*]}
|
||||
echo # Change to original script for pretty-printing (80-col. display).
|
||||
printf "%8d" ${Primes[*]}
|
||||
echo; echo
|
||||
|
||||
exit $?
|
|
@ -0,0 +1,24 @@
|
|||
#!/bin/bash
|
||||
# from.sh
|
||||
|
||||
# Emulates the useful "from" utility in Solaris, BSD, etc.
|
||||
# Echoes the "From" header line in all messages
|
||||
#+ in your e-mail directory.
|
||||
|
||||
|
||||
MAILDIR=~/mail/* # No quoting of variable. Why?
|
||||
GREP_OPTS="-H -A 5 --color" # Show file, plus extra context lines
|
||||
#+ and display "From" in color.
|
||||
TARGETSTR="^From" # "From" at beginning of line.
|
||||
|
||||
for file in $MAILDIR # No quoting of variable.
|
||||
do
|
||||
grep $GREP_OPTS "$TARGETSTR" "$file"
|
||||
# ^^^^^^^^^^ # Again, do not quote this variable.
|
||||
echo
|
||||
done
|
||||
|
||||
exit $?
|
||||
|
||||
# Might wish to pipe the output of this script to 'more' or
|
||||
#+ redirect it to a file . . .
|
|
@ -0,0 +1,65 @@
|
|||
#!/bin/bash
|
||||
# insertion-sort.bash: Insertion sort implementation in Bash
|
||||
# Heavy use of Bash array features:
|
||||
#+ (string) slicing, merging, etc
|
||||
# URL: http://www.lugmen.org.ar/~jjo/jjotip/insertion-sort.bash.d
|
||||
#+ /insertion-sort.bash.sh
|
||||
#
|
||||
# Author: JuanJo Ciarlante <jjo@irrigacion.gov.ar>
|
||||
# Lightly reformatted by ABS Guide author.
|
||||
# License: GPLv2
|
||||
# Used in ABS Guide with author's permission (thanks!).
|
||||
#
|
||||
# Test with: ./insertion-sort.bash -t
|
||||
# Or: bash insertion-sort.bash -t
|
||||
# The following *doesn't* work:
|
||||
# sh insertion-sort.bash -t
|
||||
# Why not? Hint: which Bash-specific features are disabled
|
||||
#+ when running a script by 'sh script.sh'?
|
||||
#
|
||||
: ${DEBUG:=0} # Debug, override with: DEBUG=1 ./scriptname . . .
|
||||
# Parameter substitution -- set DEBUG to 0 if not previously set.
|
||||
|
||||
# Global array: "list"
|
||||
typeset -a list
|
||||
# Load whitespace-separated numbers from stdin.
|
||||
if [ "$1" = "-t" ]; then
|
||||
DEBUG=1
|
||||
read -a list < <( od -Ad -w24 -t u2 /dev/urandom ) # Random list.
|
||||
# ^ ^ process substition
|
||||
else
|
||||
read -a list
|
||||
fi
|
||||
numelem=${#list[*]}
|
||||
|
||||
# Shows the list, marking the element whose index is $1
|
||||
#+ by surrounding it with the two chars passed as $2.
|
||||
# Whole line prefixed with $3.
|
||||
showlist()
|
||||
{
|
||||
echo "$3"${list[@]:0:$1} ${2:0:1}${list[$1]}${2:1:1} ${list[@]:$1+1};
|
||||
}
|
||||
|
||||
# Loop _pivot_ -- from second element to end of list.
|
||||
for(( i=1; i<numelem; i++ )) do
|
||||
((DEBUG))&&showlist i "[]" " "
|
||||
# From current _pivot_, back to first element.
|
||||
for(( j=i; j; j-- )) do
|
||||
# Search for the 1st elem. less than current "pivot" . . .
|
||||
[[ "${list[j-1]}" -le "${list[i]}" ]] && break
|
||||
done
|
||||
(( i==j )) && continue ## No insertion was needed for this element.
|
||||
# . . . Move list[i] (pivot) to the left of list[j]:
|
||||
list=(${list[@]:0:j} ${list[i]} ${list[j]}\
|
||||
# {0,j-1} {i} {j}
|
||||
${list[@]:j+1:i-(j+1)} ${list[@]:i+1})
|
||||
# {j+1,i-1} {i+1,last}
|
||||
((DEBUG))&&showlist j "<>" "*"
|
||||
done
|
||||
|
||||
|
||||
echo
|
||||
echo "------"
|
||||
echo $'Result:\n'${list[@]}
|
||||
|
||||
exit $?
|
|
@ -0,0 +1,177 @@
|
|||
<table>
|
||||
<title>Operator Precedence</title>
|
||||
<tgroup cols="3">
|
||||
<thead>
|
||||
<row>
|
||||
<entry>Operator</entry>
|
||||
<entry>Meaning</entry>
|
||||
<entry>Comments</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry><option></option></entry>
|
||||
<entry></entry>
|
||||
<entry><command>HIGHEST PRECEDENCE</command></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>var++ var--</option></entry>
|
||||
<entry>post-increment, post-decrement</entry>
|
||||
<entry><link linkend="cstyle">C-style</link> operators</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>++var --var</option></entry>
|
||||
<entry>pre-increment, pre-decrement</entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>! ~</option></entry>
|
||||
<entry><link linkend="notref">negation</link></entry>
|
||||
<entry>logical / bitwise, inverts sense of following
|
||||
operator</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>**</option></entry>
|
||||
<entry><link
|
||||
linkend="exponentiationref">exponentiation</link></entry>
|
||||
<entry><link linkend="arops1">arithmetic
|
||||
operation</link></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>* / %</option></entry>
|
||||
<entry>multiplication, division, modulo</entry>
|
||||
<entry>arithmetic operation</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>+ -</option></entry>
|
||||
<entry>addition, subtraction</entry>
|
||||
<entry>arithmetic operation</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option><< >></option></entry>
|
||||
<entry>left, right shift</entry>
|
||||
<entry><link linkend="bitwsops1">bitwise</link></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>-z -n</option></entry>
|
||||
<entry><firstterm>unary</firstterm> comparison</entry>
|
||||
<entry>string is/is-not <firstterm>null</firstterm></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>-e -f -t -x, etc.</option></entry>
|
||||
<entry><firstterm>unary</firstterm> comparison</entry>
|
||||
<entry><firstterm>files</firstterm></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>< -lt > -gt <= -le >= -ge</option></entry>
|
||||
<entry><firstterm>compound</firstterm> comparison</entry>
|
||||
<entry>string and integer</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>-nt -ot -ef</option></entry>
|
||||
<entry><firstterm>compound</firstterm> comparison</entry>
|
||||
<entry><firstterm>files</firstterm></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>== -eq != -ne</option></entry>
|
||||
<entry>equality / inequality</entry>
|
||||
<entry>test operators, string and integer</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>&</option></entry>
|
||||
<entry>AND</entry>
|
||||
<entry>bitwise</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>^</option></entry>
|
||||
<entry>XOR</entry>
|
||||
<entry><firstterm>exclusive</firstterm> OR, bitwise</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>|</option></entry>
|
||||
<entry>OR</entry>
|
||||
<entry>bitwise</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>&& -a</option></entry>
|
||||
<entry>AND</entry>
|
||||
<entry>logical, <firstterm>compound</firstterm>
|
||||
comparison</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>|| -o</option></entry>
|
||||
<entry>OR</entry>
|
||||
<entry>logical, <firstterm>compound</firstterm>
|
||||
comparison</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>?:</option></entry>
|
||||
<entry><link linkend="cstrinary">trinary
|
||||
operator</link></entry>
|
||||
<entry>C-style</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>=</option></entry>
|
||||
<entry><link linkend="eqref">assignment</link></entry>
|
||||
<entry>(do not confuse with equality
|
||||
<firstterm>test</firstterm>)</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>*= /= %= += -= <<= >>= &= !=</option></entry>
|
||||
<entry><link linkend="arithopscomb">combination
|
||||
assignment</link></entry>
|
||||
<entry>times-equal, divide-equal, mod-equal, etc.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
<entry></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option>,</option></entry>
|
||||
<entry><link linkend="commaop">comma</link></entry>
|
||||
<entry>links a sequence of operations</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><option></option></entry>
|
||||
<entry></entry>
|
||||
<entry><command>LOWEST PRECEDENCE</command></entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</table>
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/bash
|
||||
# rand-string.sh
|
||||
# Generating an 8-character "random" string.
|
||||
|
||||
if [ "-n $1" ] # If command line argument present,
|
||||
then #+ then set start-string to it.
|
||||
str0="$1"
|
||||
else # Else use PID of script as start-string.
|
||||
str0="$$"
|
||||
fi
|
||||
|
||||
POS=2 # Starting from position 2 in the string.
|
||||
LEN=8 # Extract eight characters.
|
||||
|
||||
str1=$( echo "$str0" | md5sum | md5sum )
|
||||
# Doubly scramble: ^^^^^^ ^^^^^^
|
||||
|
||||
randstring="${str1:$POS:$LEN}"
|
||||
# Can parameterize ^^^^ ^^^^
|
||||
|
||||
echo "$randstring"
|
||||
|
||||
exit $?
|
||||
|
||||
# bozo$ ./rand-string.sh my-password
|
||||
# 1bdd88c4
|
||||
|
||||
# No, this is is not recommended
|
||||
#+ as a method of generating hack-proof passwords.
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/bash
|
||||
# splitcopy.sh
|
||||
|
||||
# A script that splits itself into chunks,
|
||||
#+ then reassembles the chunks into an exact copy
|
||||
#+ of the original script.
|
||||
|
||||
CHUNKSIZE=4 # Size of first chunk of split files.
|
||||
OUTPREFIX=xx # csplit prefixes, by default,
|
||||
#+ files with "xx" ...
|
||||
|
||||
csplit "$0" "$CHUNKSIZE"
|
||||
|
||||
# Some comment lines for padding . . .
|
||||
# Line 15
|
||||
# Line 16
|
||||
# Line 17
|
||||
# Line 18
|
||||
# Line 19
|
||||
# Line 20
|
||||
|
||||
cat "$OUTPREFIX"* > "$0.copy" # Concatenate the chunks.
|
||||
rm "$OUTPREFIX"* # Get rid of the chunks.
|
||||
|
||||
exit $?
|
Loading…
Reference in New Issue