This commit is contained in:
gferg 2005-02-09 20:54:20 +00:00
parent 2bbea82cc4
commit bb68e7df30
3 changed files with 1209 additions and 0 deletions

View File

@ -0,0 +1,723 @@
############################################################################
#
# cdll
# by Phil Braham
#
# ############################################
# Latest version of this script available from
# http://freshmeat.net/projects/cd/
# ############################################
#
# .cd_new
#
# An enhancement of the Unix cd command
#
# There are unlimited stack entries and special entries. The stack
# entries keep the last cd_maxhistory
# directories that have been used. The special entries can be assigned
# to commonly used directories.
#
# The special entries may be pre-assigned by setting the environment
# variables CDSn or by using the -u or -U command.
#
# The following is a suggestion for the .profile file:
#
# . cdll # Set up the cd command
# alias cd='cd_new' # Replace te cd command
# cd -U # Upload pre-assigned entries for
# #+ the stact and special entries
# cd -D # Set non-default mode
# alias @="cd_new @" # Allow @ to be used to get history
#
# For help type:
#
# cd -h or
# cd -H
#
#
############################################################################
#
# Version 1.2.1
#
# Written by Phil Braham - Realtime Software Pty Ltd
# (realtime@mpx.com.au)
# Please send any suggestions or enhancements to the author (also at
# phil@braham.net)
#
############################################################################
cd_hm ()
{
${PRINTF} "%s" "cd [dir] [0-9] [@[s|h] [-g [<dir>]] [-d] [-D] [-r<n>] [dir|0-9] [-R<n>] [<dir>|0-9]
[-s<n>] [-S<n>] [-u] [-U] [-f] [-F] [-h] [-H] [-v]
<dir> Go to directory
0-n Goto previous directory (0 is previous, 1 is last but 1 etc)
n is up to max history (default is 50)
@ List history and special entries
@h List history entries
@s List special entries
-g [<dir>] Go to literal name (bypass special names)
This is to allow access to dirs called '0','1','-h' etc
-d Change default action - verbose. (See note)
-D Change default action - silent. (See note)
-s<n> Go to the special entry <n>*
-S<n> Go to the special entry <n> and replace it with the current dir*
-r<n> [<dir>] Go to directory <dir> and then put it on special entry <n>*
-R<n> [<dir>] Go to directory <dir> and put current dir on special entry <n>*
-a<n> Alternative suggested directory. See note below.
-f [<file>] File entries to <file>.
-u [<file>] Update entries from <file>.
If no filename supplied then default file (${CDPath}${2:-"$CDFile"}) is used
-F and -U are silent versions
-v Print version number
-h Help
-H Detailed help
*The special entries (0 - 9) are held until log off, replaced by another entry
or updated with the -u command
Alternative suggested directories:
If a directory is not found then CD will suggest any possibilities. These are
directories starting with the same letters and if any are found they are listed
prefixed with -a<n> where <n> is a number.
It's possible to go to the directory by entering cd -a<n> on the command line.
The directory for -r<n> or -R<n> may be a number. For example:
$ cd -r3 4 Go to history entry 4 and put it on special entry 3
$ cd -R3 4 Put current dir on the special entry 3 and go to history entry 4
$ cd -s3 Go to special entry 3
Note that commands R,r,S and s may be used without a number and refer to 0:
$ cd -s Go to special entry 0
$ cd -S Go to special entry 0 and make special entry 0 current dir
$ cd -r 1 Go to history entry 1 and put it on special entry 0
$ cd -r Go to history entry 0 and put it on special entry 0
"
if ${TEST} "$CD_MODE" = "PREV"
then
${PRINTF} "$cd_mnset"
else
${PRINTF} "$cd_mset"
fi
}
cd_Hm ()
{
cd_hm
${PRINTF} "%s" "
The previous directories (0-$cd_maxhistory) are stored in the
environment variables CD[0] - CD[$cd_maxhistory]
Similarly the special directories S0 - $cd_maxspecial are in
the environment variable CDS[0] - CDS[$cd_maxspecial]
and may be accessed from the command line
The default pathname for the -f and -u commands is $CDPath
The default filename for the -f and -u commands is $CDFile
Set the following environment variables:
CDL_PROMPTLEN - Set to the length of prompt you require.
Prompt string is set to the right characters of the
current directory.
If not set then prompt is left unchanged
CDL_PROMPT_PRE - Set to the string to prefix the prompt.
Default is:
non-root: \"\\[\\e[01;34m\\]\" (sets colour to blue).
root: \"\\[\\e[01;31m\\]\" (sets colour to red).
CDL_PROMPT_POST - Set to the string to suffix the prompt.
Default is:
non-root: \"\\[\\e[00m\\]$\" (resets colour and displays $).
root: \"\\[\\e[00m\\]#\" (resets colour and displays #).
CDPath - Set the default path for the -f & -u options.
Default is home directory
CDFile - Set the default filename for the -f & -u options.
Default is cdfile
"
cd_version
}
cd_version ()
{
printf "Version: ${VERSION_MAJOR}.${VERSION_MINOR} Date: ${VERSION_DATE}\n"
}
#
# Truncate right.
#
# params:
# p1 - string
# p2 - length to truncate to
#
# returns string in tcd
#
cd_right_trunc ()
{
local tlen=${2}
local plen=${#1}
local str="${1}"
local diff
local filler="<--"
if ${TEST} ${plen} -le ${tlen}
then
tcd="${str}"
else
let diff=${plen}-${tlen}
elen=3
if ${TEST} ${diff} -le 2
then
let elen=${diff}
fi
tlen=-${tlen}
let tlen=${tlen}+${elen}
tcd=${filler:0:elen}${str:tlen}
fi
}
#
# Three versions of do history:
# cd_dohistory - packs history and specials side by side
# cd_dohistoryH - Shows only hstory
# cd_dohistoryS - Shows only specials
#
cd_dohistory ()
{
cd_getrc
${PRINTF} "History:\n"
local -i count=${cd_histcount}
while ${TEST} ${count} -ge 0
do
cd_right_trunc "${CD[count]}" ${cd_lchar}
${PRINTF} "%2d %-${cd_lchar}.${cd_lchar}s " ${count} "${tcd}"
cd_right_trunc "${CDS[count]}" ${cd_rchar}
${PRINTF} "S%d %-${cd_rchar}.${cd_rchar}s\n" ${count} "${tcd}"
count=${count}-1
done
}
cd_dohistoryH ()
{
cd_getrc
${PRINTF} "History:\n"
local -i count=${cd_maxhistory}
while ${TEST} ${count} -ge 0
do
${PRINTF} "${count} %-${cd_flchar}.${cd_flchar}s\n" ${CD[$count]}
count=${count}-1
done
}
cd_dohistoryS ()
{
cd_getrc
${PRINTF} "Specials:\n"
local -i count=${cd_maxspecial}
while ${TEST} ${count} -ge 0
do
${PRINTF} "S${count} %-${cd_flchar}.${cd_flchar}s\n" ${CDS[$count]}
count=${count}-1
done
}
cd_getrc ()
{
cd_flchar=$(stty -a | awk -F \; '/rows/ { print $2 $3 }' | awk -F \ '{ print $4 }')
if ${TEST} ${cd_flchar} -ne 0
then
cd_lchar=${cd_flchar}/2-5
cd_rchar=${cd_flchar}/2-5
cd_flchar=${cd_flchar}-5
else
cd_flchar=${FLCHAR:=75} # cd_flchar is used for for the @s & @h history
cd_lchar=${LCHAR:=35}
cd_rchar=${RCHAR:=35}
fi
}
cd_doselection ()
{
local -i nm=0
cd_doflag="TRUE"
if ${TEST} "${CD_MODE}" = "PREV"
then
if ${TEST} -z "$cd_npwd"
then
cd_npwd=0
fi
fi
tm=$(echo "${cd_npwd}" | cut -b 1)
if ${TEST} "${tm}" = "-"
then
pm=$(echo "${cd_npwd}" | cut -b 2)
nm=$(echo "${cd_npwd}" | cut -d $pm -f2)
case "${pm}" in
a) cd_npwd=${cd_sugg[$nm]} ;;
s) cd_npwd="${CDS[$nm]}" ;;
S) cd_npwd="${CDS[$nm]}" ; CDS[$nm]=`pwd` ;;
r) cd_npwd="$2" ; cd_specDir=$nm ; cd_doselection "$1" "$2";;
R) cd_npwd="$2" ; CDS[$nm]=`pwd` ; cd_doselection "$1" "$2";;
esac
fi
if ${TEST} "${cd_npwd}" != "." -a "${cd_npwd}" != ".." -a "${cd_npwd}" -le ${cd_maxhistory} >>/dev/null 2>&1
then
cd_npwd=${CD[$cd_npwd]}
else
case "$cd_npwd" in
@) cd_dohistory ; cd_doflag="FALSE" ;;
@h) cd_dohistoryH ; cd_doflag="FALSE" ;;
@s) cd_dohistoryS ; cd_doflag="FALSE" ;;
-h) cd_hm ; cd_doflag="FALSE" ;;
-H) cd_Hm ; cd_doflag="FALSE" ;;
-f) cd_fsave "SHOW" $2 ; cd_doflag="FALSE" ;;
-u) cd_upload "SHOW" $2 ; cd_doflag="FALSE" ;;
-F) cd_fsave "NOSHOW" $2 ; cd_doflag="FALSE" ;;
-U) cd_upload "NOSHOW" $2 ; cd_doflag="FALSE" ;;
-g) cd_npwd="$2" ;;
-d) cd_chdefm 1; cd_doflag="FALSE" ;;
-D) cd_chdefm 0; cd_doflag="FALSE" ;;
-r) cd_npwd="$2" ; cd_specDir=0 ; cd_doselection "$1" "$2";;
-R) cd_npwd="$2" ; CDS[0]=`pwd` ; cd_doselection "$1" "$2";;
-s) cd_npwd="${CDS[0]}" ;;
-S) cd_npwd="${CDS[0]}" ; CDS[0]=`pwd` ;;
-v) cd_version ; cd_doflag="FALSE";;
esac
fi
}
cd_chdefm ()
{
if ${TEST} "${CD_MODE}" = "PREV"
then
CD_MODE=""
if ${TEST} $1 -eq 1
then
${PRINTF} "${cd_mset}"
fi
else
CD_MODE="PREV"
if ${TEST} $1 -eq 1
then
${PRINTF} "${cd_mnset}"
fi
fi
}
cd_fsave ()
{
local sfile=${CDPath}${2:-"$CDFile"}
if ${TEST} "$1" = "SHOW"
then
${PRINTF} "Saved to %s\n" $sfile
fi
${RM} -f ${sfile}
local -i count=0
while ${TEST} ${count} -le ${cd_maxhistory}
do
echo "CD[$count]=\"${CD[$count]}\"" >> ${sfile}
count=${count}+1
done
count=0
while ${TEST} ${count} -le ${cd_maxspecial}
do
echo "CDS[$count]=\"${CDS[$count]}\"" >> ${sfile}
count=${count}+1
done
}
cd_upload ()
{
local sfile=${CDPath}${2:-"$CDFile"}
if ${TEST} "${1}" = "SHOW"
then
${PRINTF} "Loading from %s\n" ${sfile}
fi
. ${sfile}
}
cd_new ()
{
local -i count
local -i choose=0
cd_npwd="${1}"
cd_specDir=-1
cd_doselection "${1}" "${2}"
if ${TEST} ${cd_doflag} = "TRUE"
then
if ${TEST} "${CD[0]}" != "`pwd`"
then
count=$cd_maxhistory
while ${TEST} $count -gt 0
do
CD[$count]=${CD[$count-1]}
count=${count}-1
done
CD[0]=`pwd`
fi
command cd "${cd_npwd}" 2>/dev/null
if ${TEST} $? -eq 1
then
${PRINTF} "Unknown dir: %s\n" "${cd_npwd}"
local -i ftflag=0
for i in "${cd_npwd}"*
do
if ${TEST} -d "${i}"
then
if ${TEST} ${ftflag} -eq 0
then
${PRINTF} "Suggest:\n"
ftflag=1
fi
${PRINTF} "\t-a${choose} %s\n" "$i"
cd_sugg[$choose]="${i}"
choose=${choose}+1
fi
done
fi
fi
if ${TEST} ${cd_specDir} -ne -1
then
CDS[${cd_specDir}]=`pwd`
fi
if ${TEST} ! -z "${CDL_PROMPTLEN}"
then
cd_right_trunc "${PWD}" ${CDL_PROMPTLEN}
cd_rp=${CDL_PROMPT_PRE}${tcd}${CDL_PROMPT_POST}
export PS1="$(echo -ne ${cd_rp})"
fi
}
#################################################################################
# #
# Initialisation here #
# #
#################################################################################
#
VERSION_MAJOR="1"
VERSION_MINOR="2.1"
VERSION_DATE="24-MAY-2003"
#
alias cd=cd_new
#
# Set up commands
RM=/bin/rm
TEST=test
PRINTF=printf # Use builtin printf
#################################################################################
# #
# Change this to modify the default pre- and post prompt strings. #
# These only come into effect if CDL_PROMPTLEN is set. #
# #
#################################################################################
if ${TEST} ${EUID} -eq 0
then
# CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="$HOSTNAME@"}
CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="\\[\\e[01;31m\\]"} # Root is in red
CDL_PROMPT_POST=${CDL_PROMPT_POST:="\\[\\e[00m\\]#"}
else
CDL_PROMPT_PRE=${CDL_PROMPT_PRE:="\\[\\e[01;34m\\]"} # Users in blue
CDL_PROMPT_POST=${CDL_PROMPT_POST:="\\[\\e[00m\\]$"}
fi
#################################################################################
#
# cd_maxhistory defines the max number of history entries allowed.
typeset -i cd_maxhistory=50
#################################################################################
#
# cd_maxspecial defines the number of special entries.
typeset -i cd_maxspecial=9
#
#
#################################################################################
#
# cd_histcount defines the number of entries displayed in the history command.
typeset -i cd_histcount=9
#
#################################################################################
export CDPath=${HOME}/
# Change these to use a different #
#+ default path and filename #
export CDFile=${CDFILE:=cdfile} # for the -u and -f commands #
#
#################################################################################
#
typeset -i cd_lchar cd_rchar cd_flchar
# This is the number of chars to allow for the #
cd_flchar=${FLCHAR:=75} #+ cd_flchar is used for for the @s & @h history #
typeset -ax CD CDS
#
cd_mset="\n\tDefault mode is now set - entering cd with no parameters has the default action\n\tUse cd -d or -D for cd to go to previous directory with no parameters\n"
cd_mnset="\n\tNon-default mode is now set - entering cd with no parameters is the same as entering cd 0\n\tUse cd -d or -D to change default cd action\n"
# ==================================================================== #
: &lt;&lt;DOCUMENTATION
Written by Phil Braham. Realtime Software Pty Ltd.
Released under GNU license. Free to use. Please pass any modifications
or comments to the author Phil Braham:
realtime@mpx.com.au
===============================================================================
cdll is a replacement for cd and incorporates similar functionality to
the bash pushd and popd commands but is independent of them.
This version of cdll has been tested on Linux using Bash. It will work
on most Linux versions but will probably not work on other shells without
modification.
Introduction
============
cdll allows easy moving about between directories. When changing to a new
directory the current one is automatically put onto a stack. By default
50 entries are kept, but this is configurable. Special directories can be
kept for easy access - by default up to 10, but this is configurable. The
most recent stack entries and the special entries can be easily viewed.
The directory stack and special entries can be saved to, and loaded from,
a file. This allows them to be set up on login, saved before logging out
or changed when moving project to project.
In addition, cdll provides a flexible command prompt facility that allows,
for example, a directory name in colour that is truncated from the left
if it gets too long.
Setting up cdll
===============
Copy cdll to either your local home directory or a central directory
such as /usr/bin (this will require root access).
Copy the file cdfile to your home directory. It will require read and
write access. This a default file that contains a directory stack and
special entries.
To replace the cd command you must add commands to your login script.
The login script is one or more of:
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc
/etc/bash.bashrc.local
To setup your login, ~/.bashrc is recommended, for global (and root) setup
add the commands to /etc/bash.bashrc.local
To set up on login, add the command:
. &lt;dir&gt;/cdll
For example if cdll is in your local home directory:
. ~/cdll
If in /usr/bin then:
. /usr/bin/cdll
If you want to use this instead of the buitin cd command then add:
alias cd='cd_new'
We would also recommend the following commands:
alias @='cd_new @'
cd -U
cd -D
If you want to use cdll's prompt facilty then add the following:
CDL_PROMPTLEN=nn
Where nn is a number described below. Initially 99 would be suitable
number.
Thus the script looks something like this:
######################################################################
# CD Setup
######################################################################
CDL_PROMPTLEN=21 # Allow a prompt length of up to 21 characters
. /usr/bin/cdll # Initialise cdll
alias cd='cd_new' # Replace the built in cd command
alias @='cd_new @' # Allow @ at the prompt to display history
cd -U # Upload directories
cd -D # Set default action to non-posix
######################################################################
The full meaning of these commands will become clear later.
There are a couple of caveats. If another program changes the directory
without calling cdll, then the directory won't be put on the stack and
also if the prompt facility is used then this will not be updated. Two
programs that can do this are pushd and popd. To update the prompt and
stack simply enter:
cd .
Note that if the previous entry on the stack is the current directory
then the stack is not updated.
Usage
=====
cd [dir] [0-9] [@[s|h] [-g &lt;dir&gt;] [-d] [-D] [-r&lt;n&gt;] [dir|0-9] [-R&lt;n&gt;]
[&lt;dir&gt;|0-9] [-s&lt;n&gt;] [-S&lt;n&gt;] [-u] [-U] [-f] [-F] [-h] [-H] [-v]
&lt;dir&gt; Go to directory
0-n Goto previous directory (0 is previous, 1 is last but 1, etc.)
n is up to max history (default is 50)
@ List history and special entries (Usually available as $ @)
@h List history entries
@s List special entries
-g [&lt;dir&gt;] Go to literal name (bypass special names)
This is to allow access to dirs called '0','1','-h' etc
-d Change default action - verbose. (See note)
-D Change default action - silent. (See note)
-s&lt;n&gt; Go to the special entry &lt;n&gt;
-S&lt;n&gt; Go to the special entry &lt;n&gt; and replace it with the current dir
-r&lt;n&gt; [&lt;dir&gt;] Go to directory &lt;dir&gt; and then put it on special entry &lt;n&gt;
-R&lt;n&gt; [&lt;dir&gt;] Go to directory &lt;dir&gt; and put current dir on special entry &lt;n&gt;
-a&lt;n&gt; Alternative suggested directory. See note below.
-f [&lt;file&gt;] File entries to &lt;file&gt;.
-u [&lt;file&gt;] Update entries from &lt;file&gt;.
If no filename supplied then default file (~/cdfile) is used
-F and -U are silent versions
-v Print version number
-h Help
-H Detailed help
Examples
========
These examples assume non-default mode is set (that is, cd with no
parameters will go to the most recent stack directory), that aliases
have been set up for cd and @ as described above and that cd's prompt
facility is active and the prompt length is 21 characters.
/home/phil$ @ # List the entries with the @
History: # Output of the @ command
..... # Skipped these entries for brevity
1 /home/phil/ummdev S1 /home/phil/perl # Most recent two history entries
0 /home/phil/perl/eg S0 /home/phil/umm/ummdev # and two special entries are shown
/home/phil$ cd /home/phil/utils/Cdll # Now change directories
/home/phil/utils/Cdll$ @ # Prompt reflects the directory.
History: # New history
.....
1 /home/phil/perl/eg S1 /home/phil/perl # History entry 0 has moved to 1
0 /home/phil S0 /home/phil/umm/ummdev # and the most recent has entered
To go to a history entry:
/home/phil/utils/Cdll$ cd 1 # Go to history entry 1.
/home/phil/perl/eg$ # Current directory is now what was 1
To go to a special entry:
/home/phil/perl/eg$ cd -s1 # Go to special entry 1
/home/phil/umm/ummdev$ # Current directory is S1
To go to a directory called, for example, 1:
/home/phil$ cd -g 1 # -g ignores the special meaning of 1
/home/phil/1$
To put current directory on the special list as S1:
cd -r1 . # OR
cd -R1 . # These have the same effect if the directory is
#+ . (the current directory)
To go to a directory and add it as a special
The directory for -r&lt;n&gt; or -R&lt;n&gt; may be a number. For example:
$ cd -r3 4 Go to history entry 4 and put it on special entry 3
$ cd -R3 4 Put current dir on the special entry 3 and go to
history entry 4
$ cd -s3 Go to special entry 3
Note that commands R,r,S and s may be used without a number and
refer to 0:
$ cd -s Go to special entry 0
$ cd -S Go to special entry 0 and make special entry 0
current dir
$ cd -r 1 Go to history entry 1 and put it on special entry 0
$ cd -r Go to history entry 0 and put it on special entry 0
Alternative suggested directories:
If a directory is not found, then CD will suggest any
possibilities. These are directories starting with the same letters
and if any are found they are listed prefixed with -a&lt;n&gt;
where &lt;n&gt; is a number. It's possible to go to the directory
by entering cd -a&lt;n&gt; on the command line.
Use cd -d or -D to change default cd action. cd -H will show
current action.
The history entries (0-n) are stored in the environment variables
CD[0] - CD[n]
Similarly the special directories S0 - 9 are in the environment
variable CDS[0] - CDS[9]
and may be accessed from the command line, for example:
ls -l ${CDS[3]}
cat ${CD[8]}/file.txt
The default pathname for the -f and -u commands is ~
The default filename for the -f and -u commands is cdfile
Configuration
=============
The following environment variables can be set:
CDL_PROMPTLEN - Set to the length of prompt you require.
Prompt string is set to the right characters of the current
directory. If not set, then prompt is left unchanged. Note
that this is the number of characters that the directory is
shortened to, not the total characters in the prompt.
CDL_PROMPT_PRE - Set to the string to prefix the prompt.
Default is:
non-root: "\\[\\e[01;34m\\]" (sets colour to blue).
root: "\\[\\e[01;31m\\]" (sets colour to red).
CDL_PROMPT_POST - Set to the string to suffix the prompt.
Default is:
non-root: "\\[\\e[00m\\]$" (resets colour and displays $).
root: "\\[\\e[00m\\]#" (resets colour and displays #).
Note:
CDL_PROMPT_PRE & _POST only t
CDPath - Set the default path for the -f & -u options.
Default is home directory
CDFile - Set the default filename for the -f & -u options.
Default is cdfile
There are three variables defined in the file cdll which control the
number of entries stored or displayed. They are in the section labeled
'Initialisation here' towards the end of the file.
cd_maxhistory - The number of history entries stored.
Default is 50.
cd_maxspecial - The number of special entries allowed.
Default is 9.
cd_histcount - The number of history and special entries
displayed. Default is 9.
Note that cd_maxspecial should be >= cd_histcount to avoid displaying
special entries that can't be set.
Version: 1.2.1 Date: 24-MAY-2003
DOCUMENTATION

View File

@ -0,0 +1,24 @@
#!/bin/bash
# func-cmdlinearg.sh
# Call this script with a command-line argument,
#+ something like $0 arg1.
func ()
{
echo "$1"
}
echo "First call to function: no arg passed."
echo "See if command-line arg is seen."
func
# No! Command-line arg not seen.
echo "============================================================"
echo
echo "Second call to function: command-line arg passed explicitly."
func $1
# Now it's seen!
exit 0

View File

@ -0,0 +1,462 @@
#!/bin/bash
# wgetter2.bash
# Author: Little Monster [monster@monstruum.co.uk]
# ==> Used in ABS Guide with permission of script author.
# ==> This script still needs debugging and fixups (exercise for reader).
# ==> It could also use some additional editing in the comments.
# This is wgetter2 --
#+ a Bash script to make wget a bit more friendly, and save typing.
# Carefully crafted by Little Monster.
# More or less complete on 02/02/2005.
# If you think this script can be improved,
#+ email me at: monster@monstruum.co.uk
# ==> and cc: to the author of the ABS Guide, please.
# This script is licenced under the GPL.
# You are free to copy, alter and re-use it,
#+ but please don't try to claim you wrote it.
# Log your changes here instead.
# =======================================================================
# changelog:
# 02/02/2005. Minor additions by Little Monster.
# (See after # +++++++++++ )
# 29/01/2005. Minor stylistic edits and cleanups by author of ABS Guide.
# Added exit error codes.
# 22/11/2004. Finished initial version of second version of wgetter:
# wgetter2 is born.
# 01/12/2004. Changed 'runn' function so it can be run 2 ways --
# either ask for a file name or have one input on the CL.
# 01/12/2004. Made sensible handling of no URL's given.
# 01/12/2004. Made loop of main options, so you don't
# have to keep calling wgetter 2 all the time.
# Runs as a session instead.
# 01/12/2004. Added looping to 'runn' function.
# Simplified and improved.
# 01/12/2004. Added state to recursion setting.
# Enables re-use of previous value.
# 05/12/2004. Modified the file detection routine in the 'runn' function
# so it's not fooled by empty values, and is cleaner.
# 01/02/2004. Added cookie finding routine from later version (which
# isn't ready yet), so as not to have hard-coded paths.
# =======================================================================
# Error codes for abnormal exit.
E_USAGE=67 # Usage message, then quit.
E_NO_OPTS=68 # No command-line args entered.
E_NO_URLS=69 # No URLs passed to script.
E_NO_SAVEFILE=70 # No save filename passed to script.
E_USER_EXIT=71 # User decides to quit.
# Basic default wget command we want to use.
# This is the place to change it, if required.
# NB: if using a proxy, set http_proxy = yourproxy in .wgetrc.
# Otherwise delete --proxy=on, below.
# ====================================================================
CommandA="wget -nc -c -t 5 --progress=bar --random-wait --proxy=on -r"
# ====================================================================
# --------------------------------------------------------------------
# Set some other variables and explain them.
pattern=" -A .jpg,.JPG,.jpeg,.JPEG,.gif,.GIF,.htm,.html,.shtml,.php"
# wget's option to only get certain types of file.
# comment out if not using
today=`date +%F` # Used for a filename.
home=$HOME # Set HOME to an internal variable.
# In case some other path is used, change it here.
depthDefault=3 # Set a sensible default recursion.
Depth=$depthDefault # Otherwise user feedback doesn't tie in properly.
RefA="" # Set blank referring page.
Flag="" # Default to not saving anything,
#+ or whatever else might be wanted in future.
lister="" # Used for passing a list of urls directly to wget.
Woptions="" # Used for passing wget some options for itself.
inFile="" # Used for the run function.
newFile="" # Used for the run function.
savePath="$home/w-save"
Config="$home/.wgetter2rc"
# This is where some variables can be stored,
#+ if permanently changed from within the script.
Cookie_List="$home/.cookielist"
# So we know where the cookies are kept . . .
cFlag="" # Part of the cookie file selection routine.
# Define the options available. Easy to change letters here if needed.
# These are the optional options; you don't just wait to be asked.
save=s # Save command instead of executing it.
cook=c # Change cookie file for this session.
help=h # Usage guide.
list=l # Pass wget the -i option and URL list.
runn=r # Run saved commands as an argument to the option.
inpu=i # Run saved commands interactively.
wopt=w # Allow to enter options to pass directly to wget.
# --------------------------------------------------------------------
if [ -z "$1" ]; then # Make sure we get something for wget to eat.
echo "You must at least enter a URL or option!"
echo "-$help for usage."
exit $E_NO_OPTS
fi
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# added added added added added added added added added added added added
if [ ! -e "$Config" ]; then # See if configuration file exists.
echo "Creating configuration file, $Config"
echo "# This is the configuration file for wgetter2" > "$Config"
echo "# Your customised settings will be saved in this file" >> "$Config"
else
source $Config # Import variables we set outside the script.
fi
if [ ! -e "$Cookie_List" ]; then
# Set up a list of cookie files, if there isn't one.
echo "Hunting for cookies . . ."
find -name cookies.txt >> $Cookie_List # Create the list of cookie files.
fi # Isolate this in its own 'if' statement,
#+ in case we got interrupted while searching.
if [ -z "$cFlag" ]; then # If we haven't already done this . . .
echo # Make a nice space after the command prompt.
echo "Looks like you haven't set up your source of cookies yet."
n=0 # Make sure the counter doesn't contain random values.
while read; do
Cookies[$n]=$REPLY # Put the cookie files we found into an array.
echo "$n) ${Cookies[$n]}" # Create a menu.
n=$(( n + 1 )) # Increment the counter.
done < $Cookie_List # Feed the read statement.
echo "Enter the number of the cookie file you want to use."
echo "If you won't be using cookies, just press RETURN."
echo
echo "I won't be asking this again. Edit $Config"
echo "If you decide to change at a later date"
echo "or use the -${cook} option for per session changes."
read
if [ ! -z $REPLY ]; then # User didn't just press return.
Cookie=" --load-cookies ${Cookies[$REPLY]}"
# Set the variable here as well as in the config file.
echo "Cookie=\" --load-cookies ${Cookies[$REPLY]}\"" >> $Config
fi
echo "cFlag=1" >> $Config # So we know not to ask again.
fi
# end added section end added section end added section end added section end
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Another variable.
# This one may or may not be subject to variation.
# A bit like the small print.
CookiesON=$Cookie
# echo "cookie file is $CookiesON" # For debugging.
# echo "home is ${home}" # For debugging. Got caught with this one!
wopts()
{
echo "Enter options to pass to wget."
echo "It is assumed you know what you're doing."
echo
echo "You can pass their arguments here too."
# That is to say, everything passed here is passed to wget.
read Wopts
# Read in the options to be passed to wget.
Woptions=" $Wopts"
# Assign to another variable.
# Just for fun, or something . . .
echo "passing options ${Wopts} to wget"
# Mainly for debugging.
# Is cute.
return
}
save_func()
{
echo "Settings will be saved."
if [ ! -d $savePath ]; then # See if directory exists.
mkdir $savePath # Create the directory to save things in
#+ if it isn't already there.
fi
Flag=S
# Tell the final bit of code what to do.
# Set a flag since stuff is done in main.
return
}
usage() # Tell them how it works.
{
echo "Welcome to wgetter. This is a front end to wget."
echo "It will always run wget with these options:"
echo "$CommandA"
echo "and the pattern to match: $pattern (which you can change at the top of this script)."
echo "It will also ask you for recursion depth, and if you want to use a referring page."
echo "Wgetter accepts the following options:"
echo ""
echo "-$help : Display this help."
echo "-$save : Save the command to a file $savePath/wget-($today) instead of running it."
echo "-$runn : Run saved wget commands instead of starting a new one --"
echo "Enter filename as argument to this option."
echo "-$inpu : Run saved wget commands interactively --"
echo "The script will ask you for the filename."
echo "-$cook : Change the cookies file for this session."
echo "-$list : Tell wget to use URL's from a list instead of from the command line."
echo "-$wopt : Pass any other options direct to wget."
echo ""
echo "See the wget man page for additional options you can pass to wget."
echo ""
exit $E_USAGE # End here. Don't process anything else.
}
list_func() # Gives the user the option to use the -i option to wget,
#+ and a list of URLs.
{
while [ 1 ]; do
echo "Enter the name of the file containing URL's (press q to change your mind)."
read urlfile
if [ ! -e $urlfile ] && [ $urlfile != q ]; then
# Look for a file, or the quit option.
echo "That file does not exist!"
elif [ $urlfile = q ]; then # Check quit option.
echo "Not using a URL list."
return
else
echo "Using $urlfile."
echo "If you gave me URL's on the command line, I'll use those first."
# Report wget standard behaviour to the user.
lister=" -i $urlfile" # This is what we will pass to wget.
return
fi
done
}
cookie_func() # Give the user the option to use a different cookie file.
{
while [ 1 ]; do
echo "Change the cookies file. Press return if you don't want to change it."
read Cookies
# NB: this is not the same as Cookie, earlier.
# There is an 's' on the end.
# Bit like chocolate chips.
if [ -z $Cookies ]; then # Escape clause for wusses.
return
elif [ ! -e $Cookies ]; then
echo "File does not exist. Try again." # Keep 'em going . . .
else
CookiesON=" --load-cookies $Cookies" # File is good. Let's use it!
return
fi
done
}
run_func()
{
if [ -z "$OPTARG" ]; then
# Test to see if we used the in-line option or the query one.
if [ ! -d "$savePath" ]; then # In case directory doesn't exist . . .
echo "$savePath does not appear to exist."
echo "Please supply path and filename of saved wget commands:"
read newFile
until [ -f "$newFile" ]; do # Keep going till we get something.
echo "Sorry, that file does not exist. Please try again."
# Try really hard to get something.
read newFile
done
# -------------------------------------------------------------------------
# if [ -z ( grep wget ${newfile} ) ]; then
# Assume they haven't got the right file and bail out.
# echo "Sorry, that file does not contain wget commands. Aborting."
# exit
# fi
#
# This is bogus code.
# It doesn't actually work.
# If anyone wants to fix it, feel free!
# -------------------------------------------------------------------------
filePath="${newFile}"
else
echo "Save path is $savePath"
echo "Please enter name of the file which you want to use."
echo "You have a choice of:"
ls $savePath # Give them a choice.
read inFile
until [ -f "$savePath/$inFile" ]; do # Keep going till we get something.
if [ ! -f "${savePath}/${inFile}" ]; then # If file doesn't exist.
echo "Sorry, that file does not exist. Please choose from:"
ls $savePath # If a mistake is made.
read inFile
fi
done
filePath="${savePath}/${inFile}" # Make one variable . . .
fi
else filePath="${savePath}/${OPTARG}" # Which can be many things . . .
fi
if [ ! -f "$filePath" ]; then # If a bogus file got through.
echo "You did not specify a suitable file."
echo "Run this script with the -${save} option first."
echo "Aborting."
exit $E_NO_SAVEFILE
fi
echo "Using: $filePath"
while read; do
eval $REPLY
echo "Completed: $REPLY"
done < $filePath # Feed the actual file we are using into a 'while' loop.
exit
}
# Fish out any options we are using for the script.
# This is based on the demo in "Learning The Bash Shell" (O'Reilly).
while getopts ":$save$cook$help$list$runn:$inpu$wopt" opt
do
case $opt in
$save) save_func;; # Save some wgetter sessions for later.
$cook) cookie_func;; # Change cookie file.
$help) usage;; # Get help.
$list) list_func;; # Allow wget to use a list of URLs.
$runn) run_func;; # Useful if you are calling wgetter from, for example,
#+ a cron script.
$inpu) run_func;; # When you don't know what your files are named.
$wopt) wopts;; # Pass options directly to wget.
\?) echo "Not a valid option."
echo "Use -${wopt} if you want to pass options directly to wget,"
echo "or -${help} for help";; # Catch anything else.
esac
done
shift $((OPTIND - 1)) # Do funky magic stuff with $#.
if [ -z "$1" ] && [ ! -z $lister ]; then
# We should be left with at least one URL
#+ on the command line, unless a list is
#+ being used -- catch empty CL's.
echo "No URL's given! You must enter them on the same line as wgetter2."
echo "E.g., wgetter2 http://somesite http://anothersite."
echo "Use $help option for more information."
exit $E_NO_URLS # Bail out, with appropriate error code.
fi
URLS=" $@"
# Use this so that URL list can be changed if we stay in the option loop.
while [ 1 ]; do
# This is where we ask for the most used options.
# (Mostly unchanged from version 1 of wgetter)
if [ -z $curDepth ]; then
Current=""
else Current=" Current value is $curDepth"
fi
echo "How deep should I go? (integer: Default is $depthDefault.$Current)"
read Depth # Recursion -- how far should we go?
inputB="" # Reset this to blank on each pass of the loop.
echo "Enter the name of the referring page (default is none)."
read inputB # Need this for some sites.
echo "Do you want to have the output logged to the terminal"
echo "(y/n, default is yes)?"
read noHide # Otherwise wget will just log it to a file.
case $noHide in # Now you see me, now you don't.
y|Y ) hide="";;
n|N ) hide=" -b";;
* ) hide="";;
esac
if [ -z ${Depth} ]; then # User accepted either default or current depth,
#+ in which case Depth is now empty.
if [ -z ${curDepth} ]; then # See if a depth was set on a previous iteration.
Depth="$depthDefault" # Set the default recursion depth if nothing
#+ else to use.
else Depth="$curDepth" # Otherwise, set the one we used before.
fi
fi
Recurse=" -l $Depth" # Set how deep we want to go.
curDepth=$Depth # Remember setting for next time.
if [ ! -z $inputB ]; then
RefA=" --referer=$inputB" # Option to use referring page.
fi
WGETTER="${CommandA}${pattern}${hide}${RefA}${Recurse}${CookiesON}${lister}${Woptions}${URLS}"
# Just string the whole lot together . . .
# NB: no embedded spaces.
# They are in the individual elements so that if any are empty,
#+ we don't get an extra space.
if [ -z "${CookiesON}" ] && [ "$cFlag" = "1" ] ; then
echo "Warning -- can't find cookie file"
# This should be changed, in case the user has opted to not use cookies.
fi
if [ "$Flag" = "S" ]; then
echo "$WGETTER" >> $savePath/wget-${today}
# Create a unique filename for today, or append to it if it exists.
echo "$inputB" >> $savePath/site-list-${today}
# Make a list, so it's easy to refer back to,
#+ since the whole command is a bit confusing to look at.
echo "Command saved to the file $savePath/wget-${today}"
# Tell the user.
echo "Referring page URL saved to the file $savePath/site-list-${today}"
# Tell the user.
Saver=" with save option"
# Stick this somewhere, so it appears in the loop if set.
else
echo "*****************"
echo "*****Getting*****"
echo "*****************"
echo ""
echo "$WGETTER"
echo ""
echo "*****************"
eval "$WGETTER"
fi
echo ""
echo "Starting over$Saver."
echo "If you want to stop, press q."
echo "Otherwise, enter some URL's:"
# Let them go again. Tell about save option being set.
read
case $REPLY in # Need to change this to a 'trap' clause.
q|Q ) exit $E_USER_EXIT;; # Exercise for the reader?
* ) URLS=" $REPLY";;
esac
echo ""
done
exit 0