Tricky BASH quoting question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tricky BASH quoting question
# 8  
Old 04-06-2013
How about this:-

There is always a back door...

EDIT:
Remember! I am a mere amateur, I don't code for a living...

Code:
Last login: Sat Apr  6 16:21:32 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ one="/Users/barrywalker/Desk*/"
Barrys-MacBook-Pro:~ barrywalker$ two="Empty Drawer/"
Barrys-MacBook-Pro:~ barrywalker$ three="1KH*"
Barrys-MacBook-Pro:~ barrywalker$ ls $one"$two"$three
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Function_Generator_OSX.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Function_Generator_OSX.py~
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Generator_OSX.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Generator_OSX.py~
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Wave.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Wave.py~
Barrys-MacBook-Pro:~ barrywalker$ > /tmp/txt
Barrys-MacBook-Pro:~ barrywalker$ ls $one"$two"$three >> /tmp/txt
Barrys-MacBook-Pro:~ barrywalker$ less /tmp/txt

Code:
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Function_Generator_OSX.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Function_Generator_OSX.py~
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Generator_OSX.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Generator_OSX.py~
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Wave.py
/Users/barrywalker/Desktop/Empty Drawer/1KHz_Sine_Wave.py~
/tmp/txt (END)

# 9  
Old 04-06-2013
@OP: Try:
Code:
 XML=$(<"$xmlLocation")

bash is the only shell where the quotes are is necessary in case the file name contains spaces for example..

In any other POSIX shell this should suffice:
Code:
XML=$(<$xmlLocation)

or at least:
Code:
XML=$(cat <$xmlLocation)

But
Code:
XML=$(cat "$xmlLocation")

There is a big difference between
Code:
"$xmlLocation"

and
Code:
$"xmlLocation"

--
Note:
To expand wildcards you could use a for loop for example..
Then you should be able to use one of the example above with the loop variable..

Code:
for file in blabla*/blabla
do
  printf "%s\n" "$file"
done


Last edited by Scrutinizer; 04-06-2013 at 03:37 PM..
# 10  
Old 04-06-2013
Quote:
Originally Posted by Scrutinizer
In any other POSIX shell this should suffice:
Code:
XML=$(<$xmlLocation)

That's a good point. Field splitting the word in a redirection operation not only violates POSIX, but it makes no sense (for the same reasons that field splitting is not performed on a variable assignment nor the word being tested by a case statement).

For the OP, here's a possible workaround. If space characters during field splitting are the only problem, the following should allow you to get passed it without using external commands or a subshell:
Code:
eval XML="\$(<${xmlLocation// /\\ })"

Regards,
Alister

---------- Post updated at 12:34 PM ---------- Previous update was at 12:31 PM ----------

And immediately after posting that, the following occurred to me. Duh!
Code:
IFS= XML=$(<$xmlLocation)

You may need to save the original IFS so that it can be restored afterwards.

Regards,
Alister

Last edited by alister; 04-06-2013 at 02:22 PM..
This User Gave Thanks to alister For This Post:
# 11  
Old 04-06-2013
Please learn from the following exercise:
Code:
xmlLocation="two words"
echo 'blabla' >"$xmlLocation"99
read XML <"$xmlLocation"*
echo "$XML"

echo 'two
lines' >"$xmlLocation"99
XML=$(cat "$xmlLocation"*)
echo "$XML"

This User Gave Thanks to MadeInGermany For This Post:
# 12  
Old 04-06-2013
@MadeInGermany:
In dash, you will get:
Code:
cannot open two words*: No such file

Also, what happens (when the shell does happen to expand wildcards in the variable in the redirect and) "$xmlLocation"* would expand to more than one file? The first would render and ambiguous redirect and the second would concatenate the content of those files and put that in a variable.

IMO this is not the place to use wild cards. it would be more reliable for example to use a for loop to expand wildcards and then use use the loop variable with proper quoting..
# 13  
Old 04-07-2013
Hopfully I will have time to work on this project after next Wednesday. I think Allister's ideas are sound (I was wondeing about IFS= myself at dinner last night) but have not has access to that machine over the weekend to try them out.

