[arch-general] Something so ugly only a BASH scripter could love it.
David C. Rankin
drankinatty at suddenlinkmail.com
Sat Oct 17 03:27:39 EDT 2009
Listmates,
Here is an off-the-wall one for the BASH guys. Many times I need to get file
or directory information from the command line with wildcards in the filename
or path. I have always hated having to quote the filename or path to prevent
shell expansion before it gets to my script. I was pecking around on a script
for rsync that would take the same type of file and path information with
wildcards and hit upon a really cool way to handle the input without having to
quote the names with wildcards to prevent expansion. (.. at least I thought it
was cool)
Any way, I thought somebody else could get some use out of it. I've replaced
the rsync calls with echo statments so your can test it if you have any use
for it. Any script that deals with multiple files or filespecs is a candidate
for its use. Take a look.
The process is a 2-loop process with an Array of cli inputs as the outer loop
and the inner loop tests whether the array element holds a directory or a
filespec with globbing. If it is a filespec with wildcards, then the wildcards
are processed with the inner loop using ls for the individual files. Works for
one file or a thousand specified in as many chunks as you want to put on the
command line:
#!/bin/bash --norc
OLDIFS=$IFS
IFS=$'\n'
## Fill an Array with all CLI input
declare -a CLIARRAY
CLIARRAY=( "$@" )
## Step through CLIARRAY with ls to expand wildcards and process
## files specified on the command line sequentially. Rely on
## rsync to throw error if bad filename
## Simple echo is used for this example
for ((a=0;a<${#CLIARRAY[@]};a++)); do
## if the argument is a directory rsync in 1 shot, else rsync each file
if [[ -d ${CLIARRAY[${a}]} ]]; then
echo "directory: ${CLIARRAY[${a}]}"
else
for b in $(ls ${CLIARRAY[${a}]}); do
echo "file: $b"
done
fi
done
IFS=$OLDIFS
exit 0
If you are interested in using it with rsync as I had it originally, just
replace the first echo with:
rsync -ruv ${CLIARRAY[${a}]} ${SSUSER}@${DESTHOST}:${DESTPATH}
and the second echo with:
rsync -ruv $b ${SSUSER}@${DESTHOST}:${DESTPATH}
This allowed me to collect a lot of dispersed information for troubleshooting
the X issues that have been plaguing me with a minimum of typing. I had used
it like:
2nv /var/log/mes* /var/log/Xorg.* /var/log/kdm.log
(the script name was 2nv (short for rsync to nirvana) but it allow chunks to
be thown into it and it would nicely rsync the wanted files to their
destination.
Script in good health :p
--
David C. Rankin, J.D.,P.E.
Rankin Law Firm, PLLC
510 Ochiltree Street
Nacogdoches, Texas 75961
Telephone: (936) 715-9333
Facsimile: (936) 715-9339
www.rankinlawfirm.com
More information about the arch-general
mailing list