
257
Appendix
APPENDIX
bash Script Samples
This appendix contains several scripts that can be useful to you in your daily work,
as well as serving as models for writing other scripts. You can download the scripts
from http://www.centralsoft.org.
Adding Users
If you have a lot of turnover (such as in a university, where new students enter in
bunches once or several times a year), this script can help you add them to your sys-
tem quickly. It reads a file listing information about each user and invokes useradd
with the proper arguments (see the section “User Management” in Chapter 8 for
details about useradd and its variants):
#!/bin/bash
expiredate=2009-02-18
if [[ -z "$1" ]] ; then
echo ""
echo "Please give exactly one file name."
echo "The file will have one user per line."
echo "Each line will have:"
echo " username"
echo " group"
echo " personal real name"
echo ""
echo "Sample line:
echo "alfredo marketing Alfredo de Darc"
exit 1
fi
cat "$1" | while read username groupname realname
do
# Skip blank lines.
if [[ -z $username || -z $groupname || -z $realname ]]; then
continue
fi