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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

# ==> If command line args present, then work on files named.<br />

for i do<br />

sed −e '1,/^$/d' −e '/^[ ]*$/d' $i<br />

# −−> Ditto, as above.<br />

done<br />

fi<br />

# ==> Exercise: Add error checking and other options.<br />

# ==><br />

# ==> Note that the small sed script repeats, except for the arg passed.<br />

# ==> Does it make sense to embed it in a function? Why or why not?<br />

Example A−14. ftpget: Downloading files via ftp<br />

#! /bin/sh<br />

# $Id: ftpget,v 1.2 91/05/07 21:15:43 moraes Exp $<br />

# Script to perform batch anonymous ftp. Essentially converts a list of<br />

# of command line arguments into input to ftp.<br />

# Simple, and quick − written as a companion to ftplist<br />

# −h specifies the remote host (default prep.ai.mit.edu)<br />

# −d specifies the remote directory to cd to − you can provide a sequence<br />

# of −d options − they will be cd'ed to in turn. If the paths are relative,<br />

# make sure you get the sequence right. Be careful with relative paths −<br />

# there are far too many symlinks nowadays.<br />

# (default is the ftp login directory)<br />

# −v turns on the verbose option of ftp, and shows all responses from the<br />

# ftp server.<br />

# −f remotefile[:localfile] gets the remote file into localfile<br />

# −m pattern does an mget with the specified pattern. Remember to quote<br />

# shell characters.<br />

# −c does a local cd to the specified directory<br />

# For example,<br />

# ftpget −h expo.lcs.mit.edu −d contrib −f xplaces.shar:xplaces.sh \<br />

# −d ../pub/R3/fixes −c ~/fixes −m 'fix*'<br />

# will get xplaces.shar from ~ftp/contrib on expo.lcs.mit.edu, and put it in<br />

# xplaces.sh in the current working directory, and get all fixes from<br />

# ~ftp/pub/R3/fixes and put them in the ~/fixes directory.<br />

# Obviously, the sequence of the options is important, since the equivalent<br />

# commands are executed by ftp in corresponding order<br />

#<br />

# Mark Moraes (moraes@csri.toronto.edu), Feb 1, 1989<br />

# ==> Angle brackets changed to parens, so Docbook won't get indigestion.<br />

#<br />

# ==> These comments added by author of this document.<br />

# PATH=/local/bin:/usr/ucb:/usr/bin:/bin<br />

# export PATH<br />

# ==> Above 2 lines from original script probably superfluous.<br />

TMPFILE=/tmp/ftp.$$<br />

# ==> Creates temp file, using process id of script ($$)<br />

# ==> to construct filename.<br />

SITE=`domainname`.toronto.edu<br />

# ==> 'domainname' similar to 'hostname'<br />

# ==> May rewrite this to parameterize this for general use.<br />

usage="Usage: $0 [−h remotehost] [−d remotedirectory]... [−f remfile:localfile]... \<br />

Appendix A. Contributed Scripts 386

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

Saved successfully!

Ooh no, something went wrong!