mirror of https://github.com/tLDP/LDP
added
This commit is contained in:
parent
98e2eb10fd
commit
ed4a7f6e36
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,43 @@
|
|||
#!/bin/bash
|
||||
# assert.sh
|
||||
|
||||
assert () # If condition false,
|
||||
{ #+ exit from script with error message.
|
||||
E_PARAM_ERR=98
|
||||
E_ASSERT_FAILED=99
|
||||
|
||||
|
||||
if [ -z "$2" ] # Not enough parameters passed.
|
||||
then
|
||||
return $E_PARAM_ERR # No damage done.
|
||||
fi
|
||||
|
||||
lineno=$2
|
||||
|
||||
if [ ! $1 ]
|
||||
then
|
||||
echo "Assertion failed: \"$1\""
|
||||
echo "File $0, line $lineno"
|
||||
exit $E_ASSERT_FAILED
|
||||
# else
|
||||
# return
|
||||
# and continue executing script.
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
a=5
|
||||
b=4
|
||||
condition="$a -lt $b" # Error message and exit from script.
|
||||
# Try setting "condition" to something else,
|
||||
#+ and see what happens.
|
||||
|
||||
assert "$condition" $LINENO
|
||||
# The remainder of the script executes only if the "assert" does not fail.
|
||||
|
||||
|
||||
# Some commands.
|
||||
# ...
|
||||
# Some more commands.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/bash
|
||||
# bashandperl.sh
|
||||
|
||||
echo "Greetings from the Bash part of the script."
|
||||
# More Bash commands may follow here.
|
||||
|
||||
exit 0
|
||||
# End of Bash part of the script.
|
||||
|
||||
# =======================================================
|
||||
|
||||
#!/usr/bin/perl
|
||||
# This part of the script must be invoked with -x option.
|
||||
|
||||
print "Greetings from the Perl part of the script.\n";
|
||||
# More Perl commands may follow here.
|
||||
|
||||
# End of Perl part of the script.
|
|
@ -0,0 +1,40 @@
|
|||
#!/bin/bash
|
||||
# idelete.sh: Deleting a file by its inode number.
|
||||
|
||||
# This is useful when a filename starts with an illegal character,
|
||||
#+ such as ? or -.
|
||||
|
||||
ARGCOUNT=1 # Filename arg must be passed to script.
|
||||
E_WRONGARGS=70
|
||||
E_FILE_NOT_EXIST=71
|
||||
E_CHANGED_MIND=72
|
||||
|
||||
if [ $# -ne "$ARGCOUNT" ]
|
||||
then
|
||||
echo "Usage: `basename $0` filename"
|
||||
exit $E_WRONGARGS
|
||||
fi
|
||||
|
||||
if [ ! -e "$1" ]
|
||||
then
|
||||
echo "File \""$1"\" does not exist."
|
||||
exit $E_FILE_NOT_EXIST
|
||||
fi
|
||||
|
||||
inum=`ls -i | grep "$1" | awk '{print $1}'`
|
||||
# inum = inode (index node) number of file
|
||||
# Every file has an inode, a record that hold its physical address info.
|
||||
|
||||
echo; echo -n "Are you absolutely sure you want to delete \"$1\" (y/n)? "
|
||||
read answer
|
||||
case "$answer" in
|
||||
[nN]) echo "Changed your mind, huh?"
|
||||
exit $E_CHANGED_MIND
|
||||
;;
|
||||
*) echo "Deleting file \"$1\".";;
|
||||
esac
|
||||
|
||||
find . -inum $inum -exec rm {} \;
|
||||
echo "File "\"$1"\" deleted!"
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,28 @@
|
|||
#!/bin/bash
|
||||
# int-or-string.sh
|
||||
# Integer or string?
|
||||
|
||||
a=2334 # Integer.
|
||||
let "a += 1"
|
||||
echo "a = $a " # Integer, still.
|
||||
echo
|
||||
|
||||
b=${a/23/BB} # Transform into a string.
|
||||
echo "b = $b" # BB35
|
||||
declare -i b # Declaring it an integer doesn't help.
|
||||
echo "b = $b" # BB35, still.
|
||||
|
||||
let "b += 1" # BB35 + 1 =
|
||||
echo "b = $b" # 1
|
||||
echo
|
||||
|
||||
c=BB34
|
||||
echo "c = $c" # BB34
|
||||
d=${c/BB/23} # Transform into an integer.
|
||||
echo "d = $d" # 2334
|
||||
let "d += 1" # 2334 + 1 =
|
||||
echo "d = $d" # 2335
|
||||
|
||||
# Variables in Bash are essentially untyped.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,14 @@
|
|||
#!/bin/bash
|
||||
# m4.sh: Using the m4 macro processor
|
||||
|
||||
# Strings
|
||||
string=abcdA01
|
||||
echo "len($string)" | m4 # 7
|
||||
echo "substr($string,4)" | m4 # A01
|
||||
echo "regexp($string,[0-1][0-1],\&Z)" | m4 # 01Z
|
||||
|
||||
# Arithmetic
|
||||
echo "incr(22)" | m4 # 23
|
||||
echo "eval(99 / 3)" | m4 # 33
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,37 @@
|
|||
#!/bin/bash
|
||||
# match-string.sh: simple string matching
|
||||
|
||||
match_string ()
|
||||
{
|
||||
MATCH=0
|
||||
NOMATCH=90
|
||||
PARAMS=2 # Function requires 2 arguments.
|
||||
BAD_PARAMS=91
|
||||
|
||||
[ $# -eq $PARAMS ] || return $BAD_PARAMS
|
||||
|
||||
case "$1" in
|
||||
"$2") return $MATCH;;
|
||||
* ) return $NOMATCH;;
|
||||
esac
|
||||
|
||||
}
|
||||
|
||||
|
||||
a=one
|
||||
b=two
|
||||
c=three
|
||||
d=two
|
||||
|
||||
|
||||
match_string $a # wrong number of parameters
|
||||
echo $? # 91
|
||||
|
||||
match_string $a $b # no match
|
||||
echo $? # 90
|
||||
|
||||
match_string $b $d # match
|
||||
echo $? # 0
|
||||
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,51 @@
|
|||
#!/bin/bash
|
||||
# ramdisk.sh
|
||||
|
||||
# A "ramdisk" is a segment of system RAM memory
|
||||
#+ that acts as if it were a filesystem.
|
||||
# Its advantage is very fast access (read/write time).
|
||||
# Disadvantages: volatility, loss of data on reboot or powerdown.
|
||||
# less RAM available to system.
|
||||
#
|
||||
# What good is a ramdisk?
|
||||
# Keeping a large dataset, such as a table or dictionary on ramdisk
|
||||
#+ speeds up data lookup, since memory access is much faster than disk access.
|
||||
|
||||
|
||||
E_NON_ROOT_USER=70 # Must run as root.
|
||||
ROOTUSER_NAME=root
|
||||
|
||||
MOUNTPT=/mnt/ramdisk
|
||||
SIZE=2000 # 2K blocks (change as appropriate)
|
||||
BLOCKSIZE=1024 # 1K (1024 byte) block size
|
||||
DEVICE=/dev/ram0 # First ram device
|
||||
|
||||
username=`id -nu`
|
||||
if [ "$username" != "$ROOTUSER_NAME" ]
|
||||
then
|
||||
echo "Must be root to run \"`basename $0`\"."
|
||||
exit $E_NON_ROOT_USER
|
||||
fi
|
||||
|
||||
if [ ! -d "$MOUNTPT" ] # Test whether mount point already there,
|
||||
then #+ so no error if this script is run
|
||||
mkdir $MOUNTPT #+ multiple times.
|
||||
fi
|
||||
|
||||
dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM device.
|
||||
mke2fs $DEVICE # Create an ext2 filesystem on it.
|
||||
mount $DEVICE $MOUNTPT # Mount it.
|
||||
chmod 777 $MOUNTPT # So ordinary user can access ramdisk.
|
||||
# However, must be root to unmount it.
|
||||
|
||||
echo "\"$MOUNTPT\" now available for use."
|
||||
# The ramdisk is now accessible for storing files, even by an ordinary user.
|
||||
|
||||
# Caution, the ramdisk is volatile, and its contents will disappear
|
||||
#+ on reboot or power loss.
|
||||
# Copy anything you want saved to a regular directory.
|
||||
|
||||
# After reboot, run this script again to set up ramdisk.
|
||||
# Remounting /mnt/ramdisk without the other steps will not work.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,24 @@
|
|||
#!/bin/bash
|
||||
# recurse.sh
|
||||
|
||||
# Can a script recursively call itself?
|
||||
# Yes, but this is of little or no practical use
|
||||
#+ except perhaps as a "proof of concept".
|
||||
|
||||
RANGE=10
|
||||
MAXVAL=9
|
||||
|
||||
i=$RANDOM
|
||||
let "i %= $RANGE" # Generate a random number between 0 and $MAXVAL.
|
||||
|
||||
if [ "$i" -lt "$MAXVAL" ]
|
||||
then
|
||||
echo "i = $i"
|
||||
./$0 # Script recursively spawns a new instance of itself.
|
||||
fi # Each child script does the same, until
|
||||
#+ a generated $i equals $MAXVAL.
|
||||
|
||||
# Using a "while" loop instead of an "if/then" test causes problems.
|
||||
# Exercise for the reader: Explain why.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,27 @@
|
|||
#!/bin/bash
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
Filename=names.data # Default, if no filename specified.
|
||||
else
|
||||
Filename=$1
|
||||
fi
|
||||
|
||||
Savefile=$Filename.new # Filename to save results in.
|
||||
FinalName=Jonah # Name to terminate "read" on.
|
||||
|
||||
line_count=`wc $Filename | awk '{ print $1 }'` # Number of lines in target file.
|
||||
|
||||
|
||||
for name in `seq $line_count`
|
||||
do
|
||||
read name
|
||||
echo "$name"
|
||||
if [ "$name" = "$FinalName" ]
|
||||
then
|
||||
break
|
||||
fi
|
||||
done < "$Filename" > "$Savefile" # Redirects stdin to file $Filename,
|
||||
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ and saves it to backup file.
|
||||
|
||||
exit 0
|
|
@ -0,0 +1,22 @@
|
|||
#!/bin/bash
|
||||
# symlinks.sh: Lists symbolic links in a directory.
|
||||
|
||||
ARGS=1 # Expect one command-line argument.
|
||||
OUTFILE=symlinks.list # save file
|
||||
|
||||
if [ $# -ne "$ARGS" ] # If not 1 arg...
|
||||
then
|
||||
directory=`pwd` # current working directory
|
||||
else
|
||||
directory=$1
|
||||
fi
|
||||
|
||||
echo "symbolic links in directory \"$directory\""
|
||||
|
||||
for file in $( find $directory -type l ) # -type l = symbolic links
|
||||
do
|
||||
echo "$file"
|
||||
done | sort > "$OUTFILE" # stdout of loop
|
||||
# ^^^^^^^^^^^^ redirected to save file.
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue