mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
e6d9f5caff
commit
6fab97ade3
|
@ -1,5 +1,5 @@
|
|||
The following is the copyright notice for the two included "contrib" scripts
|
||||
by Mark Moraes, behead.sh and ftpget.sh.
|
||||
The following is the mandatory copyright notice for the two included
|
||||
"contrib" scripts by Mark Moraes, "behead.sh" and "ftpget.sh".
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,14 +1,27 @@
|
|||
SOME SCRIPTS WILL NOT RUN
|
||||
SOME SCRIPTS WILL NOT RUN
|
||||
|
||||
|
||||
Note that the source code for a few of the example shell scripts
|
||||
(ex57.sh, ex70.sh, ex71.sh, ex71a.sh, ex71b.sh, encryptedpw.sh, pw.sh,
|
||||
du.sh, rnd.sh, logevents.sh, and rot13.sh) have the "<" and ">"
|
||||
in place of angle brackets (< and >). This is necessary for the Docbook
|
||||
SGML conversion. If you plan to run these scripts from the enclosed source
|
||||
files, then it will, of course, be necessary to restore the angle brackets.
|
||||
Note that the source code for a few of the example shell scripts,
|
||||
|
||||
< becomes <
|
||||
> becomes >
|
||||
<< becomes <<
|
||||
>> becomes >>
|
||||
ex57.sh
|
||||
ex70.sh
|
||||
ex71.sh
|
||||
ex71a.sh
|
||||
ex71b.sh
|
||||
encryptedpw.sh
|
||||
pw.sh
|
||||
du.sh
|
||||
rnd.sh
|
||||
logevents.sh
|
||||
rot13.sh
|
||||
read-r.sh
|
||||
|
||||
have the "<" and ">" in place of angle brackets (< and >). This
|
||||
is necessary for the Docbook SGML conversion. If you plan to run these
|
||||
scripts from the enclosed source files, then it will, of course, be
|
||||
necessary to restore the angle brackets.
|
||||
|
||||
< becomes <
|
||||
> becomes >
|
||||
<< becomes <<
|
||||
>> becomes >>
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
REM VIEWDATA: INSPIRED BY AN EXAMPLE IN "DOS POWERTOOLS", BY PAUL SOMERSON
|
||||
REM VIEWDATA
|
||||
|
||||
REM INSPIRED BY AN EXAMPLE IN "DOS POWERTOOLS"
|
||||
REM BY PAUL SOMERSON
|
||||
|
||||
|
||||
@ECHO OFF
|
||||
|
||||
IF !%1==! GOTO VIEWDATA
|
||||
REM IF NO COMMAND-LINE ARG...
|
||||
|
||||
FIND "%1" C:\BOZO\BOOKLIST.TXT
|
||||
GOTO EXIT0
|
||||
REM PRINT LINE WITH STRING MATCH, THEN EXIT.
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -5,17 +5,25 @@
|
|||
# From an article by the author of this document.
|
||||
# in issue #38 of "Linux Gazette", http://www.linuxgazette.com.
|
||||
|
||||
# This script must be run as root.
|
||||
ROOT_UID=0 # This script must be run as root.
|
||||
E_NOTROOT=67 # Non-root exit error.
|
||||
|
||||
if [ "$UID" -ne "$ROOT_UID" ]
|
||||
then
|
||||
echo "Must be root to run this script."
|
||||
exit $E_NOTROOT
|
||||
fi
|
||||
|
||||
# Use with extreme caution!
|
||||
# If something goes wrong, you may wipe out your current filesystem.
|
||||
|
||||
|
||||
NEWDISK=/dev/hdb # Assumes /dev/hdb vacant. Check!
|
||||
NEWDISK=/dev/hdb # Assumes /dev/hdb vacant. Check!
|
||||
MOUNTPOINT=/mnt/newdisk # Or choose another mount point.
|
||||
|
||||
|
||||
fdisk $NEWDISK
|
||||
mke2fs -cv $NEWDISK1 # Check for bad blocks & verbose output.
|
||||
mke2fs -cv $NEWDISK1 # Check for bad blocks & verbose output.
|
||||
# Note: /dev/hdb1, *not* /dev/hdb!
|
||||
mkdir $MOUNTPOINT
|
||||
chmod 777 $MOUNTPOINT # Makes new drive accessible to all users.
|
||||
|
@ -27,8 +35,7 @@ chmod 777 $MOUNTPOINT # Makes new drive accessible to all users.
|
|||
# If it works, umount it, and proceed.
|
||||
|
||||
# Final step:
|
||||
# Add following line to /etc/fstab.
|
||||
# Add the following line to /etc/fstab.
|
||||
# /dev/hdb1 /mnt/newdisk ext2 defaults 1 1
|
||||
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -9,14 +9,13 @@ shopt -s expand_aliases
|
|||
alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
|
||||
Jesse_James
|
||||
|
||||
|
||||
echo; echo; echo;
|
||||
|
||||
alias ll="ls -l"
|
||||
# May use either single (') or double (") quotes to define an alias.
|
||||
|
||||
echo "Trying aliased \"ll\":"
|
||||
ll /usr/X11R6/bin/mk* # Alias works.
|
||||
ll /usr/X11R6/bin/mk* #* Alias works.
|
||||
|
||||
echo
|
||||
|
||||
|
@ -28,7 +27,7 @@ echo
|
|||
alias lll="ls -l $directory$prefix"
|
||||
|
||||
echo "Trying aliased \"lll\":"
|
||||
lll # Long listing of all files in /usr/X11R6/bin stating with mk.
|
||||
lll # Long listing of all files in /usr/X11R6/bin stating with mk.
|
||||
# Alias handles concatenated variables, including wild-card o.k.
|
||||
|
||||
|
||||
|
@ -42,7 +41,7 @@ if [ TRUE ]
|
|||
then
|
||||
alias rr="ls -l"
|
||||
echo "Trying aliased \"rr\" within if/then statement:"
|
||||
rr /usr/X11R6/bin/mk* # Error message results!
|
||||
rr /usr/X11R6/bin/mk* #* Error message results!
|
||||
# Aliases not expanded within compound statements.
|
||||
echo "However, previously expanded alias still recognized:"
|
||||
ll /usr/X11R6/bin/mk*
|
||||
|
@ -55,9 +54,15 @@ while [ $count -lt 3 ]
|
|||
do
|
||||
alias rrr="ls -l"
|
||||
echo "Trying aliased \"rrr\" within \"while\" loop:"
|
||||
rrr /usr/X11R6/bin/mk* # Alias will not expand here either.
|
||||
rrr /usr/X11R6/bin/mk* #* Alias will not expand here either.
|
||||
let count+=1
|
||||
done
|
||||
|
||||
echo; echo
|
||||
|
||||
alias xyz="cat $1" # Try a positional parameter in an alias.
|
||||
xyz # If you invoke the script with a filename as a parameter.
|
||||
# This seems to work,
|
||||
#+ although the Bash documentation suggests that it shouldn't.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
# allprofs.sh: print all user profiles
|
||||
|
||||
# allprofs - print all user profiles
|
||||
# This script written by Heiner Steven, and modified by the document author.
|
||||
|
||||
FILE=.bashrc # File containing user profile,
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
ROOT_UID=0 # Root has $UID 0.
|
||||
|
||||
if [ "$UID" -eq "$ROOT_UID" ]
|
||||
if [ "$UID" -eq "$ROOT_UID" ] # Will the real "root" please stand up?
|
||||
then
|
||||
echo "You are root."
|
||||
else
|
||||
|
@ -11,3 +11,21 @@ else
|
|||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
|
||||
# ============================================================= #
|
||||
# Code below will not execute, because the script already exited.
|
||||
|
||||
# An alternate method of getting to the root of matters:
|
||||
|
||||
ROOTUSER_NAME=root
|
||||
|
||||
username=`id -nu`
|
||||
if [ "$username" = "$ROOTUSER_NAME" ]
|
||||
then
|
||||
echo "Rooty, toot, toot. You are root."
|
||||
else
|
||||
echo "You are just a regular fella."
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
ARGS=1 # Number of arguments expected.
|
||||
ARGS=1 # Number of arguments expected.
|
||||
E_BADARGS=65 # Exit value if incorrect number of args passed.
|
||||
|
||||
test $# -ne $ARGS && echo "Usage: `basename $0` $ARGS argument(s)" && exit $E_BADARGS
|
||||
|
|
|
@ -27,9 +27,9 @@ else
|
|||
fi
|
||||
|
||||
|
||||
# The -a and -o options provide
|
||||
# an alternative compound condition test.
|
||||
# Thanks to Patrick Callahan for pointing this out.
|
||||
# The -a and -o options provide
|
||||
#+ an alternative compound condition test.
|
||||
# Thanks to Patrick Callahan for pointing this out.
|
||||
|
||||
|
||||
if [ "$a" -eq 24 -a "$b" -eq 47 ]
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#!/bin/bash
|
||||
# Invoke this script with several arguments, such as "one two three".
|
||||
|
||||
E_BADARGS=65
|
||||
|
||||
if [ ! -n "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` argument1 argument2 etc."
|
||||
exit 65
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
|
||||
echo
|
||||
|
||||
index=1
|
||||
|
@ -17,7 +18,7 @@ for arg in "$*" # Doesn't work properly if "$*" isn't quoted.
|
|||
do
|
||||
echo "Arg #$index = $arg"
|
||||
let "index+=1"
|
||||
done # $* sees all arguments as single word.
|
||||
done # $* sees all arguments as single word.
|
||||
echo "Entire arg list seen as single word."
|
||||
|
||||
echo
|
||||
|
@ -29,7 +30,7 @@ for arg in "$@"
|
|||
do
|
||||
echo "Arg #$index = $arg"
|
||||
let "index+=1"
|
||||
done # $@ sees arguments as separate words.
|
||||
done # $@ sees arguments as separate words.
|
||||
echo "Arg list seen as separate words."
|
||||
|
||||
echo
|
||||
|
|
|
@ -7,22 +7,22 @@ let "n = $n + 1" # let "n = n + 1" also works.
|
|||
echo -n "$n "
|
||||
|
||||
: $((n = $n + 1))
|
||||
# ":" necessary because otherwise Bash attempts
|
||||
# to interpret "$((n = $n + 1))" as a command.
|
||||
# ":" necessary because otherwise Bash attempts
|
||||
#+ to interpret "$((n = $n + 1))" as a command.
|
||||
echo -n "$n "
|
||||
|
||||
n=$(($n + 1))
|
||||
echo -n "$n "
|
||||
|
||||
: $[ n = $n + 1 ]
|
||||
# ":" necessary because otherwise Bash attempts
|
||||
# to interpret "$((n = $n + 1))" as a command.
|
||||
# ":" necessary because otherwise Bash attempts
|
||||
#+ to interpret "$((n = $n + 1))" as a command.
|
||||
# Works even if "n" was initialized as a string.
|
||||
echo -n "$n "
|
||||
|
||||
n=$[ $n + 1 ]
|
||||
# Works even if "n" was initialized as a string.
|
||||
# Avoid this type of construct, since it is obsolete and nonportable.
|
||||
# Works even if "n" was initialized as a string.
|
||||
#* Avoid this type of construct, since it is obsolete and nonportable.
|
||||
echo -n "$n "; echo
|
||||
|
||||
# Thanks, Stephane Chazelas.
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
#!/bin/bash
|
||||
# Arithmetic tests.
|
||||
|
||||
# The (( )) construct evaluates and tests numerical expressions.
|
||||
# The (( ... )) construct evaluates and tests numerical expressions.
|
||||
|
||||
(( 0 ))
|
||||
echo "Exit status of \"(( 0 ))\" is $?."
|
||||
|
||||
(( 1 ))
|
||||
echo "Exit status of \"(( 1\ ))" is $?."
|
||||
echo "Exit status of \"(( 1 ))\" is $?."
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
# ==> Used in this document with the script author's permission.
|
||||
# ==> Comments added by document author.
|
||||
|
||||
PN=`basename "$0"` # Program name
|
||||
NOARGS=65
|
||||
PN=`basename "$0"` # Program name
|
||||
VER=`echo '$Revision$' | cut -d' ' -f2` # ==> VER=1.2
|
||||
|
||||
Usage () {
|
||||
|
@ -28,8 +29,8 @@ A number may be
|
|||
octal (base 8) starting with 0 (i.e. 014)
|
||||
hexadecimal (base 16) starting with 0x (i.e. 0xc)
|
||||
decimal otherwise (i.e. 12)" >&2
|
||||
exit 65
|
||||
}
|
||||
exit $NOARGS
|
||||
} # ==> Function to print usage message.
|
||||
|
||||
Msg () {
|
||||
for i # ==> in [list] missing.
|
||||
|
@ -81,7 +82,7 @@ while [ $# -gt 0 ]
|
|||
do
|
||||
case "$1" in
|
||||
--) shift; break;;
|
||||
-h) Usage;; # ==> Help message.
|
||||
-h) Usage;; # ==> Help message.
|
||||
-*) Usage;;
|
||||
*) break;; # first number
|
||||
esac # ==> More error checking for illegal input would be useful.
|
||||
|
|
|
@ -3,22 +3,22 @@
|
|||
# empty line
|
||||
# Mark Moraes, University of Toronto
|
||||
|
||||
# --> These comments added by author of this document.
|
||||
# ==> These comments added by author of this document.
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
# --> If no command line args present, then works on file redirected to stdin.
|
||||
# ==> If no command line args present, then works on file redirected to stdin.
|
||||
sed -e '1,/^$/d' -e '/^[ ]*$/d'
|
||||
# --> Delete empty lines and all lines until
|
||||
# --> first one beginning with white space.
|
||||
else
|
||||
# --> If command line args present, then work on files named.
|
||||
# ==> If command line args present, then work on files named.
|
||||
for i do
|
||||
sed -e '1,/^$/d' -e '/^[ ]*$/d' $i
|
||||
# --> Ditto, as above.
|
||||
done
|
||||
fi
|
||||
|
||||
# --> Exercise for the reader: Add error checking and other options.
|
||||
# -->
|
||||
# --> Note that the small sed script repeats, except for the arg passed.
|
||||
# --> Does it make sense to embed it in a function? Why or why not?
|
||||
# ==> Exercise for the reader: Add error checking and other options.
|
||||
# ==>
|
||||
# ==> Note that the small sed script repeats, except for the arg passed.
|
||||
# ==> Does it make sense to embed it in a function? Why or why not?
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#!/bin/bash
|
||||
# Locates matching strings in a binary file.
|
||||
# bin-grep.sh: Locates matching strings in a binary file.
|
||||
|
||||
# A "grep" replacement for binary files.
|
||||
# Similar effect to "grep -a"
|
||||
|
||||
E_BADARGS=65
|
||||
NOFILE=66
|
||||
E_NOFILE=66
|
||||
|
||||
if [ $# -ne 2 ]
|
||||
then
|
||||
|
@ -16,13 +16,13 @@ fi
|
|||
if [ ! -f "$2" ]
|
||||
then
|
||||
echo "File \"$2\" does not exist."
|
||||
exit $NOFILE
|
||||
exit $E_NOFILE
|
||||
fi
|
||||
|
||||
|
||||
for word in $( strings "$2" | grep "$1" )
|
||||
# The "strings" command lists strings in binary files,
|
||||
# then piped to "grep", which tests for desired string.
|
||||
# The "strings" command lists strings in binary files.
|
||||
# Output then piped to "grep", which tests for desired string.
|
||||
do
|
||||
echo $word
|
||||
done
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#!/bin/bash
|
||||
# Breaking out of loops.
|
||||
# break-levels.sh: Breaking out of loops.
|
||||
|
||||
# "break N" breaks out of N level loops.
|
||||
|
||||
|
@ -13,11 +13,9 @@ do
|
|||
|
||||
if [ "$innerloop" -eq 3 ]
|
||||
then
|
||||
break
|
||||
# Replace the line above with break 2
|
||||
# to see what happens ("breaks" out of both inner and outer loops.)
|
||||
break # Try break 2 to see what happens.
|
||||
# ("Breaks" out of both inner and outer loops.)
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
echo
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Bubble sort, of sorts.
|
||||
# bubble.sh: Bubble sort, of sorts.
|
||||
|
||||
# Recall the algorithm for a bubble sort. In this particular version...
|
||||
|
||||
|
@ -26,9 +25,9 @@ exchange()
|
|||
declare -a Countries # Declare array, optional here since it's initialized below.
|
||||
|
||||
Countries=(Netherlands Ukraine Zaire Turkey Russia Yemen Syria Brazil Argentina Nicaragua Japan Mexico Venezuela Greece England Israel Peru Canada Oman Denmark Wales France Kenya Qatar Liechtenstein Hungary)
|
||||
# Couldn't think of one starting with X (darn).
|
||||
# Couldn't think of one starting with X (darn!).
|
||||
|
||||
clear # Clear the screen to start with.
|
||||
clear # Clear the screen to start with.
|
||||
|
||||
echo "0: ${Countries[*]}" # List entire array at pass 0.
|
||||
|
||||
|
@ -37,16 +36,19 @@ let "comparisons = $number_of_elements - 1"
|
|||
|
||||
count=1 # Pass number.
|
||||
|
||||
while [ $comparisons -gt 0 ] # Beginning of outer loop
|
||||
while [ "$comparisons" -gt 0 ] # Beginning of outer loop
|
||||
do
|
||||
|
||||
index=0 # Reset index to start of array after each pass.
|
||||
|
||||
while [ $index -lt $comparisons ] # Beginning of inner loop
|
||||
while [ "$index" -lt "$comparisons" ] # Beginning of inner loop
|
||||
do
|
||||
if [ ${Countries[$index]} \> ${Countries[`expr $index + 1`]} ]
|
||||
# If out of order...
|
||||
# Recalling that \> is ASCII comparison operator.
|
||||
|
||||
# if [[ ${Countries[$index]} > ${Countries[`expr $index + 1`]} ]]
|
||||
# also works.
|
||||
then
|
||||
exchange $index `expr $index + 1` # Swap.
|
||||
fi
|
||||
|
@ -54,16 +56,15 @@ do
|
|||
done # End of inner loop
|
||||
|
||||
|
||||
let "comparisons -= 1"
|
||||
# Since "heaviest" element bubbles to bottom, we need do one less comparison each pass.
|
||||
let "comparisons -= 1" # Since "heaviest" element bubbles to bottom,
|
||||
# we need do one less comparison each pass.
|
||||
|
||||
echo
|
||||
echo "$count: ${Countries[@]}"
|
||||
# Print resultant array at end of each pass.
|
||||
echo "$count: ${Countries[@]}" # Print resultant array at end of each pass.
|
||||
echo
|
||||
let "count += 1" # Increment pass count.
|
||||
let "count += 1" # Increment pass count.
|
||||
|
||||
done # End of outer loop
|
||||
done # End of outer loop
|
||||
|
||||
# All done.
|
||||
|
||||
|
|
|
@ -1,22 +1,23 @@
|
|||
#!/bin/bash
|
||||
# Manipulating a variable, C-style, using the ((...)) construct.
|
||||
|
||||
|
||||
echo
|
||||
|
||||
(( a = 23 )) # Setting a value, C-style, with spaces on both sides of the "=".
|
||||
echo "a (initial value) = $a"
|
||||
|
||||
(( a++ )) # Post-increment 'a', C-style.
|
||||
(( a++ )) # Post-increment 'a', C-style.
|
||||
echo "a (after a++) = $a"
|
||||
|
||||
(( a-- )) # Post-decrement 'a', C-style.
|
||||
(( a-- )) # Post-decrement 'a', C-style.
|
||||
echo "a (after a--) = $a"
|
||||
|
||||
|
||||
(( ++a )) # Pre-increment 'a', C-style.
|
||||
(( ++a )) # Pre-increment 'a', C-style.
|
||||
echo "a (after ++a) = $a"
|
||||
|
||||
(( --a )) # Pre-decrement 'a', C-style.
|
||||
(( --a )) # Pre-decrement 'a', C-style.
|
||||
echo "a (after --a) = $a"
|
||||
|
||||
echo
|
||||
|
@ -37,7 +38,6 @@ echo
|
|||
|
||||
# See also "for" and "while" loops using the ((...)) construct.
|
||||
|
||||
# Note: these may not work with early versions of Bash,
|
||||
# or on other platforms besides Linux.
|
||||
# These work only with Bash, version 2.04 or later.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
#!/bin/bash
|
||||
# Using command substitution to generate a "case" variable.
|
||||
|
||||
|
||||
case $( arch ) in # "arch" returns machine architecture.
|
||||
i386 ) echo "80386-based machine";;
|
||||
i486 ) echo "80486-based machine";;
|
||||
i586 ) echo "Pentium-based machine";;
|
||||
i686 ) echo "Pentium2-based machine";;
|
||||
i686 ) echo "Pentium2+-based machine";;
|
||||
* ) echo "Other type of machine";;
|
||||
esac
|
||||
|
||||
|
|
|
@ -3,13 +3,12 @@
|
|||
# Adds up a specified column (of numbers) in the target file.
|
||||
|
||||
ARGS=2
|
||||
WRONGARGS=65
|
||||
E_WRONGARGS=65
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
# Check for proper no. of command line args.
|
||||
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
|
||||
then
|
||||
echo "Usage: `basename $0` filename column-number"
|
||||
exit $WRONGARGS
|
||||
exit $E_WRONGARGS
|
||||
fi
|
||||
|
||||
filename=$1
|
||||
|
|
|
@ -5,13 +5,12 @@
|
|||
# This uses indirect references.
|
||||
|
||||
ARGS=2
|
||||
WRONGARGS=65
|
||||
E_WRONGARGS=65
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
# Check for proper no. of command line args.
|
||||
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
|
||||
then
|
||||
echo "Usage: `basename $0` filename column-number"
|
||||
exit $WRONGARGS
|
||||
exit $E_WRONGARGS
|
||||
fi
|
||||
|
||||
filename=$1
|
||||
|
|
|
@ -5,13 +5,12 @@
|
|||
# This uses the environment to pass a script variable to 'awk'.
|
||||
|
||||
ARGS=2
|
||||
WRONGARGS=65
|
||||
E_WRONGARGS=65
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
# Check for proper no. of command line args.
|
||||
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
|
||||
then
|
||||
echo "Usage: `basename $0` filename column-number"
|
||||
exit $WRONGARGS
|
||||
exit $E_WRONGARGS
|
||||
fi
|
||||
|
||||
filename=$1
|
||||
|
|
|
@ -3,14 +3,14 @@
|
|||
PROCNAME=pppd # ppp daemon
|
||||
PROCFILENAME=status # Where to look.
|
||||
NOTCONNECTED=65
|
||||
INTERVAL=2 # Update every 2 seconds.
|
||||
INTERVAL=2 # Update every 2 seconds.
|
||||
|
||||
pidno=$( ps ax | grep -v "ps ax" | grep -v grep | grep $PROCNAME | awk '{ print $1 }' )
|
||||
# Finding the process number of 'pppd', the 'ppp daemon'.
|
||||
# Have to filter out the process lines generated by the search itself.
|
||||
|
||||
|
||||
if [ -z "$pidno" ] # If no pid, then process is not running.
|
||||
if [ -z "$pidno" ] # If no pid, then process is not running.
|
||||
then
|
||||
echo "Not connected."
|
||||
exit $NOTCONNECTED
|
||||
|
@ -18,7 +18,7 @@ else
|
|||
echo "Connected."; echo
|
||||
fi
|
||||
|
||||
while [ true ] # Endless loop, script can be improved here.
|
||||
while [ true ] # Endless loop, script can be improved here.
|
||||
do
|
||||
|
||||
if [ ! -e "/proc/$pidno/$PROCFILENAME" ]
|
||||
|
@ -40,6 +40,7 @@ done
|
|||
exit 0
|
||||
|
||||
# As it stands, this script must be terminated with a Control-C.
|
||||
# Exercises for the reader:
|
||||
|
||||
# Exercises for the reader:
|
||||
# Improve the script so it exits on a "q" keystroke.
|
||||
# Make the script more user-friendly in other ways.
|
||||
|
|
|
@ -11,7 +11,8 @@ do
|
|||
if [ "$inner" -eq 7 ]
|
||||
then
|
||||
continue 2 # Continue at loop on 2nd level, that is "outer loop".
|
||||
# Replace above line with a simple "continue" to see normal loop behavior.
|
||||
# Replace above line with a simple "continue"
|
||||
# to see normal loop behavior.
|
||||
fi
|
||||
|
||||
echo -n "$inner " # 8 9 10 will never echo.
|
||||
|
|
|
@ -9,24 +9,24 @@ SPEED=2 # May use higher speed if supported.
|
|||
|
||||
echo; echo "Insert source CD, but do *not* mount it."
|
||||
echo "Press ENTER when ready. "
|
||||
read ready # Wait for input, $ready not used.
|
||||
read ready # Wait for input, $ready not used.
|
||||
|
||||
echo; echo "Copying the source CD to $OF."
|
||||
echo "This may take a while. Please be patient."
|
||||
|
||||
dd if=$CDROM of=$OF bs=$BLOCKSIZE # Raw device copy.
|
||||
dd if=$CDROM of=$OF bs=$BLOCKSIZE # Raw device copy.
|
||||
|
||||
|
||||
echo; echo "Remove data CD."
|
||||
echo "Insert blank CDR."
|
||||
echo "Press ENTER when ready. "
|
||||
read ready # Wait for input, $ready not used.
|
||||
read ready # Wait for input, $ready not used.
|
||||
|
||||
echo "Copying $OF to CDR."
|
||||
|
||||
cdrecord -v -isosize speed=$SPEED dev=0,0 $OF
|
||||
# Uses Joerg Schilling's "cdrecord" package (see its docs).
|
||||
# (http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html)
|
||||
# http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html
|
||||
|
||||
|
||||
echo; echo "Done copying $OF to CDR on device $CDROM."
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
#!/bin/bash
|
||||
# du.sh: DOS to UNIX text file converter.
|
||||
|
||||
WRONGARGS=65
|
||||
E_WRONGARGS=65
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename-to-convert"
|
||||
exit $WRONGARGS
|
||||
exit $E_WRONGARGS
|
||||
fi
|
||||
|
||||
NEWFILENAME=$1.unx
|
||||
|
||||
CR='\015' # Carriage return.
|
||||
# Lines in DOS text files end in a CR-LF.
|
||||
# Lines in a DOS text file end in a CR-LF.
|
||||
|
||||
tr -d $CR < $1 > $NEWFILENAME
|
||||
# Delete CR and write to new file.
|
||||
|
|
|
@ -23,6 +23,4 @@ echo "Number of elements in array2 = ${#array2[*]}" # 0
|
|||
|
||||
echo
|
||||
|
||||
# Thanks, S.C.
|
||||
|
||||
exit 0
|
||||
exit 0 # Thanks, S.C.
|
||||
|
|
|
@ -1,22 +1,21 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Example 3-71 modified to use encrypted password.
|
||||
# Example "ex72.sh" modified to use encrypted password.
|
||||
|
||||
E_BADARGS=65
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit 65
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
Username=bozo
|
||||
# Change to suit.
|
||||
Username=bozo # Change to suit.
|
||||
|
||||
Filename=`basename $1`
|
||||
# Strips pathname out of file name
|
||||
Filename=`basename $1` # Strips pathname out of file name
|
||||
|
||||
Server="XXX"
|
||||
Directory="YYY"
|
||||
# Change above to actual server name & directory.
|
||||
Directory="YYY" # Change above to actual server name & directory.
|
||||
|
||||
|
||||
password=`cruft <pword`
|
||||
|
@ -29,15 +28,14 @@ password=`cruft <pword`
|
|||
|
||||
|
||||
ftp -n $Server <<End-Of-Session
|
||||
# -n option disables auto-logon
|
||||
|
||||
user $Username $Password
|
||||
binary
|
||||
bell
|
||||
# Ring 'bell' after each file transfer
|
||||
cd $Directory
|
||||
put $Filename
|
||||
bye
|
||||
End-Of-Session
|
||||
# -n option to "ftp" disables auto-logon.
|
||||
# "bell" rings 'bell' after each file transfer.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,23 +1,38 @@
|
|||
#!/bin/bash
|
||||
# Escaped characters
|
||||
# escaped.sh: escaped characters
|
||||
|
||||
echo; echo
|
||||
|
||||
echo "\v\v\v\v" # Prints \v\v\v\v
|
||||
# Must use the -e option with 'echo' to print escaped characters.
|
||||
echo -e "\v\v\v\v" # Prints 4 vertical tabs.
|
||||
echo -e "\042" # Prints " (quote, octal ASCII character 42).
|
||||
echo -e "\042" # Prints " (quote, octal ASCII character 42).
|
||||
|
||||
|
||||
# Bash, version 2 and later, permits using the $'\xxx' construct.
|
||||
echo $'\n'
|
||||
echo $'\a'
|
||||
echo $'\t \042 \t' # Quote (") framed by tabs.
|
||||
echo $'\t \042 \t' # Quote (") framed by tabs.
|
||||
|
||||
# Assigning ASCII characters to a variable.
|
||||
# ----------------------------------------
|
||||
quote=$'\042' # " assigned to a variable.
|
||||
quote=$'\042' # " assigned to a variable.
|
||||
echo "$quote This is a quoted string, $quote and this lies outside the quotes."
|
||||
|
||||
echo
|
||||
|
||||
# Concatenating ASCII chars in a variable.
|
||||
triple_underline=$'\137\137\137' # 137 is octal ASCII code for _
|
||||
triple_underline=$'\137\137\137' # 137 is octal ASCII code for "_".
|
||||
echo "$triple_underline UNDERLINE $triple_underline"
|
||||
|
||||
ABC=$'\101\102\103\010' # 101, 102, 103 are octal A, B, C.
|
||||
echo $ABC
|
||||
|
||||
echo; echo
|
||||
|
||||
escape=$'\033' # 033 is octal for escape.
|
||||
echo "\"escape\" echoes as $escape"
|
||||
|
||||
echo; echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
echo
|
||||
|
||||
echo "Testing \"0\""
|
||||
if [ 0 ]
|
||||
#zero
|
||||
if [ 0 ] # zero
|
||||
then
|
||||
echo "0 is true."
|
||||
else
|
||||
|
@ -14,8 +13,7 @@ fi
|
|||
echo
|
||||
|
||||
echo "Testing \"NULL\""
|
||||
if [ ]
|
||||
#NULL (empty condition)
|
||||
if [ ] # NULL (empty condition)
|
||||
then
|
||||
echo "NULL is true."
|
||||
else
|
||||
|
@ -25,8 +23,7 @@ fi
|
|||
echo
|
||||
|
||||
echo "Testing \"xyz\""
|
||||
if [ xyz ]
|
||||
#string
|
||||
if [ xyz ] # string
|
||||
then
|
||||
echo "Random string is true."
|
||||
else
|
||||
|
@ -36,8 +33,8 @@ fi
|
|||
echo
|
||||
|
||||
echo "Testing \"\$xyz\""
|
||||
if [ $xyz ] # Tests if $xyz is null, but...
|
||||
#string
|
||||
if [ $xyz ] # Tests if $xyz is null, but...
|
||||
# it's only an uninitialized variable.
|
||||
then
|
||||
echo "Uninitialized variable is true."
|
||||
else
|
||||
|
@ -47,8 +44,7 @@ fi
|
|||
echo
|
||||
|
||||
echo "Testing \"-n \$xyz\""
|
||||
if [ -n "$xyz" ] # ...this is better.
|
||||
#string
|
||||
if [ -n "$xyz" ] # More pedantically correct.
|
||||
then
|
||||
echo "Uninitialized variable is true."
|
||||
else
|
||||
|
@ -57,6 +53,7 @@ fi
|
|||
|
||||
echo
|
||||
|
||||
|
||||
# When is "false" true?
|
||||
|
||||
echo "Testing \"false\""
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
echo
|
||||
|
||||
|
||||
if test -z "$1"
|
||||
then
|
||||
echo "No command-line arguments."
|
||||
|
@ -12,16 +11,14 @@ fi
|
|||
|
||||
# Both code blocks are functionally identical.
|
||||
|
||||
if [ -z "$1" ]
|
||||
# if [ -z "$1"
|
||||
# also works, but outputs an error message.
|
||||
if [ -z "$1" ] # if [ -z "$1" should work, but...
|
||||
# Bash responds to a missing close bracket with an error message.
|
||||
then
|
||||
echo "No command-line arguments."
|
||||
else
|
||||
echo "First command-line argument is $1."
|
||||
fi
|
||||
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -5,7 +5,7 @@ filename=sys.log
|
|||
cat /dev/null > $filename; echo "Creating / cleaning out file."
|
||||
# Creates file if it does not already exist,
|
||||
# and truncates it to zero length if it does.
|
||||
# : > filename would also work.
|
||||
# : > filename also works.
|
||||
|
||||
tail /var/log/messages > $filename
|
||||
# /var/log/messages must have world read permission for this to work.
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
a=4
|
||||
b=5
|
||||
|
||||
# Here a and b can be treated either as integers or strings.
|
||||
# Here "a" and "b" can be treated either as integers or strings.
|
||||
# There is some blurring between the arithmetic and string comparisons.
|
||||
# Be careful.
|
||||
# Caution advised.
|
||||
|
||||
if [ "$a" -ne "$b" ]
|
||||
then
|
||||
|
@ -21,6 +21,8 @@ then
|
|||
echo "(string comparison)"
|
||||
fi
|
||||
|
||||
# In this instance, both "-ne" and "!=" work.
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -7,7 +7,7 @@ NOTFOUND=66
|
|||
NOTGZIP=67
|
||||
|
||||
if [ $# -eq 0 ] # same effect as: if [ -z "$1" ]
|
||||
# $1 can be empty but present: zmost "" arg2 arg3
|
||||
# $1 can exist, but be empty: zmost "" arg2 arg3
|
||||
then
|
||||
echo "Usage: `basename $0` filename" >&2
|
||||
# Error message to stderr.
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
echo
|
||||
|
||||
# When is a variable "naked", i.e., lacking the '$' in front?
|
||||
# When it is being assigned, rather than referenced.
|
||||
|
||||
# Assignment
|
||||
a=879
|
||||
|
@ -24,7 +25,7 @@ done
|
|||
echo
|
||||
echo
|
||||
|
||||
# In a 'read' statement
|
||||
# In a 'read' statement (also a type of assignment)
|
||||
echo -n "Enter \"a\" "
|
||||
read a
|
||||
echo "The value of \"a\" is now $a"
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
#!/bin/bash
|
||||
|
||||
a=23
|
||||
# Simple case
|
||||
a=23 # Simple case
|
||||
echo $a
|
||||
b=$a
|
||||
echo $b
|
||||
|
||||
# Now, getting a little bit fancier...
|
||||
|
||||
a=`echo Hello!`
|
||||
# Assigns result of 'echo' command to 'a'
|
||||
a=`echo Hello!` # Assigns result of 'echo' command to 'a'
|
||||
echo $a
|
||||
|
||||
a=`ls -l`
|
||||
# Assigns result of 'ls -l' command to 'a'
|
||||
a=`ls -l` # Assigns result of 'ls -l' command to 'a'
|
||||
echo $a
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -8,14 +8,13 @@ echo
|
|||
echo "The name of this script is \"$0\"."
|
||||
# Adds ./ for current directory
|
||||
echo "The name of this script is \"`basename $0`\"."
|
||||
# Strip out path name info (see 'basename')
|
||||
# Strips out path name info (see 'basename')
|
||||
|
||||
echo
|
||||
|
||||
if [ -n "$1" ] # Tested variable is quoted.
|
||||
if [ -n "$1" ] # Tested variable is quoted.
|
||||
then
|
||||
echo "Parameter #1 is $1"
|
||||
# Need quotes to escape #
|
||||
echo "Parameter #1 is $1" # Need quotes to escape #
|
||||
fi
|
||||
|
||||
if [ -n "$2" ]
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Does a 'whois domain-name' lookup
|
||||
# on any of 3 alternate servers:
|
||||
# ripe.net, cw.net, radb.net
|
||||
# Does a 'whois domain-name' lookup on any of 3 alternate servers:
|
||||
# ripe.net, cw.net, radb.net
|
||||
|
||||
# Place this script, named 'wh' in /usr/local/bin
|
||||
|
||||
|
@ -25,6 +24,6 @@ case `basename $0` in
|
|||
"wh-radb") whois $1@whois.radb.net;;
|
||||
"wh-cw" ) whois $1@whois.cw.net;;
|
||||
* ) echo "Usage: `basename $0` [domain-name]";;
|
||||
esac
|
||||
esac
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
#!/bin/bash
|
||||
# Using 'shift' to step through all the positional parameters.
|
||||
|
||||
# Name this script something like shift000,
|
||||
# Name this script something like shft,
|
||||
# and invoke it with some parameters, for example
|
||||
# ./shift000 a b c def 23 skidoo
|
||||
# ./shft a b c def 23 skidoo
|
||||
|
||||
# Demo of using 'shift'
|
||||
# to step through all the positional parameters.
|
||||
|
||||
until [ -z "$1" ]
|
||||
until [ -z "$1" ] # Until all parameters used up...
|
||||
do
|
||||
echo -n "$1 "
|
||||
shift
|
||||
done
|
||||
|
||||
echo
|
||||
# Extra line feed.
|
||||
echo # Extra line feed.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -3,16 +3,16 @@
|
|||
# Run as root, of course.
|
||||
|
||||
LOG_DIR=/var/log
|
||||
ROOT_UID=0 # Only users with $UID 0 have root privileges.
|
||||
LINES=50 # Default number of lines saved.
|
||||
XCD=66 # Can't change directory?
|
||||
NOTROOT=67 # Non-root exit error.
|
||||
ROOT_UID=0 # Only users with $UID 0 have root privileges.
|
||||
LINES=50 # Default number of lines saved.
|
||||
E_XCD=66 # Can't change directory?
|
||||
E_NOTROOT=67 # Non-root exit error.
|
||||
|
||||
|
||||
if [ "$UID" -ne "$ROOT_UID" ]
|
||||
then
|
||||
echo "Must be root to run this script."
|
||||
exit $NOTROOT
|
||||
exit $E_NOTROOT
|
||||
fi
|
||||
|
||||
if [ -n "$1" ]
|
||||
|
@ -20,55 +20,54 @@ if [ -n "$1" ]
|
|||
then
|
||||
lines=$1
|
||||
else
|
||||
lines=$LINES
|
||||
# default, if not specified on command line.
|
||||
lines=$LINES # Default, if not specified on command line.
|
||||
fi
|
||||
|
||||
|
||||
# Stephane Chazelas suggests the following,
|
||||
# as a better way of checking command line arguments,
|
||||
# but this is still a bit advanced for this stage of the tutorial.
|
||||
# Stephane Chazelas suggests the following,
|
||||
#+ as a better way of checking command line arguments,
|
||||
#+ but this is still a bit advanced for this stage of the tutorial.
|
||||
#
|
||||
# WRONGARGS=65 # Non-numerical argument (bad arg format)
|
||||
# E_WRONGARGS=65 # Non-numerical argument (bad arg format)
|
||||
#
|
||||
# case "$1" in
|
||||
# "" ) lines=50;;
|
||||
# *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $WRONGARGS;;
|
||||
# *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;;
|
||||
# * ) lines=$1;;
|
||||
# esac
|
||||
#
|
||||
# Skip ahead to "Loops" to understand this.
|
||||
#* Skip ahead to "Loops" to understand this.
|
||||
|
||||
|
||||
cd $LOG_DIR
|
||||
|
||||
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "LOG_DIR" ]
|
||||
# Not in /var/log?
|
||||
# Not in /var/log?
|
||||
then
|
||||
echo "Can't change to $LOG_DIR."
|
||||
exit $XCD
|
||||
exit $E_XCD
|
||||
fi # Doublecheck if in right directory, before messing with log file.
|
||||
|
||||
# far better is:
|
||||
# ---
|
||||
# cd /var/log || {
|
||||
# echo "Cannot change to necessary directory." >&2
|
||||
# exit $XCD;
|
||||
# exit $E_XCD;
|
||||
# }
|
||||
|
||||
|
||||
|
||||
|
||||
tail -$lines messages > mesg.temp # Saves last section of message log file.
|
||||
mv mesg.temp messages # Becomes new log directory.
|
||||
mv mesg.temp messages # Becomes new log directory.
|
||||
|
||||
|
||||
# cat /dev/null > messages
|
||||
# No longer needed, as the above method is safer.
|
||||
#* No longer needed, as the above method is safer.
|
||||
|
||||
cat /dev/null > wtmp # > wtemp has the same effect.
|
||||
echo "Logs cleaned up."
|
||||
|
||||
exit 0
|
||||
# A zero return value from the script upon exit
|
||||
# indicates success to the shell.
|
||||
# A zero return value from the script upon exit
|
||||
#+ indicates success to the shell.
|
||||
|
|
|
@ -5,30 +5,27 @@ func1 ()
|
|||
echo This is a function.
|
||||
}
|
||||
|
||||
declare -f
|
||||
# Lists the function above.
|
||||
declare -f # Lists the function above.
|
||||
|
||||
echo
|
||||
|
||||
declare -i var1 # var1 is an integer.
|
||||
var1=2367
|
||||
echo "var1 declared as $var1"
|
||||
var1=var1+1
|
||||
# Integer declaration eliminates the need for 'let'.
|
||||
var1=var1+1 # Integer declaration eliminates the need for 'let'.
|
||||
echo "var1 incremented by 1 is $var1."
|
||||
# Attempt to change variable declared as integer
|
||||
echo "Attempting to change var1 to floating point value, 2367.1."
|
||||
var1=2367.1
|
||||
# Results in error message, with no change to variable.
|
||||
var1=2367.1 # Results in error message, with no change to variable.
|
||||
echo "var1 is still $var1"
|
||||
|
||||
echo
|
||||
|
||||
declare -r var2=13.36
|
||||
echo "var2 declared as $var2"
|
||||
# Attempt to change readonly variable.
|
||||
var2=13.37
|
||||
# Generates error message, and exit from script.
|
||||
echo "var2 is still $var2" # This line will not execute.
|
||||
declare -r var2=13.36 # 'declare' permits setting a variable property
|
||||
#+ and simultaneously assigning it a value.
|
||||
echo "var2 declared as $var2" # Attempt to change readonly variable.
|
||||
var2=13.37 # Generates error message, and exit from script.
|
||||
|
||||
exit 0 # Script will not exit here.
|
||||
echo "var2 is still $var2" # This line will not execute.
|
||||
|
||||
exit 0 # Script will not exit here.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# $RANDOM returns a different random integer at each invocation.
|
||||
# Nominal range: 0 - 32767 (signed integer).
|
||||
# Nominal range: 0 - 32767 (signed 16-bit integer).
|
||||
|
||||
MAXCOUNT=10
|
||||
count=1
|
||||
|
@ -17,7 +17,7 @@ do
|
|||
done
|
||||
echo "-----------------"
|
||||
|
||||
# If you need a random int within a certain range, then use the 'modulo' operator.
|
||||
# If you need a random int within a certain range, use the 'modulo' operator.
|
||||
# This returns the remainder of a division operation.
|
||||
|
||||
RANGE=500
|
||||
|
@ -26,7 +26,7 @@ echo
|
|||
|
||||
number=$RANDOM
|
||||
let "number %= $RANGE"
|
||||
echo "Random number less than $RANGE --> $number"
|
||||
echo "Random number less than $RANGE --- $number"
|
||||
|
||||
echo
|
||||
|
||||
|
@ -40,7 +40,7 @@ while [ "$number" -le $FLOOR ]
|
|||
do
|
||||
number=$RANDOM
|
||||
done
|
||||
echo "Random number greater than $FLOOR --> $number"
|
||||
echo "Random number greater than $FLOOR --- $number"
|
||||
echo
|
||||
|
||||
|
||||
|
@ -51,11 +51,11 @@ do
|
|||
number=$RANDOM
|
||||
let "number %= $RANGE" # Scales $number down within $RANGE.
|
||||
done
|
||||
echo "Random number between $FLOOR and $RANGE --> $number"
|
||||
echo "Random number between $FLOOR and $RANGE --- $number"
|
||||
echo
|
||||
|
||||
|
||||
# May generate binary choice, that is, "true" or "false" value.
|
||||
# Generate binary choice, that is, "true" or "false" value.
|
||||
BINARY=2
|
||||
number=$RANDOM
|
||||
T=1
|
||||
|
@ -74,7 +74,7 @@ echo
|
|||
|
||||
|
||||
# May generate toss of the dice.
|
||||
SPOTS=7
|
||||
SPOTS=7 # Modulo 7 gives range 0 - 6.
|
||||
DICE=2
|
||||
ZERO=0
|
||||
die1=0
|
||||
|
@ -82,14 +82,14 @@ die2=0
|
|||
|
||||
# Tosses each die separately, and so gives correct odds.
|
||||
|
||||
while [ "$die1" -eq $ZERO ] #Can't have a zero come up.
|
||||
while [ "$die1" -eq $ZERO ] # Can't have a zero come up.
|
||||
do
|
||||
let "die1 = $RANDOM % $SPOTS"
|
||||
let "die1 = $RANDOM % $SPOTS" # Roll first one.
|
||||
done
|
||||
|
||||
while [ "$die2" -eq $ZERO ]
|
||||
do
|
||||
let "die2 = $RANDOM % $SPOTS"
|
||||
let "die2 = $RANDOM % $SPOTS" # Roll second one.
|
||||
done
|
||||
|
||||
let "throw = $die1 + $die2"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
# Planets revisited.
|
||||
|
||||
# Want to associate name of each planet with its distance from the sun.
|
||||
# Associate the name of each planet with its distance from the sun.
|
||||
|
||||
for planet in "Mercury 36" "Venus 67" "Earth 93" "Mars 142" "Jupiter 483"
|
||||
do
|
||||
|
@ -10,7 +10,7 @@ do
|
|||
|
||||
# May need to save original positional parameters, since they get overwritten.
|
||||
# One way of doing this is to use an array,
|
||||
# original_params=("$@)
|
||||
# original_params=("$@")
|
||||
|
||||
echo "$1 $2,000,000 miles from the sun"
|
||||
#-------two tabs---concatenate zeroes onto parameter $2
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Invoke both with and without arguments,
|
||||
# and see what happens.
|
||||
# Invoke both with and without arguments, and see what happens.
|
||||
|
||||
for a
|
||||
do
|
||||
echo $a
|
||||
echo -n "$a "
|
||||
done
|
||||
|
||||
# 'in list' missing, therefore operates on '$@'
|
||||
# (command-line argument list, including white space)
|
||||
# The 'in list' missing, therefore the loop operates on '$@'
|
||||
# (command-line argument list, including whitespace).
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#!/bin/bash
|
||||
|
||||
ARGS=2
|
||||
EXPECTED_ARGS=2
|
||||
E_BADARGS=65
|
||||
|
||||
if [ $# -ne $ARGS ]
|
||||
if [ $# -ne $EXPECTED_ARGS ]
|
||||
# Check for proper no. of command line args.
|
||||
then
|
||||
echo "Usage: `basename $0` phone# text-file"
|
||||
|
@ -18,22 +18,19 @@ then
|
|||
fi
|
||||
|
||||
|
||||
# Create fax formatted files from text files.
|
||||
fax make $2
|
||||
fax make $2 # Create fax formatted files from text files.
|
||||
|
||||
for file in $(ls $2.0*)
|
||||
# Concatenate the converted files.
|
||||
# Uses wild card in variable list.
|
||||
for file in $(ls $2.0*) # Concatenate the converted files.
|
||||
# Uses wild card in variable list.
|
||||
do
|
||||
fil="$fil $file"
|
||||
done
|
||||
|
||||
# Do the work.
|
||||
efax -d /dev/ttyS3 -o1 -t "T$1" $fil
|
||||
efax -d /dev/ttyS3 -o1 -t "T$1" $fil # Do the work.
|
||||
|
||||
|
||||
# As S.C. points out, the for-loop can be eliminated with
|
||||
# efax -d /dev/ttyS3 -o1 -t "T$1" $2.0*
|
||||
# but it's not as instructive [grin].
|
||||
# but it's not quite as instructive [grin].
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -5,10 +5,8 @@ LIMIT=10
|
|||
|
||||
while [ "$var0" -lt "$LIMIT" ]
|
||||
do
|
||||
echo -n "$var0 "
|
||||
# -n suppresses newline.
|
||||
var0=`expr $var0 + 1`
|
||||
# var0=$(($var0+1)) also works.
|
||||
echo -n "$var0 " # -n suppresses newline.
|
||||
var0=`expr $var0 + 1` # var0=$(($var0+1)) also works.
|
||||
done
|
||||
|
||||
echo
|
||||
|
|
|
@ -2,16 +2,14 @@
|
|||
|
||||
echo
|
||||
|
||||
while [ "$var1" != "end" ]
|
||||
do
|
||||
while [ "$var1" != "end" ] # while test "$var1" != "end"
|
||||
do # also works.
|
||||
echo "Input variable #1 (end to exit) "
|
||||
read var1
|
||||
# It's not 'read $var1' because value of var1 is being set.
|
||||
echo "variable #1 = $var1"
|
||||
# Need quotes because of #
|
||||
read var1 # Not 'read $var1' (why?).
|
||||
echo "variable #1 = $var1" # Need quotes because of "#".
|
||||
# If input is 'end', echoes it here.
|
||||
# Does not test for termination condition until top of loop.
|
||||
echo
|
||||
done
|
||||
|
||||
# Note: Echoes 'end' because termination condition tested for at top of loop.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
until [ "$var1" = end ]
|
||||
# Tests condition at top of loop.
|
||||
until [ "$var1" = end ] # Tests condition here, at top of loop.
|
||||
do
|
||||
echo "Input variable #1 "
|
||||
echo "(end to exit)"
|
||||
|
|
|
@ -8,25 +8,21 @@ echo "Printing Numbers 1 through 20 (but not 3 and 11)."
|
|||
a=0
|
||||
|
||||
while [ $a -le "$LIMIT" ]
|
||||
|
||||
do
|
||||
a=$(($a+1))
|
||||
|
||||
if [ "$a" -eq 3 ] || [ "$a" -eq 11 ]
|
||||
# Excludes 3 and 11
|
||||
if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # Excludes 3 and 11
|
||||
then
|
||||
continue
|
||||
# Skip rest of this particular loop iteration.
|
||||
continue # Skip rest of this particular loop iteration.
|
||||
fi
|
||||
|
||||
echo -n "$a "
|
||||
done
|
||||
|
||||
# Exercise for reader:
|
||||
# Exercise for the reader:
|
||||
# Why does loop print up to 20?
|
||||
|
||||
echo
|
||||
echo
|
||||
echo; echo
|
||||
|
||||
echo Printing Numbers 1 through 20, but something happens after 2.
|
||||
|
||||
|
@ -42,16 +38,12 @@ do
|
|||
|
||||
if [ "$a" -gt 2 ]
|
||||
then
|
||||
break
|
||||
# Skip entire rest of loop.
|
||||
break # Skip entire rest of loop.
|
||||
fi
|
||||
|
||||
echo -n "$a "
|
||||
done
|
||||
|
||||
echo
|
||||
echo
|
||||
|
||||
echo
|
||||
echo; echo; echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo
|
||||
echo "Hit a key, then hit return."
|
||||
echo; echo "Hit a key, then hit return."
|
||||
read Keypress
|
||||
|
||||
case "$Keypress" in
|
||||
|
@ -9,7 +8,12 @@ case "$Keypress" in
|
|||
[A-Z] ) echo "Uppercase letter";;
|
||||
[0-9] ) echo "Digit";;
|
||||
* ) echo "Punctuation, whitespace, or other";;
|
||||
esac
|
||||
# Allows ranges of characters in [square brackets].
|
||||
esac # Allows ranges of characters in [square brackets].
|
||||
|
||||
# Exercise for the reader:
|
||||
# As the script stands, # it accepts a single keystroke, then terminates.
|
||||
# Change the script so it accepts continuous input,
|
||||
# reports on each keystroke, and terminates only when "X" is hit.
|
||||
# Hint: enclose everything in a "while" loop.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -9,9 +9,12 @@
|
|||
|
||||
sed -e /^$/d "$1"
|
||||
# The '-e' means an "editing" command follows (optional here).
|
||||
# '^' is beginning of line,
|
||||
# '$' is end,
|
||||
# and 'd' is delete.
|
||||
# Quoting the command-line arg permits special chars in the filename.
|
||||
# '^' is the beginning of line, '$' is the end.
|
||||
# This match lines with nothing between the beginning and the end,
|
||||
# blank lines.
|
||||
# The 'd' is the delete command.
|
||||
|
||||
# Quoting the command-line arg permits
|
||||
# whitespace and special characters in the filename.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Crude rolodex-type database
|
||||
# Crude address database
|
||||
|
||||
clear
|
||||
# Clear the screen.
|
||||
clear # Clear the screen.
|
||||
|
||||
echo " Contact List"
|
||||
echo " ------- ----"
|
||||
|
@ -50,13 +49,17 @@ case "$person" in
|
|||
|
||||
* )
|
||||
# Default option.
|
||||
# Empty input (hitting RETURN) fits here, too.
|
||||
echo
|
||||
echo "Not yet in database."
|
||||
;;
|
||||
|
||||
;;
|
||||
|
||||
esac
|
||||
|
||||
echo
|
||||
|
||||
# Exercise for the reader:
|
||||
# Change the script so it accepts continuous input,
|
||||
# instead of terminating after displaying just one address.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
PS3='Choose your favorite vegetable: '
|
||||
# Sets the prompt string.
|
||||
PS3='Choose your favorite vegetable: ' # Sets the prompt string.
|
||||
|
||||
echo
|
||||
|
||||
|
@ -11,8 +10,7 @@ do
|
|||
echo "Your favorite veggie is $vegetable."
|
||||
echo "Yuck!"
|
||||
echo
|
||||
break
|
||||
# if no 'break' here, keeps looping forever.
|
||||
break # if no 'break' here, keeps looping forever.
|
||||
done
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -3,9 +3,6 @@
|
|||
# 'getopts' processes command line arguments to script.
|
||||
# The arguments are parsed as "options" (flags) and associated arguments.
|
||||
|
||||
# Usage: scriptname -options
|
||||
# Note: dash (-) necessary
|
||||
|
||||
# Try invoking this script with
|
||||
# 'scriptname -mn'
|
||||
# 'scriptname -oq qOption' (qOption can be some arbitrary string.)
|
||||
|
@ -24,6 +21,9 @@ then
|
|||
echo "Usage: `basename $0` options (-mnopqrs)"
|
||||
exit $OPTERROR # Exit and explain usage, if no argument(s) given.
|
||||
fi
|
||||
# Usage: scriptname -options
|
||||
# Note: dash (-) necessary
|
||||
|
||||
|
||||
while getopts ":mnopq:rs" Option
|
||||
do
|
||||
|
@ -40,7 +40,6 @@ do
|
|||
done
|
||||
|
||||
shift $(($OPTIND - 1))
|
||||
# Decrements the argument pointer
|
||||
# so it points to next argument.
|
||||
# Decrements the argument pointer so it points to next argument.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -13,9 +13,8 @@ echo "Command-line argument #3 = $3"
|
|||
|
||||
echo
|
||||
|
||||
set `uname -a`
|
||||
# Sets the positional parameters to the output
|
||||
# of the command `uname -a`
|
||||
set `uname -a` # Sets the positional parameters to the output
|
||||
# of the command `uname -a`
|
||||
|
||||
echo "Positional parameters after set \`uname -a\` :"
|
||||
# $1, $2, $3, etc. reinitialized to result of `uname -a`
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
a=/home/heraclius/daily-journal.txt
|
||||
a=/home/bozo/daily-journal.txt
|
||||
|
||||
echo "Basename of /home/heraclius/daily-journal.txt = `basename $a`"
|
||||
echo "Dirname of /home/heraclius/daily-journal.txt = `dirname $a`"
|
||||
echo "Basename of /home/bozo/daily-journal.txt = `basename $a`"
|
||||
echo "Dirname of /home/bozo/daily-journal.txt = `dirname $a`"
|
||||
echo
|
||||
echo "My own home is `basename ~/`." # Also works with just ~.
|
||||
echo "The home of my home is `dirname ~/`." # Also works with just ~.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo -n "Enter the value of variable 'var1': "
|
||||
# -n option to echo suppresses newline
|
||||
# The -n option to echo suppresses newline.
|
||||
|
||||
read var1
|
||||
# Note no '$' in front of var1, since it is being set.
|
||||
|
@ -9,10 +9,9 @@ read var1
|
|||
echo "var1 = $var1"
|
||||
|
||||
|
||||
# Note that a single 'read' statement can set multiple variables.
|
||||
|
||||
echo
|
||||
|
||||
# A single 'read' statement can set multiple variables.
|
||||
echo -n "Enter the values of variables 'var2' and 'var3' (separated by a space or tab): "
|
||||
read var2 var3
|
||||
echo "var2 = $var2 var3 = $var3"
|
||||
|
|
|
@ -4,17 +4,18 @@ dir1=/usr/local
|
|||
dir2=/var/spool
|
||||
|
||||
pushd $dir1
|
||||
# Will do an automatic 'dirs'
|
||||
# (list directory stack to stdout).
|
||||
echo "Now in directory `pwd`."
|
||||
# Uses back-quoted 'pwd'.
|
||||
# Will do an automatic 'dirs' (list directory stack to stdout).
|
||||
echo "Now in directory `pwd`." # Uses back-quoted 'pwd'.
|
||||
|
||||
# Now, do some stuff in directory 'dir1'.
|
||||
pushd $dir2
|
||||
echo "Now in directory `pwd`."
|
||||
|
||||
# Now, do some stuff in directory 'dir2'.
|
||||
echo "The top entry in the DIRSTACK array is $DIRSTACK."
|
||||
popd
|
||||
echo "Now back in directory `pwd`."
|
||||
|
||||
# Now, do some more stuff in directory 'dir1'.
|
||||
popd
|
||||
echo "Now back in original working directory `pwd`."
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Load a data file.
|
||||
. data-file
|
||||
. data-file # Load a data file.
|
||||
# Same effect as "source data-file", but more portable.
|
||||
|
||||
# Note that the file "data-file", given below
|
||||
# must be present in working directory.
|
||||
# The file "data-file" must be present in current working directory,
|
||||
# since it is referred to by its 'basename'.
|
||||
|
||||
# Now, reference some data from that file.
|
||||
|
||||
|
|
|
@ -1,15 +1,26 @@
|
|||
#!/bin/bash
|
||||
|
||||
ROOT_UID=0 # Only users with $UID 0 have root privileges.
|
||||
E_NOTROOT=65
|
||||
E_NOPARAMS=66
|
||||
|
||||
if [ "$UID" -ne "$ROOT_UID" ]
|
||||
then
|
||||
echo "Must be root to run this script."
|
||||
# "Run along kid, it's past your bedtime."
|
||||
exit $E_NOTROOT
|
||||
fi
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` find-string"
|
||||
exit 65
|
||||
exit $E_NOPARAMS
|
||||
fi
|
||||
|
||||
|
||||
echo "Updating 'locate' database..."
|
||||
echo "This may take a while."
|
||||
updatedb /usr &
|
||||
# Must be run as root.
|
||||
updatedb /usr & # Must be run as root.
|
||||
|
||||
wait
|
||||
# Don't run the rest of the script until 'updatedb' finished.
|
||||
|
@ -17,7 +28,7 @@ wait
|
|||
|
||||
locate $1
|
||||
|
||||
# Lacking the wait command, in the worse case scenario,
|
||||
# Without the wait command, in the worse case scenario,
|
||||
# the script would exit while 'updatedb' was still running,
|
||||
# leaving it as an orphan process.
|
||||
|
||||
|
|
|
@ -8,8 +8,7 @@ ARGS=3
|
|||
E_BADARGS=65 # Wrong number of arguments passed to script.
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
# Test number of arguments to script
|
||||
# (always a good idea).
|
||||
# Test number of arguments to script (always a good idea).
|
||||
then
|
||||
echo "Usage: `basename $0` old-pattern new-pattern filename"
|
||||
exit $E_BADARGS
|
||||
|
@ -32,8 +31,6 @@ sed -e "s/$old_pattern/$new_pattern/g" $file_name
|
|||
# and /pattern/ invokes address matching.
|
||||
# The "g", or global flag causes substitution for *every*
|
||||
# occurence of $old_pattern on each line, not just the first.
|
||||
# Read the literature on 'sed' for a more
|
||||
# in-depth explanation.
|
||||
# Read the literature on 'sed' for a more in-depth explanation.
|
||||
|
||||
exit 0
|
||||
# Successful invocation of the script returns 0.
|
||||
exit 0 # Successful invocation of the script returns 0.
|
||||
|
|
|
@ -16,7 +16,7 @@ DEFAULTDIR=/opt
|
|||
if [ -z "$1" ]
|
||||
then
|
||||
IMAGE_DIRECTORY=$DEFAULTDIR
|
||||
# Default directory, if not specified on command line.
|
||||
# Default directory, if not specified on command line.
|
||||
else
|
||||
IMAGE_DIRECTORY=$1
|
||||
fi
|
||||
|
@ -24,7 +24,7 @@ fi
|
|||
ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE
|
||||
# The "l" option gives a "long" file listing.
|
||||
# The "R" option makes the listing recursive.
|
||||
# The "F" option marks the file types (directories suffixed by a /).
|
||||
# The "F" option marks the file types (directories get a trailing /).
|
||||
echo "Creating table of contents."
|
||||
|
||||
mkisofs -r -o $IMAGFILE $IMAGE_DIRECTORY
|
||||
|
|
|
@ -3,13 +3,16 @@
|
|||
# Generates a log file in current directory
|
||||
# from the tail end of /var/log/messages.
|
||||
|
||||
# Note: /var/log/messages must be readable by ordinary users
|
||||
# if invoked by same (#root chmod 755 /var/log/messages).
|
||||
# Note: /var/log/messages must be world readable
|
||||
# if this script invoked by an ordinary user.
|
||||
# #root chmod 644 /var/log/messages
|
||||
|
||||
LINES=5
|
||||
|
||||
( date; uname -a ) >>logfile
|
||||
# Time and machine name
|
||||
echo --------------------------------------------------------------------- >>logfile
|
||||
tail -5 /var/log/messages | xargs | fmt -s >>logfile
|
||||
tail -$LINES /var/log/messages | xargs | fmt -s >>logfile
|
||||
echo >>logfile
|
||||
echo >>logfile
|
||||
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
# Copy (verbose) all files in current directory
|
||||
# to directory specified on command line.
|
||||
|
||||
if [ -z "$1" ]
|
||||
# Exit if no argument given.
|
||||
if [ -z "$1" ] # Exit if no argument given.
|
||||
then
|
||||
echo "Usage: `basename $0` directory-to-copy-to"
|
||||
exit 65
|
||||
|
|
|
@ -6,7 +6,6 @@ echo $y # but linefeeds removed.
|
|||
y=`eval df` # Similar to y=`df`
|
||||
echo $y # but linefeeds removed.
|
||||
|
||||
# Note that LF's not preserved,
|
||||
# and this may make it easier to parse output.
|
||||
# Since LF's not preserved, it may make it easier to parse output.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
#!/bin/bash
|
||||
|
||||
y=`eval ps ax | sed -n '/ppp/p' | awk '{ print $1 }'`
|
||||
# Finding the process number of 'ppp'
|
||||
# Finding the process number of 'ppp'.
|
||||
|
||||
kill -9 $y
|
||||
# Killing it
|
||||
kill -9 $y # Killing it
|
||||
|
||||
# Above lines may be replaced by
|
||||
# kill -9 `ps ax | awk '/ppp/ { print $1 }'
|
||||
# kill -9 `ps ax | awk '/ppp/ { print $1 }'
|
||||
|
||||
|
||||
# Restore to previous state...
|
||||
|
||||
chmod 666 /dev/ttyS3
|
||||
# Doing a SIGKILL on ppp changes the permissions
|
||||
# on the serial port. Must be restored.
|
||||
# on the serial port. Restore them to previous state.
|
||||
|
||||
rm /var/lock/LCK..ttyS3
|
||||
# Remove the serial port lock file.
|
||||
rm /var/lock/LCK..ttyS3 # Remove the serial port lock file.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Demonstrating some of the uses of 'expr'
|
||||
# +++++++++++++++++++++++++++++++++++++++
|
||||
# =======================================
|
||||
|
||||
echo
|
||||
|
||||
|
@ -82,10 +82,12 @@ echo "Substring of \"$a\", starting at position 2, and 6 chars long is \"$b\"."
|
|||
b=`expr length $a`
|
||||
echo "Length of \"$a\" is $b."
|
||||
|
||||
|
||||
# 'match' operations similarly to 'grep'
|
||||
# uses Regular expressions
|
||||
b=`expr match "$a" '[0-9]*'`
|
||||
echo Number of digits at the beginning of \"$a\" is $b.
|
||||
b=`expr match "$a" '\([0-9]*\)'`
|
||||
b=`expr match "$a" '\([0-9]*\)'` # Note escaped parentheses.
|
||||
echo "The digits at the beginning of \"$a\" are \"$b\"."
|
||||
|
||||
echo
|
||||
|
|
|
@ -2,25 +2,26 @@
|
|||
|
||||
echo
|
||||
|
||||
let a=11
|
||||
# Same as 'a=11'
|
||||
let a=a+5
|
||||
# Equivalent to let "a = a + 5"
|
||||
# (double quotes makes it more readable)
|
||||
echo "a = $a"
|
||||
let "a <<= 3"
|
||||
# Equivalent of let "a = a << 3"
|
||||
echo "a left-shifted 3 places = $a"
|
||||
let a=11 # Same as 'a=11'
|
||||
let a=a+5 # Equivalent to let "a = a + 5"
|
||||
# (double quotes and spaces make it more readable)
|
||||
echo "11 + 5 = $a"
|
||||
|
||||
let "a /= 4"
|
||||
# Equivalent to let "a = a / 4"
|
||||
echo $a
|
||||
let "a -= 5"
|
||||
# Equivalent to let "a = a - 5"
|
||||
echo $a
|
||||
let "a = a * 10"
|
||||
echo $a
|
||||
let "a %= 8"
|
||||
echo $a
|
||||
let "a <<= 3" # Equivalent to let "a = a << 3"
|
||||
echo "\"\$a\" (=16) left-shifted 3 places = $a"
|
||||
|
||||
let "a /= 4" # Equivalent to let "a = a / 4"
|
||||
echo "128 / 4 = $a"
|
||||
|
||||
let "a -= 5" # Equivalent to let "a = a - 5"
|
||||
echo "32 - 5 = $a"
|
||||
|
||||
let "a = a * 10" # Equivalent to let "a = a * 10"
|
||||
echo "27 * 10 = $a"
|
||||
|
||||
let "a %= 8" # Equivalent to let "a = a % 8"
|
||||
echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#!/bin/bash
|
||||
|
||||
# printf demo
|
||||
|
||||
PI=3.14159265358979
|
||||
|
@ -11,17 +10,30 @@ echo
|
|||
|
||||
printf "Pi to 2 decimal places = %1.2f" $PI
|
||||
echo
|
||||
printf "Pi to 9 decimal places = %1.9f" $PI
|
||||
# Note correct round off.
|
||||
printf "Pi to 9 decimal places = %1.9f" $PI # It even rounds off correctly.
|
||||
|
||||
printf "\n"
|
||||
# Prints a line feed, equivalent to 'echo'.
|
||||
printf "\n" # Prints a line feed,
|
||||
# equivalent to 'echo'.
|
||||
|
||||
printf "Constant = \t%d\n" $DecimalConstant
|
||||
# Insert tab (\t)
|
||||
printf "Constant = \t%d\n" $DecimalConstant # Inserts tab (\t)
|
||||
|
||||
printf "%s %s \n" $Message1 $Message2
|
||||
|
||||
echo
|
||||
|
||||
# ==========================================#
|
||||
# Simulation of C function, 'sprintf'.
|
||||
# Loading a variable with a formatted string.
|
||||
|
||||
echo
|
||||
|
||||
Pi12=$(printf "%1.12f" $PI)
|
||||
echo "Pi to 12 decimal places = $Pi12"
|
||||
|
||||
Msg=`printf "%s %s \n" $Message1 $Message2`
|
||||
echo $Msg; echo $Msg
|
||||
|
||||
# As it happens, the 'sprintf' function can now be accessed
|
||||
# as a loadable module to Bash, but this is not portable.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -3,16 +3,18 @@
|
|||
# Copying a directory tree using cpio.
|
||||
|
||||
ARGS=2
|
||||
E_BADARGS=65
|
||||
|
||||
if [ $# -ne "$ARGS" ]
|
||||
then
|
||||
echo Usage: `basename $0` source destination
|
||||
exit 65
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
source=$1
|
||||
destination=$2
|
||||
|
||||
find "$source" -depth | cpio -admvp "$destination"
|
||||
# Read the man page to decipher these cpio options.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
|
||||
E_BADARGS=65
|
||||
|
||||
if [ -z "$1" ]
|
||||
# Standard check whether command line arg is present.
|
||||
if [ -z "$1" ] # Standard check for command line arg.
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_BADARGS
|
||||
|
@ -12,7 +11,7 @@ fi
|
|||
|
||||
tr a-z A-Z <"$1"
|
||||
|
||||
# Same effect as above, but using character set notation:
|
||||
# Same effect as above, but using POSIX character set notation:
|
||||
# tr '[:lower:]' '[:upper:]' <"$1"
|
||||
# Thanks, S.C.
|
||||
|
||||
|
|
|
@ -1,20 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo hello
|
||||
echo $?
|
||||
# exit status 0 returned
|
||||
# because command successful.
|
||||
echo $? # Exit status 0 returned because command successful.
|
||||
|
||||
lskdf
|
||||
# bad command
|
||||
echo $?
|
||||
# non-zero exit status returned.
|
||||
lskdf # Unrecognized command.
|
||||
echo $? # Non-zero exit status returned.
|
||||
|
||||
echo
|
||||
|
||||
exit 113
|
||||
# Will return 113 to shell.
|
||||
exit 113 # Will return 113 to shell.
|
||||
# To verify this, type "echo $?" after script terminates.
|
||||
|
||||
# By convention, an 'exit 0' shows success,
|
||||
# while a non-zero exit value indicates an error or anomalous condition.
|
||||
# By convention, an 'exit 0' indicates success,
|
||||
# while a non-zero exit value means an error or anomalous condition.
|
||||
|
|
|
@ -4,10 +4,9 @@
|
|||
|
||||
b=`ls /usr/local/bin`
|
||||
|
||||
# ...40 columns wide.
|
||||
echo $b | fmt -w 40
|
||||
echo $b | fmt -w 40 # ...40 columns wide.
|
||||
|
||||
# Could also have been done by
|
||||
# echo $b | fold - -s -w 40
|
||||
# echo $b | fold - -s -w 40
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
#!/bin/bash
|
||||
|
||||
#Using the 'date' command
|
||||
|
||||
# Needs a leading '+' to invoke formatting.
|
||||
# Exercising the 'date' command
|
||||
|
||||
echo "The number of days since the year's beginning is `date +%j`."
|
||||
# Needs a leading '+' to invoke formatting.
|
||||
# %j gives day of year.
|
||||
|
||||
|
||||
echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
|
||||
# %s yields number of seconds since "UNIX epoch" began,
|
||||
# but how is this useful?
|
||||
|
@ -22,3 +19,4 @@ echo $filename
|
|||
# Read the 'date' man page for more formatting options.
|
||||
|
||||
exit 0
|
||||
# Note that the "+%s" option to 'date' is GNU-specific.
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
#!/bin/bash
|
||||
|
||||
lines=35
|
||||
# Allow 35 lines for the header (very generous).
|
||||
lines=35 # Allow 35 lines for the header (very generous).
|
||||
|
||||
for File in *
|
||||
# Test all the files in the current working directory...
|
||||
for File in * # Test all the files in the current working directory...
|
||||
do
|
||||
search1=`head -$lines $File | grep begin | wc -w`
|
||||
search2=`tail -$lines $File | grep end | wc -w`
|
||||
# Uuencoded files have a "begin" near the beginning, and an "end" near the end.
|
||||
# Uuencoded files have a "begin" near the beginning,
|
||||
# and an "end" near the end.
|
||||
if [ "$search1" -gt 0 ]
|
||||
then
|
||||
if [ "$search2" -gt 0 ]
|
||||
|
@ -23,4 +22,7 @@ done
|
|||
# into thinking it is a uuencoded file,
|
||||
# because it contains both "begin" and "end".
|
||||
|
||||
# Exercise to the reader:
|
||||
# Modify this script to check for a newsgroup header.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -11,9 +11,8 @@ done
|
|||
|
||||
echo; echo
|
||||
|
||||
# Yes, "seq" may also take a replaceable parameter.
|
||||
|
||||
COUNT=80
|
||||
COUNT=80 # Yes, 'seq' may also take a replaceable parameter.
|
||||
|
||||
for a in `seq $COUNT` # or for a in $( seq $COUNT )
|
||||
do
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#!/bin/bash
|
||||
|
||||
exec echo "Exiting \"$0\"."
|
||||
# Exit from script.
|
||||
exec echo "Exiting \"$0\"." # Exit from script.
|
||||
|
||||
# The following lines never execute.
|
||||
|
||||
echo "This will never echo."
|
||||
|
||||
exit 0
|
||||
exit 0 # Will not exit here.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/bin/sh
|
||||
|
||||
# --> Comments added by the author of this document marked by "-->".
|
||||
# --> Comments added by the author of this document marked by "# -->".
|
||||
|
||||
# --> This is part of the 'rc' script package
|
||||
# --> by Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Some shell commands may precede the Perl script.
|
||||
# Shell commands may precede the Perl script.
|
||||
|
||||
perl -e 'print "This is an embedded Perl script\n"'
|
||||
# Like sed and awk, Perl also uses the "-e" option.
|
||||
# Like sed, Perl also uses the "-e" option.
|
||||
|
||||
# Some shell commands may follow.
|
||||
# Shell commands may follow.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -5,9 +5,8 @@
|
|||
for filename in *
|
||||
do
|
||||
badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p`
|
||||
# Files containing those nasties: + { ; " \ = ? ~ ( ) < > & * | $
|
||||
rm $badname 2>/dev/null
|
||||
# So error messages deep-sixed.
|
||||
# Files containing those nasties: + { ; " \ = ? ~ ( ) < > & * | $
|
||||
rm $badname 2>/dev/null # So error messages deep-sixed.
|
||||
done
|
||||
|
||||
# Now, take care of files containing all manner of whitespace.
|
||||
|
@ -24,4 +23,3 @@ exit 0
|
|||
find . -name '*[+{;"\\=?~()<>&*|$ ]*' -exec rm -f '{}' \;
|
||||
exit 0
|
||||
# (Thanks, S.C.)
|
||||
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Backs up all files in current directory
|
||||
# modified within last 24 hours
|
||||
# in a tarred and gzipped file.
|
||||
# Backs up all files in current directory modified within last 24 hours
|
||||
# in a "tarball" (tarred and gzipped file).
|
||||
|
||||
if [ $# = 0 ]
|
||||
NOARGS=0
|
||||
E_BADARGS=65
|
||||
|
||||
if [ $# = $NOARGS ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit 65
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
tar cvf - `find . -mtime -1 -type f -print` > $1.tar
|
||||
|
|
|
@ -2,13 +2,11 @@
|
|||
|
||||
funky ()
|
||||
{
|
||||
echo This is a funky function.
|
||||
echo Now exiting funky function.
|
||||
}
|
||||
echo "This is a funky function."
|
||||
echo "Now exiting funky function."
|
||||
} # Function declaration must precede call.
|
||||
|
||||
# Note: function must precede call.
|
||||
|
||||
# Now, call the function.
|
||||
# Now, call the function.
|
||||
|
||||
funky
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Let's check some of the system's environmental variables.
|
||||
# Check some of the system's environmental variables.
|
||||
# If, for example, $USER, the name of the person at the console, is not set,
|
||||
# the machine will not recognize you.
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
func2 () {
|
||||
if [ -z "$1" ]
|
||||
# Checks if parameter #1 is zero length.
|
||||
if [ -z "$1" ] # Checks if parameter #1 is zero length.
|
||||
then
|
||||
echo "-Parameter #1 is zero length.-" # Also if no parameter is passed.
|
||||
else
|
||||
|
@ -36,7 +35,7 @@ echo "One parameter passed."
|
|||
func2 first # Called with one param
|
||||
echo
|
||||
|
||||
echo "Two parameter passed."
|
||||
echo "Two parameters passed."
|
||||
func2 first second # Called with two params
|
||||
echo
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Arabic number to Roman numeral conversion
|
||||
# Range 0 - 200
|
||||
# Range: 0 - 200
|
||||
# It's crude, but it works.
|
||||
|
||||
# Extending the range and otherwise improving the script
|
||||
|
@ -9,23 +9,24 @@
|
|||
|
||||
# Usage: roman number-to-convert
|
||||
|
||||
ARG_ERR=65
|
||||
LIMIT=200
|
||||
E_ARG_ERR=65
|
||||
E_OUT_OF_RANGE=66
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` number-to-convert"
|
||||
exit $ARG_ERR
|
||||
exit $E_ARG_ERR
|
||||
fi
|
||||
|
||||
num=$1
|
||||
if [ "$num" -gt $LIMIT ]
|
||||
then
|
||||
echo "Out of range!"
|
||||
exit $OUT_OF_RANGE
|
||||
exit $E_OUT_OF_RANGE
|
||||
fi
|
||||
|
||||
to_roman ()
|
||||
to_roman () # Must declare function before first call to it.
|
||||
{
|
||||
number=$1
|
||||
factor=$2
|
||||
|
@ -39,10 +40,11 @@ do
|
|||
done
|
||||
|
||||
return $number
|
||||
# Exercise for the reader:
|
||||
# Explain how this function works.
|
||||
# Hint: division by successive subtraction.
|
||||
}
|
||||
|
||||
# Note: must declare function
|
||||
# before first call to it.
|
||||
|
||||
|
||||
to_roman $num 100 C
|
||||
num=$?
|
||||
|
|
|
@ -4,17 +4,15 @@ func ()
|
|||
{
|
||||
local a=23
|
||||
echo
|
||||
echo "a in function is $a"
|
||||
echo "a in function = $a"
|
||||
echo
|
||||
}
|
||||
|
||||
func
|
||||
|
||||
# Now, see if local 'a'
|
||||
# exists outside function.
|
||||
# Now, see if local 'a' exists outside function.
|
||||
|
||||
echo "a outside function is $a"
|
||||
echo "a outside function = $a" # Nope, 'a' not visible globally.
|
||||
echo
|
||||
# Nope, 'a' not visible globally.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -10,31 +10,33 @@
|
|||
|
||||
|
||||
MAX_ARG=5
|
||||
WRONG_ARGS=65
|
||||
RANGE_ERR=66
|
||||
E_WRONG_ARGS=65
|
||||
E_RANGE_ERR=66
|
||||
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` number"
|
||||
exit $WRONG_ARGS
|
||||
exit $E_WRONG_ARGS
|
||||
fi
|
||||
|
||||
if [ "$1" -gt $MAX_ARG ]
|
||||
then
|
||||
echo "Out of range (5 is maximum)."
|
||||
# Let's get real now...
|
||||
# If you want greater range than this, rewrite it in a real programming language.
|
||||
exit $RANGE_ERR
|
||||
# Let's get real now.
|
||||
# If you want greater range than this,
|
||||
# rewrite it in a real programming language.
|
||||
exit $E_RANGE_ERR
|
||||
fi
|
||||
|
||||
fact ()
|
||||
{
|
||||
local number=$1
|
||||
# Variable "number" must be declared as local otherwise this doesn't work.
|
||||
# Variable "number" must be declared as local,
|
||||
# otherwise this doesn't work.
|
||||
if [ "$number" -eq 0 ]
|
||||
then
|
||||
factorial=1
|
||||
factorial=1 # Factorial of 0 = 1.
|
||||
else
|
||||
let "decrnum = number - 1"
|
||||
fact $decrnum # Recursive function call.
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
# "and list"
|
||||
|
||||
if [ ! -z "$1" ] && echo "Argument #1 = $1" && [ ! -z "$2" ] && echo "Argument #2 = $2"
|
||||
then
|
||||
echo "At least 2 arguments to script."
|
||||
echo "At least 2 arguments passed to script."
|
||||
# All the chained commands return true.
|
||||
else
|
||||
echo "Less than 2 arguments to script."
|
||||
echo "Less than 2 arguments passed to script."
|
||||
# At least one of the chained commands returns false.
|
||||
fi
|
||||
# Note that "if [ ! -z $1 ]" works, but its supposed equivalent,
|
||||
|
@ -16,7 +15,7 @@ fi
|
|||
# It is best to always quote tested variables.
|
||||
|
||||
|
||||
# This accomplishes the same thing, coded using "pure" if/then statements.
|
||||
# This accomplishes the same thing, using "pure" if/then statements.
|
||||
if [ ! -z "$1" ]
|
||||
then
|
||||
echo "Argument #1 = $1"
|
||||
|
@ -24,9 +23,9 @@ fi
|
|||
if [ ! -z "$2" ]
|
||||
then
|
||||
echo "Argument #2 = $2"
|
||||
echo "At least 2 arguments to script."
|
||||
echo "At least 2 arguments passed to script."
|
||||
else
|
||||
echo "Less than 2 arguments to script."
|
||||
echo "Less than 2 arguments passed to script."
|
||||
fi
|
||||
# It's longer and less elegant than using an "and list".
|
||||
|
||||
|
|
|
@ -3,26 +3,27 @@
|
|||
# "Delete", not-so-cunning file deletion utility.
|
||||
# Usage: delete filename
|
||||
|
||||
E_BADARGS=65
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
file=nothing
|
||||
else
|
||||
file=$1
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
# Fetch file name (or "nothing") for deletion message.
|
||||
|
||||
|
||||
[ ! -f "$1" ] && echo "$1 not found. Can't delete a nonexistent file."
|
||||
file=$1 # Set filename.
|
||||
|
||||
[ ! -f "$1" ] && echo "File \"$1\" not found. \
|
||||
Cowardly refusing to delete a nonexistent file."
|
||||
# AND LIST, to give error message if file not present.
|
||||
# Note echo message continued on to a second line with an escape.
|
||||
|
||||
[ ! -f "$1" ] || ( rm -f $1; echo "$file deleted." )
|
||||
[ ! -f "$1" ] || (rm -f $1; echo "File \"$file\" deleted.")
|
||||
# OR LIST, to delete file if present.
|
||||
# ( command1 ; command2 ) is, in effect, an AND LIST variant.
|
||||
|
||||
# Note logic inversion above.
|
||||
# AND LIST executes on true, OR LIST on false.
|
||||
|
||||
[ ! -z "$1" ] || echo "Usage: `basename $0` filename"
|
||||
# OR LIST, to give error message if no command line arg (file name).
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -5,18 +5,18 @@ area[11]=23
|
|||
area[13]=37
|
||||
area[51]=UFOs
|
||||
|
||||
# Note that array members need not be consecutive
|
||||
# or contiguous.
|
||||
# Array members need not be consecutive or contiguous.
|
||||
|
||||
# Some members of the array can be left uninitialized.
|
||||
# Gaps in the array are o.k.
|
||||
|
||||
|
||||
echo -n "area[11] = "
|
||||
echo ${area[11]}
|
||||
echo ${area[11]} # {curly brackets} needed
|
||||
|
||||
echo -n "area[13] = "
|
||||
echo ${area[13]}
|
||||
# Note that {curly brackets} needed
|
||||
|
||||
echo "Contents of area[51] are ${area[51]}."
|
||||
|
||||
# Contents of uninitialized array variable print blank.
|
||||
|
@ -36,12 +36,9 @@ area[6]=`expr ${area[11]} + ${area[51]}`
|
|||
echo "area[6] = area[11] + area[51]"
|
||||
echo -n "area[6] = "
|
||||
echo ${area[6]}
|
||||
# This doesn't work because
|
||||
# adding an integer to a string is not permitted.
|
||||
# This fails because adding an integer to a string is not permitted.
|
||||
|
||||
echo
|
||||
echo
|
||||
echo
|
||||
echo; echo; echo
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Another array, "area2".
|
||||
|
@ -55,13 +52,10 @@ echo ${area2[0]}
|
|||
# Aha, zero-based indexing (first element of array is [0], not [1]).
|
||||
|
||||
echo -n "area2[1] = "
|
||||
echo ${area2[1]} # [1] is second element of array.
|
||||
echo ${area2[1]} # [1] is second element of array.
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
|
||||
echo
|
||||
echo
|
||||
echo
|
||||
echo; echo; echo
|
||||
|
||||
# -----------------------------------------------
|
||||
# Yet another array, "area3".
|
||||
|
@ -77,5 +71,4 @@ echo -n "area3[24] = "
|
|||
echo ${area3[24]}
|
||||
# -----------------------------------------------
|
||||
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
#!/bin/bash
|
||||
|
||||
declare -a colors
|
||||
# Permits declaring an array without specifying size.
|
||||
# Permits declaring an array without specifying its size.
|
||||
|
||||
echo "Enter your favorite colors (separated from each other by a space)."
|
||||
|
||||
read -a colors
|
||||
# Special option to 'read' command,
|
||||
# allowing it to assign elements in an array.
|
||||
# allowing assignment of elements in an array.
|
||||
|
||||
echo
|
||||
|
||||
element_count=${#colors[@]} # Special syntax to extract number of elements in array.
|
||||
element_count=${#colors[@]}
|
||||
# Special syntax to extract number of elements in array.
|
||||
# element_count=${#colors[*]} works also.
|
||||
#
|
||||
# The "@" variable allows word splitting within quotes
|
||||
# (extracts variables separated by whitespace).
|
||||
|
||||
index=0
|
||||
|
||||
# List all the elements in the array.
|
||||
while [ "$index" -lt "$element_count" ]
|
||||
do
|
||||
do # List all the elements in the array.
|
||||
echo ${colors[$index]}
|
||||
let "index = $index + 1"
|
||||
done
|
||||
|
@ -29,7 +30,8 @@ done
|
|||
#
|
||||
# Doing it with a "for" loop instead:
|
||||
# for i in "${colors[@]}"
|
||||
# do echo "$i"
|
||||
# do
|
||||
# echo "$i"
|
||||
# done
|
||||
# (Thanks, S.C.)
|
||||
|
||||
|
@ -39,7 +41,6 @@ echo
|
|||
echo ${colors[@]}
|
||||
# echo ${colors[*]} works also.
|
||||
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,26 +1,22 @@
|
|||
#!/bin/bash
|
||||
|
||||
# sieve.sh
|
||||
|
||||
# Sieve of Erastosthenes
|
||||
# Ancient algorithm for finding prime numbers.
|
||||
|
||||
# This runs a couple of orders of magnitude
|
||||
# slower than equivalent C program.
|
||||
# slower than the equivalent C program.
|
||||
|
||||
LOWER_LIMIT=1
|
||||
# Starting with 1.
|
||||
UPPER_LIMIT=1000
|
||||
# Up to 1000.
|
||||
# (You may set this higher...
|
||||
# if you have time on your hands.)
|
||||
LOWER_LIMIT=1 # Starting with 1.
|
||||
UPPER_LIMIT=1000 # Up to 1000.
|
||||
# (You may set this higher... if you have time on your hands.)
|
||||
|
||||
PRIME=1
|
||||
NON_PRIME=0
|
||||
|
||||
let SPLIT=UPPER_LIMIT/2
|
||||
# Optimization:
|
||||
# Need to test numbers only
|
||||
# halfway to upper limit.
|
||||
# Need to test numbers only halfway to upper limit.
|
||||
|
||||
|
||||
declare -a Primes
|
||||
|
@ -43,8 +39,7 @@ done
|
|||
|
||||
print_primes ()
|
||||
{
|
||||
# Print out the members of the Primes[] array
|
||||
# tagged as prime.
|
||||
# Print out the members of the Primes[] array tagged as prime.
|
||||
|
||||
i=$LOWER_LIMIT
|
||||
|
||||
|
@ -54,8 +49,7 @@ do
|
|||
if [ "${Primes[i]}" -eq "$PRIME" ]
|
||||
then
|
||||
printf "%8d" $i
|
||||
# 8 spaces per number
|
||||
# gives nice, even columns.
|
||||
# 8 spaces per number gives nice, even columns.
|
||||
fi
|
||||
|
||||
let "i += 1"
|
||||
|
@ -64,20 +58,17 @@ done
|
|||
|
||||
}
|
||||
|
||||
sift ()
|
||||
sift () # Sift out the non-primes.
|
||||
{
|
||||
# Sift out the non-primes.
|
||||
|
||||
let i=$LOWER_LIMIT+1
|
||||
# We know 1 is prime, so
|
||||
# let's start with 2.
|
||||
# We know 1 is prime, so let's start with 2.
|
||||
|
||||
until [ "$i" -gt "$UPPER_LIMIT" ]
|
||||
do
|
||||
|
||||
if [ "${Primes[i]}" -eq "$PRIME" ]
|
||||
# Don't bother sieving numbers
|
||||
# already sieved (tagged as non-prime).
|
||||
# Don't bother sieving numbers already sieved (tagged as non-prime).
|
||||
then
|
||||
|
||||
t=$i
|
||||
|
@ -86,8 +77,7 @@ then
|
|||
do
|
||||
let "t += $i "
|
||||
Primes[t]=$NON_PRIME
|
||||
# Tag as non-prime
|
||||
# all multiples.
|
||||
# Tag as non-prime all multiples.
|
||||
done
|
||||
|
||||
fi
|
||||
|
@ -103,9 +93,10 @@ done
|
|||
initialize
|
||||
sift
|
||||
print_primes
|
||||
echo
|
||||
# This is what they call structured programming.
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
||||
|
||||
|
@ -118,8 +109,8 @@ exit 0
|
|||
|
||||
# Must invoke with command-line argument (limit of primes).
|
||||
|
||||
UPPER_LIMIT=$1 # From command line.
|
||||
let SPLIT=UPPER_LIMIT/2 # Halfway to max number.
|
||||
UPPER_LIMIT=$1 # From command line.
|
||||
let SPLIT=UPPER_LIMIT/2 # Halfway to max number.
|
||||
|
||||
Primes=( '' $(seq $UPPER_LIMIT) )
|
||||
|
||||
|
@ -136,3 +127,5 @@ do
|
|||
fi
|
||||
done
|
||||
echo ${Primes[*]}
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Non-interactive use of 'vi' to edit a file.
|
||||
# (Will not work with 'vim', for some reason.)
|
||||
# Emulates 'sed'.
|
||||
|
||||
E_BADARGS=65
|
||||
|
@ -13,6 +14,8 @@ fi
|
|||
|
||||
TARGETFILE=$1
|
||||
|
||||
# Insert 2 lines in file, then save.
|
||||
#--------Begin here document-----------#
|
||||
vi $TARGETFILE <<x23LimitStringx23
|
||||
i
|
||||
This is line 1 of the example file.
|
||||
|
@ -20,6 +23,7 @@ This is line 2 of the example file.
|
|||
^[
|
||||
ZZ
|
||||
x23LimitStringx23
|
||||
#----------End here document-----------#
|
||||
|
||||
# Note that ^[ above is a literal escape
|
||||
# typed by Control-V Escape.
|
||||
|
|
|
@ -5,9 +5,9 @@ echo "var1 = $var1"
|
|||
|
||||
t=${var1#*-*}
|
||||
echo "var1 (with everything, up to and including first - stripped out) = $t"
|
||||
# t=${var1#*-} works just the same,
|
||||
# since # matches the shortest string,
|
||||
# and * matches everything preceding, including an empty string.
|
||||
# t=${var1#*-} works just the same,
|
||||
#+ since # matches the shortest string,
|
||||
#+ and * matches everything preceding, including an empty string.
|
||||
# (Thanks, S. C. for pointing this out.)
|
||||
|
||||
t=${var1##*-*}
|
||||
|
@ -19,23 +19,26 @@ echo "var1 (with everything from the last - on stripped out) = $t"
|
|||
|
||||
echo
|
||||
|
||||
# -------------------------------------------
|
||||
path_name=/home/bozo/ideas/thoughts.for.today
|
||||
# -------------------------------------------
|
||||
echo "path_name = $path_name"
|
||||
t=${path_name##/*/}
|
||||
echo "path_name, stripped of prefixes = $t"
|
||||
# Same effect as t=`basename $path_name` in this particular case.
|
||||
# t=${path_name%/}; t=${t##*/} is a more general solution, but still fails sometimes.
|
||||
# If $path_name ends with a newline, then `basename $path_name` will not work,
|
||||
# but the above expression will.
|
||||
# (Thanks Stephane Chazelas.)
|
||||
# t=${path_name%/}; t=${t##*/} is a more general solution,
|
||||
#+ but still fails sometimes.
|
||||
# If $path_name ends with a newline, then `basename $path_name` will not work,
|
||||
#+ but the above expression will.
|
||||
# (Thanks, S.C.)
|
||||
|
||||
t=${path_name%/*.*}
|
||||
# Same effect as t=`dirname $path_name`
|
||||
echo "path_name, stripped of suffixes = $t"
|
||||
# These will fail in some cases, such as "../", "/foo////", # "foo/", "/".
|
||||
# Removing suffixes, especially when the basename has no suffix,
|
||||
# but the dirname does, also complicates matters.
|
||||
# (Thanks, Stephane Chazelas.)
|
||||
# Removing suffixes, especially when the basename has no suffix,
|
||||
#+ but the dirname does, also complicates matters.
|
||||
# (Thanks, S.C.)
|
||||
|
||||
echo
|
||||
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#!/bin/bash
|
||||
|
||||
wall <<zzz23EndOfMessagezzz23
|
||||
Dees ees a message frrom Central Headquarters:
|
||||
Do not keel moose!
|
||||
# Other message text goes here.
|
||||
E-mail your noontime orders for pizza to the system administrator.
|
||||
(Add an extra dollar for anchovy or mushroom topping.)
|
||||
# Additional message text goes here.
|
||||
# Note: Comment lines printed by 'wall'.
|
||||
zzz23EndOfMessagezzz23
|
||||
|
||||
# Could have been done more efficiently by
|
||||
# wall <message-file
|
||||
# wall <message-file
|
||||
# However, saving a message template in a script saves work.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
# Same as previous example, but...
|
||||
|
||||
# The - option to a here document <<-
|
||||
# suppresses tabs in the body of the document,
|
||||
# but *not* spaces.
|
||||
# suppresses tabs in the body of the document, but *not* spaces.
|
||||
|
||||
cat <<-ENDOFMESSAGE
|
||||
This is line 1 of the message.
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
#!/bin/bash
|
||||
# Another 'cat' here document, using parameter substitution.
|
||||
# Another 'cat' here document, using parameter substitution.
|
||||
|
||||
# Try it with no command line parameters, ./scriptname
|
||||
# Try it with one command line parameter, ./scriptname Mortimer
|
||||
# Try it with one two-word quoted command line parameter, ./scriptname "Mortimer Jones"
|
||||
# Try it with one two-word quoted command line parameter,
|
||||
# ./scriptname "Mortimer Jones"
|
||||
|
||||
CMDLINEPARAM=1 # Expect at least command line parameter.
|
||||
CMDLINEPARAM=1 # Expect at least command line parameter.
|
||||
|
||||
if [ $# -ge $CMDLINEPARAM ]
|
||||
then
|
||||
NAME=$1 # If more than one command line param, then just take the first.
|
||||
NAME=$1 # If more than one command line param,
|
||||
# then just take the first.
|
||||
else
|
||||
NAME="John Doe" # Default, if no command line parameter.
|
||||
fi
|
||||
|
|
|
@ -1,35 +1,33 @@
|
|||
#!/bin/bash
|
||||
# upload.sh
|
||||
|
||||
# upload
|
||||
# upload file pair (filename.lsm, filename.tar.gz)
|
||||
# to incoming directory at Sunsite
|
||||
# Upload file pair (Filename.lsm, Filename.tar.gz)
|
||||
# to incoming directory at Sunsite (metalab.unc.edu).
|
||||
|
||||
E_ARGERROR=65
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit 65
|
||||
exit $E_ARGERROR
|
||||
fi
|
||||
|
||||
|
||||
Filename=`basename $1`
|
||||
# Strips pathname out of file name
|
||||
Filename=`basename $1` # Strips pathname out of file name.
|
||||
|
||||
Server="metalab.unc.edu"
|
||||
Directory="/incoming/Linux"
|
||||
# These need not be hard-coded into script,
|
||||
# may instead be changed to command line argument.
|
||||
# but may instead be changed to command line argument.
|
||||
|
||||
Password="your.e-mail.address"
|
||||
# Change above to suit.
|
||||
Password="your.e-mail.address" # Change above to suit.
|
||||
|
||||
ftp -n $Server <<End-Of-Session
|
||||
# -n option disables auto-logon
|
||||
|
||||
user anonymous "$Password"
|
||||
binary
|
||||
bell
|
||||
# Ring 'bell' after each file transfer
|
||||
bell # Ring 'bell' after each file transfer
|
||||
cd $Directory
|
||||
put "$Filename.lsm"
|
||||
put "$Filename.tar.gz"
|
||||
|
|
|
@ -3,26 +3,40 @@
|
|||
# Creating a swapfile.
|
||||
# This script must be run as root.
|
||||
|
||||
ROOT_UID=0 # Root has $UID 0.
|
||||
E_WRONG_USER=65 # Not root?
|
||||
|
||||
FILE=/swap
|
||||
BLOCKSIZE=1024
|
||||
PARAM_ERROR=73
|
||||
MINBLOCKS=40
|
||||
SUCCESS=0
|
||||
|
||||
|
||||
if [ -z "$1" ]
|
||||
if [ "$UID" -ne "$ROOT_UID" ]
|
||||
then
|
||||
echo "Usage: `basename $0` swapfile-size"
|
||||
# Must be at least 40 blocks.
|
||||
exit $PARAM_ERROR
|
||||
fi
|
||||
|
||||
dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$1
|
||||
echo; echo "You must be root to run this script."; echo
|
||||
exit $E_WRONG_USER
|
||||
fi
|
||||
|
||||
|
||||
echo "Creating swapfile of size $1 blocks (KB)."
|
||||
if [ -n "$1" ]
|
||||
then
|
||||
blocks=$1
|
||||
else
|
||||
blocks=$MINBLOCKS # Set to default of 40 blocks
|
||||
fi # if nothing specified on command line.
|
||||
|
||||
mkswap $FILE $1
|
||||
swapon $FILE
|
||||
if [ "$blocks" -lt $MINBLOCKS ]
|
||||
then
|
||||
blocks=$MINBLOCKS # Must be at least 40 blocks long.
|
||||
fi
|
||||
|
||||
echo "Swapfile activated."
|
||||
|
||||
echo "Creating swap file of size $blocks blocks (KB)."
|
||||
dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # Zero out file.
|
||||
|
||||
mkswap $FILE $blocks # Designate it a swap file.
|
||||
swapon $FILE # Activate swap file.
|
||||
|
||||
echo "Swap file created and activated."
|
||||
|
||||
exit $SUCCESS
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue