Bash error when using quote


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash error when using quote
# 1  
Old 12-19-2014
Bash error when using quote

Hello.
Someone can it tell me why the following syntax is incorrect. And so how to treat the filenames with spaces.

Code:
linux:~ # ls "/usr/NX/var/log/*.log"
ls: cannot access /usr/NX/var/log/*.log: No such file or directory
linux:~ #

But this one is correct.


Code:
linux:~ #  ls /usr/NX/var/log/*.log
/usr/NX/var/log/nxd.log  /usr/NX/var/log/nxerror.log  /usr/NX/var/log/nxserver.log
linux:~ #

From a bash script:
Code:
CMD="cp -v    \"/$MY_DIR_5_2/*.log\"  \"$MY_ROOT_DIR/$MY_DIR_5_2\" "
echo "COMMAND_5 : $CMD"
eval $CMD

After variables expansion :
Code:
linux:~ # cp -v    "/usr/NX/var/log/*.log"  "/root/bin/202_nomachine_4/000_backup_current_config/2014-12-18__12-29-04/usr/NX/var/log"
cp: cannot stat ‘/usr/NX/var/log/*.log': No such file or directory
linux:~ #

If either the directory source or destination contain space, I must use quote ?

Any help is welcome.
# 2  
Old 12-19-2014
Quote:
Originally Posted by jcdole
...
If either the directory source or destination contain space, I must use quote ?
....
Yes.
* is handled as shell expansion, but if you put it into quotes, it might be taken literary (as in: just as the char it is)
This is also true for REGEX, if you put them into quotes, they will fail.

There are a few exceptions to this:
Code:
for ELEMENT in "${ARRAY[*]}"
do
   echo "$ELEMENT"
done

Here, the quotes around the array would be required, if any of the arrays elements contains a space.

In some occasions it is easier to make a list-variable first.
Code:
# Defines the variable 'list' as an array
declare -A list   
# Fill the array, exit if it fails
list=$(echo /var/log/*log) || exit 1 
# Copy the files
cp -v "${list[*]}" "/target/dir"

Alternativly, you can create an array also like these:
Code:
array1=( one two "t h r e e" four)

array2=( $(ls /var/log/*log) )

array3[0]=one
array3[1]=two
array3[2]="t h r e e"

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 12-19-2014
wildcards in quotes don't expand.

Fortunately, you don't have to quote the entire string.

Code:
command "big long path with spaces/"*.ext

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-19-2014
Quote:
Originally Posted by Corona688
wildcards in quotes don't expand.

Fortunately, you don't have to quote the entire string.

Code:
command "big long path with spaces/"*.ext

Do you mean that
Code:
cp "big long source path with spaces/*.ext   big long dest path with spaces

will work ?
# 5  
Old 12-19-2014
That's exactly what I don't mean. If it contains spaces, it must be quoted (or escaped) but you can have parts of the same string inside and outside quotes. You can consider the " character to mean turn off splitting until another " is found, rather than "enclose one literal, individual string".

Code:
"this is quoted"this_is_not"this is"this_is_not"but all the same string"*.txt

So your command would be more like:

Code:
cp "big long source path with spaces/"*.ext   "big long dest path with spaces"

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 12-19-2014
Quote:
Code:
ls "/usr/NX/var/log/*.log"

Obviously, the wildcard/star * won't expand within quotes...

You could try
Code:
ls "/usr/NX/var/log/"*.log

also try
Code:
ls "/dir/with/s p a c e s/log"*.log # note this should list the files, you can't simply substitute ls with cp.

This should work if the source directory and/or source files have no space in the name:
Code:
CMD="cp -v    \"/$MY_DIR_5_2/\"*.log  \"$MY_ROOT_DIR/$MY_DIR_5_2\" "

It should work too if the source directory has spaces in the path, but you keep the quotes around it.

If the source directory and/or source files have space in the name, try this (untested)
Code:
CMD="ls \"/dir/with/s p a c e s/log\"*.log | while read file; do cp -v \"$file\" \"$MY_ROOT_DIR/$MY_DIR_5_2\"; done "

Hope this helps.
This User Gave Thanks to junior-helper For This Post:
# 7  
Old 12-19-2014
Quote:
Originally Posted by jcdole
From a bash script:
Code:
CMD="cp -v    \"/$MY_DIR_5_2/*.log\"  \"$MY_ROOT_DIR/$MY_DIR_5_2\" "
echo "COMMAND_5 : $CMD"
eval $CMD

Congratulations, you have found a bad script.

Seriously, don't do that. Don't put quotes inside quotes. They are a huge amount of trouble to get back out. Put the programming in program, and the things that vary, inside the variable, and you will have far less trouble and people will be able to understand what your code is meant to do without a debug trace.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

A terrific quote:

Food for thought! """ Everybody in this country should learn to program a computer...because it teaches you how to think. """ Steve Jobs. So, SO, true! Merry XMAS all... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replacing Double Quote in Double Quote incsv file

Hi All , We have source data file as csv file and since data could contain commas ,each attribute is quoted into double quotes.However problem is that some of the attributa data also contain double quotes which is converted to double double quote while creating csv file XLs data : ... (2 Replies)
Discussion started by: Shalini Badal
2 Replies

4. UNIX for Dummies Questions & Answers

To quote or not

My question is, "Do I not understand, or is my information out of date?" I am trying to just be a student, rtfm'ing. I am working on my work systems. Is it simply that the book was printed in 2002 and a lot has changed since then, or did I miss something? Working in Korn Shell. I have been... (5 Replies)
Discussion started by: g.j.huebschman
5 Replies

5. Shell Programming and Scripting

quote problem

Hi Everyone hwo are you i have one file "abc.txt" which contains record like this "1","2","3" "3","4","5" now i want to change above 2 to 5 so i write the code as awk ' BEGIN{FS=","} { if(NR==1) { print $1",5","$3 } else { print $0 } ' abc.txt but the outpute is "1",5,"3"... (5 Replies)
Discussion started by: aishsimplesweet
5 Replies

6. Shell Programming and Scripting

replacing a quote in some lines with multiple quote fields

i want to replace mistaken quotes in line starting with tag 300 and relocate the quote in the correct position so the input is 223;25 224;20100428064823;1;0;0;0;0;0;0;0;8;1;3;9697;18744;;;;;;;;;;;; 300;X;Event:... (3 Replies)
Discussion started by: wradwan
3 Replies

7. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

8. Shell Programming and Scripting

double-quote inside double-quote

hey all, i made a simple .sh like this: echo "<style media="screen" type="text/css">@import url("main.css");</style>" but the output is: <style media=screen type=text/css>@import url(main.css);</style> i want to keep double-quotes, can anyone help me? thanks (3 Replies)
Discussion started by: indraf
3 Replies

9. Shell Programming and Scripting

Capturing Data between first quote and next quote

I have input file like RDBMS FALIURE UTY8703 'USER_WORK.TEST' .HIghest return code '12' I want to parse data which comed between first quote till next quote USER_WORK.TEST can you please suggest how to do that (4 Replies)
Discussion started by: scorp_rahul23
4 Replies

10. UNIX for Dummies Questions & Answers

Error : no closing quote in unix

Hey I was stuck in middle of writing this script Can any one help me out of this I am writing this script to delete the log files certain days old and everything fine but when I run the script I always see error at the bottom ./PurgeOneParameter.ksh: no closing quote and for the last if... (4 Replies)
Discussion started by: pinky
4 Replies
Login or Register to Ask a Question