mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
2792cae462
commit
4a09b40a4e
File diff suppressed because it is too large
Load Diff
|
@ -1,19 +1,34 @@
|
|||
#!/bin/bash
|
||||
|
||||
#When is a variable "naked", i.e., lacking the '$' in front?
|
||||
echo
|
||||
|
||||
# When is a variable "naked", i.e., lacking the '$' in front?
|
||||
|
||||
# Assignment
|
||||
a=879
|
||||
echo $a
|
||||
echo "The value of \"a\" is $a"
|
||||
|
||||
# Assignment using 'let'
|
||||
let a=16+5
|
||||
echo $a
|
||||
echo "The value of \"a\" is now $a"
|
||||
|
||||
echo
|
||||
|
||||
# In a 'for' loop (really, a type of disguised assignment)
|
||||
echo -n "The values of \"a\" in the loop are "
|
||||
for a in 7 8 9 11
|
||||
do
|
||||
echo $a
|
||||
echo -n "$a "
|
||||
done
|
||||
|
||||
echo
|
||||
echo
|
||||
|
||||
# In a 'read' statement
|
||||
echo -n "Enter \"a\" "
|
||||
read a
|
||||
echo "The value of \"a\" is now $a"
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,9 +1,94 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Prints different random integer
|
||||
# at each invocation.
|
||||
# $RANDOM returns a different random integer at each invocation.
|
||||
# Nominal range: 0 - 32767 (signed integer).
|
||||
|
||||
MAXCOUNT=10
|
||||
count=1
|
||||
|
||||
echo
|
||||
echo "$MAXCOUNT random numbers:"
|
||||
echo "-----------------"
|
||||
while [ $count -le $MAXCOUNT ] # Generate 10 ($MAXCOUNT) random integers.
|
||||
do
|
||||
number=$RANDOM
|
||||
echo $number
|
||||
let "count += 1" # Increment count.
|
||||
done
|
||||
echo "-----------------"
|
||||
|
||||
# If you need a random int within a certain range, then use the 'modulo' operator.
|
||||
|
||||
RANGE=500
|
||||
|
||||
echo
|
||||
|
||||
number=$RANDOM
|
||||
let "number %= $RANGE"
|
||||
echo "Random number less than $RANGE --> $number"
|
||||
|
||||
echo
|
||||
|
||||
# If you need a random int greater than a lower bound,
|
||||
# then set up a test to discard all numbers below that.
|
||||
|
||||
FLOOR=200
|
||||
|
||||
number=0 #initialize
|
||||
while [ $number -le $FLOOR ]
|
||||
do
|
||||
number=$RANDOM
|
||||
done
|
||||
echo "Random number greater than $FLOOR --> $number"
|
||||
echo
|
||||
|
||||
|
||||
# May combine above two techniques to retrieve random number between two limits.
|
||||
number=0 #initialize
|
||||
while [ $number -le $FLOOR ]
|
||||
do
|
||||
number=$RANDOM
|
||||
let "number %= $RANGE"
|
||||
done
|
||||
echo "Random number between $FLOOR and $RANGE --> $number"
|
||||
echo
|
||||
|
||||
|
||||
# May generate binary choice, that is, "true" or "false" value.
|
||||
BINARY=2
|
||||
number=$RANDOM
|
||||
let "number %= $BINARY"
|
||||
if [ $number -eq 1 ]
|
||||
then
|
||||
echo "TRUE"
|
||||
else
|
||||
echo "FALSE"
|
||||
fi
|
||||
|
||||
echo
|
||||
|
||||
|
||||
# May generate toss of the dice.
|
||||
SPOTS=7
|
||||
DICE=2
|
||||
die1=0
|
||||
die2=0
|
||||
|
||||
# Tosses each die separately, and so gives correct odds.
|
||||
|
||||
while [ $die1 -eq 0 ] #Can't have a zero come up.
|
||||
do
|
||||
let "die1 = $RANDOM % $SPOTS"
|
||||
done
|
||||
|
||||
while [ $die2 -eq 0 ]
|
||||
do
|
||||
let "die2 = $RANDOM % $SPOTS"
|
||||
done
|
||||
|
||||
let "throw = $die1 + $die2"
|
||||
echo "Throw of the dice = $throw"
|
||||
echo
|
||||
|
||||
a=$RANDOM
|
||||
echo $a
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -4,9 +4,18 @@ echo -n "Enter the value of variable 'var1': "
|
|||
# -n option to echo suppresses newline
|
||||
|
||||
read var1
|
||||
# Note no '$' in front of var1,
|
||||
# since it is being set.
|
||||
# Note no '$' in front of var1, since it is being set.
|
||||
|
||||
echo "var1 = $var1"
|
||||
|
||||
|
||||
# Note that a single 'read' statement can set multiple variables.
|
||||
|
||||
echo
|
||||
|
||||
echo -n "Enter the values of variables 'var2' and 'var3' (separated by a space or tab): "
|
||||
read var2 var3
|
||||
echo "var2 = $var2 var3 = $var3"
|
||||
# If you input only one value, the other variable(s) will remain unset (null).
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -12,12 +12,13 @@ updatedb /usr &
|
|||
# Must be run as root.
|
||||
|
||||
wait
|
||||
# Don't run the rest of the script
|
||||
# until 'updatedb' finished.
|
||||
# In this case, you want the the database updated
|
||||
# before looking up the file name.
|
||||
# Don't run the rest of the script until 'updatedb' finished.
|
||||
# You want the the database updated before looking up the file name.
|
||||
|
||||
locate $1
|
||||
|
||||
# Lacking the wait command, in the worse case scenario,
|
||||
# the script would exit while 'updatedb' was still running,
|
||||
# leaving it as an orphan process.
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -16,7 +16,10 @@ else
|
|||
IMAGE_DIRECTORY=$1
|
||||
fi
|
||||
|
||||
ls -lR $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/contents
|
||||
ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/contents
|
||||
# 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 /).
|
||||
echo "Creating table of contents."
|
||||
|
||||
mkisofs -r -o cdimage.iso $IMAGE_DIRECTORY
|
||||
|
|
|
@ -60,6 +60,8 @@ fi
|
|||
echo
|
||||
echo
|
||||
|
||||
|
||||
|
||||
# String Operators
|
||||
|
||||
echo String Operators
|
||||
|
@ -67,15 +69,19 @@ echo
|
|||
|
||||
a=1234zipper43231
|
||||
echo The string being operated upon is $a.
|
||||
|
||||
# index: position of substring
|
||||
b=`expr index $a 23`
|
||||
echo Numerical position of first 23 in $a is $b.
|
||||
|
||||
# substr: print substring, starting position & length specified
|
||||
b=`expr substr $a 2 6`
|
||||
echo Substring of $a, starting at position 2 and 6 chars long is $b.
|
||||
|
||||
# length: length of string
|
||||
b=`expr length $a`
|
||||
echo Length of $a is $b.
|
||||
|
||||
# 'match' operations similarly to 'grep'
|
||||
b=`expr match $a [0-9]*`
|
||||
echo Number of digits at the beginning of $a is $b.
|
||||
|
|
|
@ -17,4 +17,7 @@ exit 143
|
|||
# To verify this, type $? after script terminates.
|
||||
|
||||
# By convention, an 'exit 0' shows success,
|
||||
# while a non-zero exit value indicates an error.
|
||||
# while a non-zero exit value indicates an error or anomalous condition.
|
||||
|
||||
# It is also appropriate for the script to use the exit status
|
||||
# to communicate with other processes, as when in a pipe with other scripts.
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
# --> This is part of the 'rc' script package
|
||||
# --> by Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
|
||||
|
||||
# --> This particular script seems to be Red Hat specific
|
||||
# --> (may not be present in other distributions).
|
||||
|
||||
# Bring down all unneeded services that are still running (there shouldn't
|
||||
# be any, so this is just a sanity check)
|
||||
|
|
|
@ -1,9 +1,43 @@
|
|||
#!/bin/bash
|
||||
|
||||
: ${HOSTNAME?} {USER?} {MAIL?}
|
||||
echo $HOSTNAME
|
||||
echo $USER
|
||||
echo $MAIL
|
||||
echo Critical env. variables set.
|
||||
# Let's 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.
|
||||
|
||||
: ${HOSTNAME?} ${USER?} ${HOME} ${MAIL?}
|
||||
echo
|
||||
echo "Name of the machine is $HOSTNAME."
|
||||
echo "You are $USER."
|
||||
echo "Your home directory is $HOME."
|
||||
echo "Your mail INBOX is located in $MAIL."
|
||||
echo
|
||||
echo "If you are reading this message,"
|
||||
echo "critical environmental variables have been set."
|
||||
echo
|
||||
echo
|
||||
|
||||
# The ':' operator seems fairly error tolerant.
|
||||
# This script works even if the '$' omitted in front of
|
||||
# {HOSTNAME}, {USER?}, {HOME?}, and {MAIL?}. Why?
|
||||
|
||||
# ------------------------------------------------------
|
||||
|
||||
# The ${variablename?} construction can also check
|
||||
# for variables set within the script.
|
||||
|
||||
ThisVariable=Value-of-ThisVariable
|
||||
# Note, by the way, that string variables may be set
|
||||
# to characters disallowed in their names.
|
||||
: ${ThisVariable?}
|
||||
echo "Value of ThisVariable is $ThisVariable".
|
||||
echo
|
||||
echo
|
||||
|
||||
# If ZZXy23AB has not been set...
|
||||
: ${ZZXy23AB?}
|
||||
# This will give you an error message and terminate.
|
||||
|
||||
echo "You will not see this message."
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
#!/bin/bash
|
||||
|
||||
# factorial
|
||||
# ---------
|
||||
|
||||
|
||||
# Does bash permit recursion?
|
||||
# Well, yes, but...
|
||||
# You gotta have rocks in your head to try it.
|
||||
|
||||
# Name this script "factorial".
|
||||
|
||||
MAX_ARG=5
|
||||
WRONG_ARGS=1
|
||||
|
@ -21,22 +24,20 @@ if [ $1 -gt $MAX_ARG ]
|
|||
then
|
||||
echo "Out of range (5 is maximum)."
|
||||
# Let's get real now...
|
||||
# If you want greater range, rewrite this
|
||||
# in a real programming language.
|
||||
# If you want greater range than this, rewrite it in a real programming language.
|
||||
exit $RANGE_ERR
|
||||
fi
|
||||
|
||||
fact ()
|
||||
{
|
||||
local number=$1
|
||||
# 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
|
||||
else
|
||||
let "decrnum = number - 1"
|
||||
fact $decrnum
|
||||
fact $decrnum # Recursive function call.
|
||||
let "factorial = $number * $?"
|
||||
fi
|
||||
|
||||
|
|
|
@ -11,15 +11,25 @@ read -a colors
|
|||
|
||||
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.
|
||||
index=0
|
||||
|
||||
# List all the elements in the array.
|
||||
while [ $index -lt $element_count ]
|
||||
do
|
||||
echo ${colors[$index]}
|
||||
let "index = $index + 1"
|
||||
done
|
||||
# Each array element listed on a separate line.
|
||||
# If this is not desired, use echo -n "${colors[$index]} "
|
||||
|
||||
echo
|
||||
|
||||
# Again, list all the elements in the array, but using a more elegant method.
|
||||
echo ${colors[@]}
|
||||
# echo ${colors[*]} works also.
|
||||
|
||||
|
||||
echo
|
||||
|
||||
|
|
|
@ -29,8 +29,12 @@ echo "$path_name, with first 11 chars stripped off, length 5 = $t"
|
|||
echo
|
||||
|
||||
t=${path_name/bozo/clown}
|
||||
echo "$path_name with bozo replaced = $t"
|
||||
echo "$path_name with \"bozo\" replaced by \"clown\" = $t"
|
||||
t=${path_name/today/}
|
||||
echo "$path_name with \"today\" deleted = $t"
|
||||
t=${path_name//o/O}
|
||||
echo "$path_name with all o's capitalized = $t"
|
||||
t=${path_name//o/}
|
||||
echo "$path_name with all o's deleted = $t"
|
||||
|
||||
exit 0
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
trap 'echo Variable Listing --- a = $a b = $b' EXIT
|
||||
# EXIT is the name of the signal generated
|
||||
# upon exit from a script.
|
||||
# EXIT is the name of the signal generated upon exit from a script.
|
||||
|
||||
a=39
|
||||
|
||||
b=36
|
||||
|
||||
exit 0
|
||||
# Note that commenting out the 'exit' command
|
||||
# does not make a difference.
|
||||
# Note that commenting out the 'exit' command makes no difference,
|
||||
# since the script exits anyhow after running out of commands.
|
||||
|
|
|
@ -12,6 +12,7 @@ echo "a = $a"
|
|||
|
||||
# Indirect reference.
|
||||
echo "Now a = ${!a}"
|
||||
# The ${!variable} notation is greatly superior to the old "eval var1=\$$var2"
|
||||
|
||||
echo
|
||||
|
||||
|
|
|
@ -1,21 +1,40 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Variables: assignment and substitution
|
||||
|
||||
a=37.5
|
||||
hello=$a
|
||||
# No space permitted on either side of = sign.
|
||||
# No space permitted on either side of = sign when initializing variables.
|
||||
|
||||
echo hello
|
||||
# Not a reference.
|
||||
|
||||
echo $hello
|
||||
echo ${hello} #Identical as above.
|
||||
echo ${hello} #Identical to above.
|
||||
|
||||
echo "$hello"
|
||||
echo "${hello}"
|
||||
|
||||
echo '$hello'
|
||||
# Variable referencing disabled by single quotes.
|
||||
# Variable referencing disabled by single quotes,
|
||||
# because $ interpreted literally.
|
||||
|
||||
# Notice the effect of different
|
||||
# types of quoting.
|
||||
# Notice the effect of different types of quoting.
|
||||
|
||||
# ------------------------------------------------
|
||||
|
||||
echo; echo
|
||||
|
||||
numbers="one two three"
|
||||
other_numbers="1 2 3"
|
||||
# If whitespace within variables, then quotes necessary.
|
||||
echo "numbers = $numbers"
|
||||
echo "other_numbers = $other_numbers"
|
||||
echo
|
||||
|
||||
echo "uninitialized variable = $uninitialized_variable"
|
||||
# Uninitialized variable has null value (no value at all).
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
|
Loading…
Reference in New Issue