I also remembered octal ASCII excaping and dreamed up the following at dinner. Have not had time to try it out yet.
Code:
xmlLocation="${xmlLocation// /\040}"
XML=$(<$xmlLocation)

Mike

Last edited by Michael Stora; 04-07-2013 at 01:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Bash quoting

I am trying to write a BASH script that will prompt a user to enter a number of days, then calculate the date. My problem is the date command uses single or double quotes. For Example.. date -d "7 days" Here is an example of some same code I am trying to work through. echo "when do you... (4 Replies)
Discussion started by: javajockey
4 Replies

2. Shell Programming and Scripting

Quoting / interface question

Hi, My first shell script is one that prints the five largest directories in a given directory. My current effort is as follows, it gives me the output I'd like, but I have to quote a globbed pathname, which seems wrong: #!/bin/sh du -hs $1 | sort -rn | head -n 5 And I must invoke... (2 Replies)
Discussion started by: aardymir
2 Replies

3. UNIX for Dummies Questions & Answers

Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only... (6 Replies)
Discussion started by: Mordaris
6 Replies

4. Shell Programming and Scripting

bash: correct quoting with find and exiv2?

Hi, I need to embed a metatag to image files which contain qrcodes, i usually do this with exiv -M "set Exif.Image.DocumentName `zbarimg -q -Sdisable -Sqrcode.enable --raw image.tif`" image.tif which works fine. However I need to do this recursivly for whole directory and subdiretory... (4 Replies)
Discussion started by: mcframe
4 Replies

5. Shell Programming and Scripting

Another tricky sed or awk question

This post is in reference to https://www.unix.com/shell-programming-scripting/137977-tricky-sed-awk-question-post302428154.html#post302428154 I am trying to go the opposite direction now: I have the following file: a,b,C,f,g a,b,D,f,g a,b,E,f,g h,i,J,k,l m,n,O,t,u m,n,P,t,u m,n,Q,t,u... (3 Replies)
Discussion started by: awayand
3 Replies

6. Shell Programming and Scripting

Quoting issue: Trouble with bash strings in script

I can do this on the command line: sqsh -S 192.168.x.x -o tmp -U user -P fakepass -D horizon -C "\ select second_id from borrower where btype like '%wsd%' " I can also just leave the SQL at the end intact on one line .... ... However, when I throw this in a script like: $SQSH -o... (4 Replies)
Discussion started by: Bubnoff
4 Replies

7. Shell Programming and Scripting

quoting question

hi guys, i have a question related to quoting but i am not sure how to formulate it... lets say we want to simulate the following shell actions cd ~/project-dir ctags /home/work/folder1/*.sh /home/work/folder2/*.sh /home/work/folder3/*.sh so i make the following script buidtags.sh ... (2 Replies)
Discussion started by: aegis
2 Replies

8. Shell Programming and Scripting

BASH quoting behavior

The block below isn't a surprise:$ ls file1 file2 file3 $ x=* $ echo $x file1 file2 file3 $ echo '$x' $x $ echo "$x" * $But I found this block a bit bewildering:$ echo $x' >' * $I'm wondering why substitution wasn't performed on the $x, since it was unquoted (as far as I can tell).... (5 Replies)
Discussion started by: na5m
5 Replies

9. UNIX for Dummies Questions & Answers

Tricky Quotation Question

Hi, I am at a point in my script where I defined the number of the command line parameter I would like to set a variable equal to: parameter_number=14 I would then like to set a variable equal to the correct parameter: variable=$parameter_number The issue here is that {} is required... (2 Replies)
Discussion started by: msb65
2 Replies

10. Shell Programming and Scripting

Tricky script question

Hi, I'm in the midst of writing a UNIX script that sftp's files to an external host and am stuck with a problem. The problem is that the files created on my server as a order number that correlates to a sequence of directories on the remote host which is where the file should be ftp'ed. ... (3 Replies)
Discussion started by: budrito
3 Replies
Login or Register to Ask a Question