Re: Bash file to variable string problem -- must be simple. What am I missing?
On Sun 03 Mar 2019 at 13:10:39 (-0500), deb wrote:
>
> On 3/3/19 2:43 AM, Thomas Schmitt wrote:
> > Hi,
> >
> > if spaces are involved, then quotation marks hould be put around the
> > argument of "echo".
> >
> > Using the leading blank from David Wright's post:
> >
> > $ fname=" long file with spaces.mp4"
> > $ x=`echo $fname | rev | cut -d. -f2 | rev`
> > $ test "$x".mp4 = "$fname" && echo IS EQUAL
> > $
> >
> > I.e. "$x".mp4 and "$fname" are not equal.
> > That's because the leading blank got lost in the "echo" run:
> >
> > $ echo "'$x'"
> > 'long file with spaces'
> >
> > Now with quotation marks around $fname to preserve the leading blank:
> >
> > $ x=`echo "$fname" | rev | cut -d. -f2 | rev`
> > $ test "$x".mp4 = "$fname" && echo IS EQUAL
> > IS EQUAL
> >
> > A similar effect would happen with double blanks inside the name:
> >
> > $ fname="long file with double spaces.mp4"
> > $ x=`echo $fname | rev | cut -d. -f2 | rev`
> > $ echo "'$x'"
> > 'long file with double spaces'
> > $ x=`echo "$fname" | rev | cut -d. -f2 | rev`
> > $ echo "'$x'"
> > 'long file with double spaces'
>
> Good thought Thomas!
>
> This also worked:
>
> $ fname=`echo "$fname" | rev | cut -d. -f2
>
> Now, the actual file did not have a leading (or trailing) space ---
> which I focused on.
>
> But IT DID have 2 adjoining spaces Within the filename.
Are you using a proportional font, by any chance, for working
on these scripts. Not a good idea.
The reason I picked on a leading space in my example was that the
eye is much more sensitive to embedded space than at the margins.
Sometimes I "lose" a file by renaming it, pasting the new name in
mc, which will preserve any accidental leading and trailing whitespace.
> Enclosing $fname in "", worked perfectly.
>
> That simple thing, that I thought I was missing, I would not have
> guessed at. Two adjoining spaces. :-)
Had the original string been pasted into the OP, like your error
message at the end, it would have been obvious to those of us using
fixed-width fonts. (BTW, asterisks are fine in the *text*, but
are no help elsewhere in the post, like in your "$fname".mp4**)
Cheers,
David.
Reply to: