- •Table of Contents
- •Chapter 1. Why Shell Programming?
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •Chapter 8. Operations and Related Topics
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.2.1. Manipulating strings using awk
- •9.2.2. Further Discussion
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •11.1. Job Control Commands
- •Chapter 12. External Filters, Programs and Commands
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •23.2.1. Local variables make recursion possible.
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •33.1. Unofficial Shell Scripting Stylesheet
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Portability Issues
- •34.8. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •Chapter 36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.3.1. Hardware
- •36.3.2. Software and Printware
- •36.4. Credits
- •Bibliography
- •Appendix A. Contributed Scripts
- •Appendix C. Exit Codes With Special Meanings
- •Appendix D. A Detailed Introduction to I/O and I/O Redirection
- •Appendix E. Localization
- •Appendix F. History Commands
- •Appendix G. A Sample .bashrc File
- •Appendix H. Converting DOS Batch Files to Shell Scripts
- •Appendix I. Exercises
- •Appendix J. Copyright
Advanced Bash−Scripting Guide
#get a list of processes (the first sed evaluation
#takes care of swapped out processes, the second
#takes care of getting the basename of the process) COMPREPLY=( $( /usr/bin/ps −u $USER −o comm | \
sed −e '1,1d' −e 's#[]\[]##g' −e 's#^.*/##'| \ awk '{if ($0 ~ /^'$cur'/) print $0}' ))
return 0
}
complete −F _killall killall killps
#Local Variables:
#mode:shell−script
#sh−shell:bash
#End:
Appendix H. Converting DOS Batch Files to Shell Scripts
Quite a number of programmers learned scripting on a PC running DOS. Even the crippled DOS batch file language allowed writing some fairly powerful scripts and applications, though they often required extensive kludges and workarounds. Occasionally, the need still arises to convert an old DOS batch file to a UNIX shell script. This is generally not difficult, as DOS batch file operators are only a limited subset of the equivalent shell script ones.
Table H−1. Batch file keywords / variables / operators, and their shell equivalents
Batch File Operator |
Shell Script Equivalent |
Meaning |
|
|
|
% |
$ |
command−line parameter prefix |
|
|
|
/ |
− |
command option flag |
|
|
|
\ |
/ |
directory path separator |
|
|
|
== |
= |
(equal−to) string comparison test |
|
|
|
!==! |
!= |
(not equal−to) string comparison |
|
|
test |
|
|
|
| |
| |
pipe |
|
|
|
@ |
set +v |
do not echo current command |
|
|
|
* |
* |
filename "wild card" |
|
|
|
> |
> |
file redirection (overwrite) |
|
|
|
>> |
>> |
file redirection (append) |
|
|
|
< |
< |
redirect stdin |
|
|
|
%VAR% |
$VAR |
environmental variable |
|
|
|
REM |
# |
comment |
|
|
|
NOT |
! |
negate following test |
|
|
|
NUL |
/dev/null |
|
|
|
|
Appendix H. Converting DOS Batch Files to Shell Scripts |
355 |
Advanced Bash−Scripting Guide
|
|
|
"black hole" for burying command |
|
|
|
|
output |
|
|
|
|
|
|
ECHO |
echo |
|
echo (many more option in Bash) |
|
|
|
|
|
|
ECHO. |
echo |
|
echo blank line |
|
|
|
|
|
|
ECHO OFF |
set +v |
|
do not echo command(s) following |
|
|
|
|
|
|
FOR %%VAR IN (LIST) DO |
for var in [list]; do |
|
"for" loop |
|
|
|
|
|
|
:LABEL |
none (unnecessary) |
|
label |
|
|
|
|
|
|
GOTO |
none (use a function) |
|
jump to another location in the |
|
|
|
|
script |
|
|
|
|
|
|
PAUSE |
sleep |
|
pause or wait an interval |
|
|
|
|
|
|
CHOICE |
case or select |
|
menu choice |
|
|
|
|
|
|
IF |
if |
|
if−test |
|
|
|
|
|
|
IF EXIST FILENAME |
if [ −e filename ] |
|
test if file exists |
|
|
|
|
|
|
IF !%N==! |
if [ −z "$N" ] |
|
if replaceable parameter "N" not |
|
|
|
|
present |
|
|
|
|
|
|
CALL |
source or . (dot operator) |
|
"include" another script |
|
|
|
|
|
|
COMMAND /C |
source or . (dot operator) |
|
"include" another script (same as |
|
|
|
|
CALL) |
|
|
|
|
|
|
SET |
export |
|
set an environmental variable |
|
|
|
|
|
|
SHIFT |
shift |
|
left shift command−line argument |
|
|
|
|
list |
|
|
|
|
|
|
SGN |
−lt or −gt |
|
sign (of integer) |
|
|
|
|
|
|
ERRORLEVEL |
$? |
|
exit status |
|
|
|
|
|
|
CON |
stdin |
|
"console" (stdin) |
|
|
|
|
|
|
PRN |
/dev/lp0 |
|
(generic) printer device |
|
|
|
|
|
|
LPT1 |
/dev/lp0 |
|
first printer device |
|
|
|
|
|
|
COM1 |
/dev/ttyS0 |
|
first serial port |
|
|
|
|
|
|
Batch files usually contain DOS commands. These must be |
translated into their UNIX equivalents in order to |
|||
convert a batch file into a shell script. |
|
|
|
Table H−2. DOS Commands and Their UNIX Equivalents
DOS Command |
UNIX Equivalent |
Effect |
|
|
|
ASSIGN |
ln |
link file or directory |
|
|
|
ATTRIB |
chmod |
change file |
|
|
permissions |
|
|
|
CD |
cd |
change directory |
|
|
|
Appendix H. Converting DOS Batch Files to Shell Scripts |
356 |
Advanced Bash−Scripting Guide
CHDIR |
cd |
change directory |
|
|
|
|
|
CLS |
clear |
clear screen |
|
|
|
|
|
COMP |
diff, comm, cmp |
file compare |
|
|
|
|
|
COPY |
cp |
file copy |
|
|
|
|
|
Ctl−C |
Ctl−C |
break (signal) |
|
|
|
|
|
Ctl−Z |
Ctl−D |
EOF (end−of−file) |
|
|
|
|
|
DEL |
rm |
delete file(s) |
|
|
|
|
|
DELTREE |
rm −rf |
delete directory |
|
|
|
recursively |
|
|
|
|
|
DIR |
ls −l |
directory listing |
|
|
|
|
|
ERASE |
rm |
delete file(s) |
|
|
|
|
|
EXIT |
exit |
exit current process |
|
|
|
|
|
FC |
comm, cmp |
file compare |
|
|
|
|
|
FIND |
grep |
find strings in files |
|
|
|
|
|
MD |
mkdir |
make directory |
|
|
|
|
|
MKDIR |
mkdir |
make directory |
|
|
|
|
|
MORE |
more |
text file paging filter |
|
|
|
|
|
MOVE |
mv |
move |
|
|
|
|
|
PATH |
$PATH |
path to executables |
|
|
|
|
|
REN |
mv |
rename (move) |
|
|
|
|
|
RENAME |
mv |
rename (move) |
|
|
|
|
|
RD |
rmdir |
remove directory |
|
|
|
|
|
RMDIR |
rmdir |
remove directory |
|
|
|
|
|
SORT |
sort |
sort file |
|
|
|
|
|
TIME |
date |
display system time |
|
|
|
|
|
TYPE |
cat |
output file to stdout |
|
|
|
|
|
XCOPY |
cp |
(extended) file copy |
|
|
|
|
|
|
|
Virtually all UNIX and shell operators and commands have |
|
|
|
many more options and enhancements than their DOS and |
|
|
|
batch file equivalents. Many DOS batch files rely on |
|
|
|
auxiliary utilities, such as ask.com, a crippled counterpart |
|
|
|
to read. |
|
|
|
DOS supports a very limited and incompatible subset of |
|
|
|
filename wildcard expansion, recognizing only the * and |
|
|
|
? characters. |
Appendix H. Converting DOS Batch Files to Shell Scripts |
357 |
Advanced Bash−Scripting Guide
Converting a DOS batch file into a shell script is generally straightforward, and the result ofttimes reads better than the original.
Example H−1. VIEWDATA.BAT: DOS Batch File
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. |
:VIEWDATA
TYPE C:\BOZO\BOOKLIST.TXT | MORE
REM SHOW ENTIRE FILE, 1 PAGE AT A TIME.
:EXIT0
The script conversion is somewhat of an improvement.
Example H−2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT
#!/bin/bash
# Conversion of VIEWDATA.BAT to shell script.
DATAFILE=/home/bozo/datafiles/book−collection.data
ARGNO=1
# @ECHO OFF |
Command |
unnecessary here. |
|
if [ $# −lt |
"$ARGNO" ] |
# IF !%1==! GOTO VIEWDATA |
|
then |
|
|
|
less |
$DATAFILE |
# TYPE C:\MYDIR\BOOKLIST.TXT | MORE |
|
else |
|
|
|
grep |
"$1" |
$DATAFILE |
# FIND "%1" C:\MYDIR\BOOKLIST.TXT |
fi |
|
|
|
exit 0 |
|
|
# :EXIT0 |
#GOTOs, labels, smoke−and−mirrors, and flimflam unnecessary.
#The converted script is short, sweet, and clean,
#which is more than can be said for the original.
Ted Davis' Shell Scripts on the PC site has a set of comprehensive tutorials on the old−fashioned art of batch file programming. Certain of his ingenious techniques could conceivably have relevance for shell scripts.
Appendix H. Converting DOS Batch Files to Shell Scripts |
358 |