27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[−c localdirectory] [−m filepattern] [−v]"<br />

ftpflags="−i −n"<br />

verbflag=<br />

set −f<br />

# So we can use globbing in −m<br />

set x `getopt vh:d:c:m:f: $*`<br />

if [ $? != 0 ]; then<br />

echo $usage<br />

exit 65<br />

fi<br />

shift<br />

trap 'rm −f ${TMPFILE} ; exit' 0 1 2 3 15<br />

echo "user anonymous ${USER−gnu}@${SITE} > ${TMPFILE}"<br />

# ==> Added quotes (recommended in complex echoes).<br />

echo binary >> ${TMPFILE}<br />

for i in $* # ==> Parse command line args.<br />

do<br />

case $i in<br />

−v) verbflag=−v; echo hash >> ${TMPFILE}; shift;;<br />

−h) remhost=$2; shift 2;;<br />

−d) echo cd $2 >> ${TMPFILE};<br />

if [ x${verbflag} != x ]; then<br />

echo pwd >> ${TMPFILE};<br />

fi;<br />

shift 2;;<br />

−c) echo lcd $2 >> ${TMPFILE}; shift 2;;<br />

−m) echo mget "$2" >> ${TMPFILE}; shift 2;;<br />

−f) f1=`expr "$2" : "\([^:]*\).*"`; f2=`expr "$2" : "[^:]*:\(.*\)"`;<br />

echo get ${f1} ${f2} >> ${TMPFILE}; shift 2;;<br />

−−) shift; break;;<br />

esac<br />

done<br />

if [ $# −ne 0 ]; then<br />

echo $usage<br />

exit 65 # ==> Changed from "exit 2" to conform with standard.<br />

fi<br />

if [ x${verbflag} != x ]; then<br />

ftpflags="${ftpflags} −v"<br />

fi<br />

if [ x${remhost} = x ]; then<br />

remhost=prep.ai.mit.edu<br />

# ==> Rewrite to match your favorite ftp site.<br />

fi<br />

echo quit >> ${TMPFILE}<br />

# ==> All commands saved in tempfile.<br />

ftp ${ftpflags} ${remhost} < ${TMPFILE}<br />

# ==> Now, tempfile batch processed by ftp.<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

rm −f ${TMPFILE}<br />

# ==> Finally, tempfile deleted (you may wish to copy it to a logfile).<br />

# ==> Exercises:<br />

# ==> −−−−−−−−−<br />

# ==> 1) Add error checking.<br />

# ==> 2) Add bells & whistles.<br />

+<br />

Antek Sawicki contributed the following script, which makes very clever use of the parameter substitution<br />

operators discussed in Section 9.3.<br />

Appendix A. Contributed Scripts 387

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!