Results 1 to 6 of 6
Basically, I have a program that will quite likely take multiple arguments with multiple spaces in them. These arguments are directory names, and as such when using bash the spaces ...
- 08-08-2009 #1
BASH: Process arguments with escaped spaces
Basically, I have a program that will quite likely take multiple arguments with multiple spaces in them. These arguments are directory names, and as such when using bash the spaces are escaped with '\'.
I use a for loop to process each directory argument, and since they have spaces I use $@ instead of $*. Adding debug echoes show that this indeed works, each argument being extracted from the list. The problem is that for some reason, the argument is being extracted without escaped spaces, and as such the rest of the program will not work.
and $DIRARG has unescaped spaces in it.Code:for DIRARG in $@ do # some stuff done
I am wondering if there is anyway to get it to keep the escapes in there so that the rest of the functions can get it.
I am not entirely sure of my general program method either, so here is some code scrawl if anyone wants to give pointers; be them on my specific problem, or on a better way to get this done. The reason I am writing this is so that I can easily set up playlists with mplayer.
Will result in a list of movements being passed to mplayer. I would think there would be a command to do this, but I didn't find anything.Code:mplayer enum music/rec/Johannes\ Brahms/Clarinet\ Quintet\,\ op.\ 115/
This was not the original state of the code, but a result of me trying to isolate the problem. The security vulnerability comments are due to the fact that OUTFILE or OUTDIR might already have something in them, and I don't check if they are empty because I was lazy and didn't want to write to routines to initially fill or to append to them.Code:#!/bin/bash ## # enum -- enumerate file paths to stdout # # usage -- enum [DIRS] # # notes -- really messy! # # written -- 8 August, 2009 by Egan McComb # # revised -- ## ERR_NARGS=3 ERR_VARGS=5 LISTCOMMAND="ls --quoting-style=escape" SAVEIFS=$IFS IFS=`echo -en "\n\b"` usage() { echo "Usage: $0 [DIRS]" >&2 } chkargc() { if [ -z $1 ] then echo "Error: Too few arguments" >&2 usage exit $ERR_NARGS fi } chkargs() { if [ ! -d $1 ] then echo "Error: Invalid directory '$1'" >&2 usage exit $ERR_VARGS fi } listdir() { echo `eval $LISTCOMMAND $*` } catpath() { DIRPATH=$1 shift for FILEARG in $@ do FILEPATH="${DIRPATH}${FILEARG}" # XXX: Security vulnerability? OUTFILE="$OUTFILE $FILEPATH" done echo $OUTFILE } enumerate() { echo $* LIST=`listdir $*` echo `catpath $* $LIST` } ##----MAIN----## chkargc $* # XXX XXX XXX: DIRARG does not retain escapes! for DIRARG in $@ do chkargs $DIRARG # XXX: Security vulnerability? OUTDIR="$OUTDIR `enumerate $DIRARG`" echo $OUTDIR done IFS=$SAVEIFS exit 0
Any advice or insight is greatly appreciated.
- 08-09-2009 #2
You are almost right! The problem is that for $@ to keep its special properties, it must be _quoted_ (and as a note, you should almost always quote your variables).
Therefore, try this instead:
Now, $DIRARG will not have its escapes, but that's okay! You simply need to quote the variable whenever you use it: this accomplishes the same thing as escaping. So, for instance, say:Code:for DIRARG in "$@" do chkargs $DIRARG # XXX: Security vulnerability? OUTDIR="$OUTDIR `enumerate $DIRARG`" echo $OUTDIR done
instead of:Code:enumerate "$DIRARG"
Does this make sense?Code:enumerate $DIRARG
DISTRO=Arch
Registered Linux User #388732
- 08-09-2009 #3
That does indeed make sense, but I still cannot get it to work...

The more I fiddle with this, the more convoluted it gets. Does anyone have a simple solution to the problem this program sets out to solve. Maybe I will come back with fresh eyes to rewrite the whole thing with quoting in mind.
Thanks for the help.
- 08-09-2009 #4
Sure enough I may have found something to invalidate this whole thing, but unfortunately I am stuck in Windoze at the moment so I cannot test. Would:
work?Code:mplayer music/rec/Béla\ Bartók/Piano\ Concerto\ No.\ 1/*
I think that bash does the file globbing before it even passes the argument to mplayer?
- 08-11-2009 #5Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
@egan, try to use quotation marks for $@ and command line argument. Take the following bash script for example (let's name it prog1):
To list a directory (with or without spaces), call the script this way:Code:#!/bin/bash echo "listing files under $@" ls "$@" -l
Ref: How to iterate over arguments in bash script - Stack OverflowCode:prog1 "my directory 1"
- 08-11-2009 #6
Thanks for the help. I will keep in mind if I need to write a program that takes spaces... the program I was writing is usesless now that I confirmed that globbing works fine (for some reason it looked like I needed to write a program the last time I tried...)


Reply With Quote
