11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

until [ -f "$savePath/$inFile" ]; do # Keep going till we get something.<br />

if [ ! -f "${savePath}/${inFile}" ]; then # If file doesn’t exist.<br />

echo "Sorry, that file does not exist. Please choose from:"<br />

ls $savePath # If a mistake is made.<br />

read inFile<br />

fi<br />

done<br />

filePath="${savePath}/${inFile}" # Make one variable . . .<br />

fi<br />

else filePath="${savePath}/${OPTARG}" # Which can be many things . . .<br />

fi<br />

if [ ! -f "$filePath" ]; then # If a bogus file got through.<br />

echo "You <strong>di</strong>d not specify a suitable file."<br />

echo "Run this script with the -${save} option first."<br />

echo "Aborting."<br />

exit $E_NO_SAVEFILE<br />

fi<br />

echo "Using: $filePath"<br />

while read; do<br />

eval $REPLY<br />

echo "Completed: $REPLY"<br />

done < $filePath # Feed the actual file we are using into a ’while’ loop.<br />

exit<br />

}<br />

# Fish out any options we are using for the script.<br />

# This is based on the demo in "Learning The <strong>Bash</strong> Shell" (O’Reilly).<br />

while getopts ":$save$cook$help$list$runn:$inpu$wopt" opt<br />

do<br />

case $opt in<br />

$save) save_func;; # Save some wgetter sessions for later.<br />

$cook) cookie_func;; # Change cookie file.<br />

$help) usage;; # Get help.<br />

$list) list_func;; # Allow wget to use a list of URLs.<br />

$runn) run_func;; # Useful if you are calling wgetter from, for example,<br />

#+ a cron script.<br />

$inpu) run_func;; # When you don’t know what your files are named.<br />

$wopt) wopts;; # Pass options <strong>di</strong>rectly to wget.<br />

\?) echo "Not a valid option."<br />

echo "Use -${wopt} if you want to pass options <strong>di</strong>rectly to wget,"<br />

echo "or -${help} for help";; # Catch anything else.<br />

esac<br />

done<br />

shift $((OPTIND - 1)) # Do funky magic stuff with $#.<br />

if [ -z "$1" ] && [ -z "$lister" ]; then<br />

# We should be left with at least one URL<br />

#+ on the command line, unless a list is<br />

723

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

Saved successfully!

Ooh no, something went wrong!