mirror of https://github.com/tLDP/LDP
add missing
This commit is contained in:
parent
6b361af168
commit
e079b4e9ba
|
@ -0,0 +1,85 @@
|
|||
#!/bin/bash
|
||||
# blotout.sh: Erase all traces of a file.
|
||||
|
||||
# This script overwrites a target file alternately
|
||||
#+ with random bytes, then zeros before finally deleting it.
|
||||
# After that, even examining the raw disk sectors
|
||||
#+ will not reveal the original file data.
|
||||
|
||||
PASSES=7 # Number of file-shredding passes.
|
||||
BLOCKSIZE=1 # I/O with /dev/urandom requires unit block size,
|
||||
#+ otherwise you get weird results.
|
||||
E_BADARGS=70
|
||||
E_NOT_FOUND=71
|
||||
E_CHANGED_MIND=72
|
||||
|
||||
if [ -z "$1" ] # No filename specified.
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
file=$1
|
||||
|
||||
if [ ! -e "$file" ]
|
||||
then
|
||||
echo "File \"$file\" not found."
|
||||
exit $E_NOT_FOUND
|
||||
fi
|
||||
|
||||
echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "
|
||||
read answer
|
||||
case "$answer" in
|
||||
[nN]) echo "Changed your mind, huh?"
|
||||
exit $E_CHANGED_MIND
|
||||
;;
|
||||
*) echo "Blotting out file \"$file\".";;
|
||||
esac
|
||||
|
||||
|
||||
flength=$(ls -l "$file" | awk '{print $5}') # Field 5 is file length.
|
||||
|
||||
pass_count=1
|
||||
|
||||
echo
|
||||
|
||||
while [ "$pass_count" -le "$PASSES" ]
|
||||
do
|
||||
echo "Pass #$pass_count"
|
||||
sync # Flush buffers.
|
||||
dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength
|
||||
# Fill with random bytes.
|
||||
sync # Flush buffers again.
|
||||
dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength
|
||||
# Fill with zeros.
|
||||
sync # Flush buffers yet again.
|
||||
let "pass_count += 1"
|
||||
echo
|
||||
done
|
||||
|
||||
|
||||
rm -f $file # Finally, delete scrambled and shredded file.
|
||||
sync # Flush buffers a final time.
|
||||
|
||||
echo "File \"$file\" blotted out and deleted."; echo
|
||||
|
||||
|
||||
# This is a fairly secure, if inefficient and slow method
|
||||
#+ of thoroughly "shredding" a file.
|
||||
|
||||
# The file cannot not be "undeleted" or retrieved by normal methods.
|
||||
# However...
|
||||
#+ this simple method will likely *not* withstand forensic analysis.
|
||||
|
||||
|
||||
# Tom Vier's "wipe" file-deletion package does a much more thorough job
|
||||
#+ of file shredding than this simple script.
|
||||
# http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2
|
||||
|
||||
# For an in-depth analysis on the topic of file deletion and security,
|
||||
#+ see Peter Gutmann's paper,
|
||||
#+ "Secure Deletion of Data From Magnetic and Solid-State Memory".
|
||||
# http://www.cs.auckland.ac.nz/~pgut001/secure_del.html
|
||||
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,38 @@
|
|||
#!/bin/bash
|
||||
# crypto-quote.sh: Encrypt quotes
|
||||
|
||||
# Will encrypt famous quotes in a simple monoalphabetic substitution.
|
||||
# The result is similar to the "Crypto Quote" puzzles
|
||||
# seen in the Op Ed pages of the Sunday paper.
|
||||
|
||||
|
||||
key=ETAOINSHRDLUBCFGJMQPVWZYXK
|
||||
# The "key" is nothing more than a scrambled alphabet.
|
||||
# Changing the "key" changes the encryption.
|
||||
|
||||
# The 'cat "$@"' construction gets input either from stdin or from files.
|
||||
# If using stdin, terminate input with a Control-D.
|
||||
# Otherwise, specify filename as command-line parameter.
|
||||
|
||||
cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
|
||||
# | to uppercase | encrypt
|
||||
# Will work on lowercase, uppercase, or mixed-case quotes.
|
||||
# Passes non-alphabetic characters through unchanged.
|
||||
|
||||
|
||||
# Try this script with something like
|
||||
# "Nothing so needs reforming as other people's habits."
|
||||
# --Mark Twain
|
||||
#
|
||||
# Output is:
|
||||
# "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ."
|
||||
# --BEML PZERC
|
||||
|
||||
# To reverse the encryption:
|
||||
# cat "$@" | tr "$key" "A-Z"
|
||||
|
||||
|
||||
# This simple-minded cipher can be broken by an average 12-year old
|
||||
# using only pencil and paper.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,153 @@
|
|||
#!/bin/bash
|
||||
# days-between.sh: Number of days between two dates.
|
||||
# Usage: ./days-between.sh [M]M/[D]D/YYYY [M]M/[D]D/YYYY
|
||||
|
||||
ARGS=2 # Two command line parameters expected.
|
||||
E_PARAM_ERR=65 # Param error.
|
||||
|
||||
REFYR=1600 # Reference year.
|
||||
CENTURY=100
|
||||
DIY=365
|
||||
ADJ_DIY=367 # Adjusted for leap year + fraction.
|
||||
MIY=12
|
||||
DIM=31
|
||||
LEAPCYCLE=4
|
||||
|
||||
MAXRETVAL=256 # Largest permissable
|
||||
# positive return value from a function.
|
||||
|
||||
diff= # Declare global variable for date difference.
|
||||
value= # Declare global variable for absolute value.
|
||||
day= # Declare globals for day, month, year.
|
||||
month=
|
||||
year=
|
||||
|
||||
|
||||
Param_Error () # Command line parameters wrong.
|
||||
{
|
||||
echo "Usage: `basename $0` [M]M/[D]D/YYYY [M]M/[D]D/YYYY"
|
||||
echo " (date must be after 1/3/1600)"
|
||||
exit $E_PARAM_ERR
|
||||
}
|
||||
|
||||
|
||||
Parse_Date () # Parse date from command line params.
|
||||
{
|
||||
month=${1%%/**}
|
||||
dm=${1%/**} # Day and month.
|
||||
day=${dm#*/}
|
||||
let "year = `basename $1`" # Not a filename, but works just the same.
|
||||
}
|
||||
|
||||
|
||||
check_date () # Checks for invalid date(s) passed.
|
||||
{
|
||||
[ "$day" -gt "$DIM" ] || [ "$month" -gt "$MIY" ] || [ "$year" -lt "$REFYR" ] && Param_Error
|
||||
# Exit script on bad value(s).
|
||||
# Uses "or-list / and-list".
|
||||
# Exercise for the reader: Implement more rigorous date checking.
|
||||
}
|
||||
|
||||
|
||||
strip_leading_zero () # Better to strip possible leading zero(s)
|
||||
{ # from day and/or month
|
||||
val=${1#0} # since otherwise Bash will interpret them
|
||||
return $val # as octal values (POSIX.2, sect 2.9.2.1).
|
||||
}
|
||||
|
||||
|
||||
day_index () # Gauss' Formula:
|
||||
{ # Days from Jan. 3, 1600 to date passed as param.
|
||||
|
||||
day=$1
|
||||
month=$2
|
||||
year=$3
|
||||
|
||||
let "month = $month - 2"
|
||||
if [ "$month" -le 0 ]
|
||||
then
|
||||
let "month += 12"
|
||||
let "year -= 1"
|
||||
fi
|
||||
|
||||
let "year -= $REFYR"
|
||||
let "indexyr = $year / $CENTURY"
|
||||
|
||||
|
||||
let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr + $indexyr/$LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"
|
||||
# For an in-depth explanation of this algorithm, see
|
||||
# http://home.t-online.de/home/berndt.schwerdtfeger/cal.htm
|
||||
|
||||
|
||||
if [ "$Days" -gt "$MAXRETVAL" ] # If greater than 256,
|
||||
then # then change to negative value
|
||||
let "dindex = 0 - $Days" # which can be returned from function.
|
||||
else let "dindex = $Days"
|
||||
fi
|
||||
|
||||
return $dindex
|
||||
|
||||
}
|
||||
|
||||
|
||||
calculate_difference () # Difference between to day indices.
|
||||
{
|
||||
let "diff = $1 - $2" # Global variable.
|
||||
}
|
||||
|
||||
|
||||
abs () # Absolute value
|
||||
{ # Uses global "value" variable.
|
||||
if [ "$1" -lt 0 ] # If negative
|
||||
then # then
|
||||
let "value = 0 - $1" # change sign,
|
||||
else # else
|
||||
let "value = $1" # leave it alone.
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
if [ $# -ne "$ARGS" ] # Require two command line params.
|
||||
then
|
||||
Param_Error
|
||||
fi
|
||||
|
||||
Parse_Date $1
|
||||
check_date $day $month $year # See if valid date.
|
||||
|
||||
strip_leading_zero $day # Remove any leading zeroes
|
||||
day=$? # on day and/or month.
|
||||
strip_leading_zero $month
|
||||
month=$?
|
||||
|
||||
day_index $day $month $year
|
||||
date1=$?
|
||||
|
||||
abs $date1 # Make sure it's positive
|
||||
date1=$value # by getting absolute value.
|
||||
|
||||
Parse_Date $2
|
||||
check_date $day $month $year
|
||||
|
||||
strip_leading_zero $day
|
||||
day=$?
|
||||
strip_leading_zero $month
|
||||
month=$?
|
||||
|
||||
day_index $day $month $year
|
||||
date2=$?
|
||||
|
||||
abs $date2 # Make sure it's positive.
|
||||
date2=$value
|
||||
|
||||
calculate_difference $date1 $date2
|
||||
|
||||
abs $diff # Make sure it's positive.
|
||||
diff=$value
|
||||
|
||||
echo $diff
|
||||
|
||||
exit 0
|
||||
# Compare this script with the implementation of Gauss' Formula in C at
|
||||
# http://buschencrew.hypermart.net/software/datedif
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
# de-rpm.sh: Unpack an 'rpm' archive
|
||||
|
||||
E_NO_ARGS=65
|
||||
TEMPFILE=$$.cpio # Tempfile with "unique" name.
|
||||
# $$ is process ID of script.
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_NO_ARGS
|
||||
fi
|
||||
|
||||
|
||||
rpm2cpio < $1 > $TEMPFILE # Converts rpm archive into cpio archive.
|
||||
cpio --make-directories -F $TEMPFILE -i # Unpacks cpio archive.
|
||||
rm -f $TEMPFILE # Deletes cpio archive.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,14 @@
|
|||
#!/bin/bash
|
||||
# erase.sh: Using "stty" to set an erase character when reading input.
|
||||
|
||||
echo -n "What is your name? "
|
||||
read name # Try to erase characters of input.
|
||||
# Won't work.
|
||||
echo "Your name is $name."
|
||||
|
||||
stty erase '#' # Set "hashmark" (#) as erase character.
|
||||
echo -n "What is your name? "
|
||||
read name # Use # to erase last character typed.
|
||||
echo "Your name is $name."
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,21 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo
|
||||
echo "String operations using \"expr $string :\" construct"
|
||||
echo "-------------------------------------------"
|
||||
echo
|
||||
|
||||
a=1234zipper43231
|
||||
echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"."
|
||||
# Escaped parentheses.
|
||||
# Regular expression parsing.
|
||||
|
||||
echo "Length of \"$a\" is `expr "$a" : '.*'`." # Length of string
|
||||
|
||||
echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`."
|
||||
|
||||
echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`."
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,66 @@
|
|||
#!/bin/bash
|
||||
# max2.sh: Maximum of two LARGE integers.
|
||||
|
||||
# This is the previous "max.sh" example,
|
||||
# modified to permit comparing large integers.
|
||||
|
||||
EQUAL=0 # Return value if both params equal.
|
||||
MAXRETVAL=256 # Maximum positive return value from a function.
|
||||
E_PARAM_ERR=-99999 # Parameter error.
|
||||
E_NPARAM_ERR=99999 # "Normalized" parameter error.
|
||||
|
||||
max2 () # Returns larger of two numbers.
|
||||
{
|
||||
if [ -z "$2" ]
|
||||
then
|
||||
return $E_PARAM_ERR
|
||||
fi
|
||||
|
||||
if [ "$1" -eq "$2" ]
|
||||
then
|
||||
return $EQUAL
|
||||
else
|
||||
if [ "$1" -gt "$2" ]
|
||||
then
|
||||
retval=$1
|
||||
else
|
||||
retval=$2
|
||||
fi
|
||||
fi
|
||||
|
||||
# -------------------------------------------------------------- #
|
||||
# This is a workaround to enable returning a large integer
|
||||
# from this function.
|
||||
if [ "$retval" -gt "$MAXRETVAL" ] # If out of range,
|
||||
then # then
|
||||
let "retval = (( 0 - $retval ))" # adjust to a negative value.
|
||||
# (( 0 - $VALUE )) changes the sign of VALUE.
|
||||
fi
|
||||
# Large *negative* return values permitted, fortunately.
|
||||
# -------------------------------------------------------------- #
|
||||
|
||||
return $retval
|
||||
}
|
||||
|
||||
max2 33001 33997
|
||||
return_val=$?
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
if [ "$return_val" -lt 0 ] # If "adjusted" negative number,
|
||||
then # then
|
||||
let "return_val = (( 0 - $return_val ))" # renormalize to positive.
|
||||
fi # "Absolute value" of $return_val.
|
||||
# -------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
if [ "$return_val" -eq "$E_NPARAM_ERR" ]
|
||||
then # Parameter error "flag" gets sign changed, too.
|
||||
echo "Error: Too few parameters."
|
||||
elif [ "$return_val" -eq "$EQUAL" ]
|
||||
then
|
||||
echo "The two numbers are equal."
|
||||
else
|
||||
echo "The larger of the two numbers is $return_val."
|
||||
fi
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,30 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo
|
||||
|
||||
echo "Enter a string terminated by a \\, then press <ENTER>."
|
||||
echo "Then, enter a second string, and again press <ENTER>."
|
||||
read var1 # The "\" suppresses the newline, when reading "var1".
|
||||
# first line \
|
||||
# second line
|
||||
|
||||
echo "var1 = $var1"
|
||||
# var1 = first line second line
|
||||
|
||||
# For each line terminated by a "\",
|
||||
# you get a prompt on the next line to continue feeding characters into var1.
|
||||
|
||||
echo; echo
|
||||
|
||||
echo "Enter another string terminated by a \\ , then press <ENTER>."
|
||||
read -r var2 # The -r option causes the "\" to be read literally.
|
||||
# first line \
|
||||
|
||||
echo "var2 = $var2"
|
||||
# var2 = first line \
|
||||
|
||||
# Data entry terminates with the first <ENTER>.
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,23 @@
|
|||
#!/bin/bash
|
||||
# return-test.sh
|
||||
|
||||
# The largest positive value a function can return is 256.
|
||||
|
||||
return_test () # Returns whatever passed to it.
|
||||
{
|
||||
return $1
|
||||
}
|
||||
|
||||
return_test 27 # o.k.
|
||||
echo $? # Returns 27.
|
||||
|
||||
return_test 256 # Still o.k.
|
||||
echo $? # Returns 256.
|
||||
|
||||
return_test 257 # Error!
|
||||
echo $? # Returns 1 (return code for miscellaneous error).
|
||||
|
||||
return_test -151896 # However, large negative numbers work.
|
||||
echo $? # Returns -151896.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,33 @@
|
|||
#!/bin/bash
|
||||
# Pattern replacement at prefix / suffix of string.
|
||||
|
||||
v0=abc1234zip1234abc # Original variable.
|
||||
echo "v0 = $v0" # abc1234zip1234abc
|
||||
echo
|
||||
|
||||
# Match at prefix (beginning) of string.
|
||||
v1=${v0/#abc/ABCDEF} # abc1234zip1234abc
|
||||
# |-|
|
||||
echo "v1 = $v1" # ABCDE1234zip1234abc
|
||||
# |---|
|
||||
|
||||
# Match at suffix (end) of string.
|
||||
v2=${v0/%abc/ABCDEF} # abc1234zip123abc
|
||||
# |-|
|
||||
echo "v2 = $v2" # abc1234zip1234ABCDEF
|
||||
# |----|
|
||||
|
||||
echo
|
||||
|
||||
# ----------------------------------------------------
|
||||
# Must match at beginning / end of string,
|
||||
#+ otherwise no replacement results.
|
||||
# ----------------------------------------------------
|
||||
v3=${v0/#123/000} # Matches, but not at beginning.
|
||||
echo "v3 = $v3" # abc1234zip1234abc
|
||||
# NO REPLACEMENT.
|
||||
v4=${v0/%123/000} # Matches, but not at end.
|
||||
echo "v4 = $v4" # abc1234zip1234abc
|
||||
# NO REPLACEMENT.
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue