Mailinglist Archive: opensuse (1599 mails)

< Previous Next >
[opensuse] supercool BASH input routine for files and directories with globbing without quoting input
  • From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Sat, 17 Oct 2009 02:23:20 -0500
  • Message-id: <200910170223.21134.drankinatty@xxxxxxxxxxxxxxxxxx>

Here is an off-the-wall one for the BASH guys. Many times I need to get
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
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 and
the inner tests whether the array element holds and directory or a filespec
with globbing. If so, then the fileglob is processed with 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


## 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}]}"

for b in $(ls ${CLIARRAY[${a}]}); do
echo "file: $b"


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
X issues 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

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
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups