LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2019, 07:50 AM   #1
nareshreddyn
LQ Newbie
 
Registered: Sep 2017
Posts: 10

Rep: Reputation: Disabled
Smile Use of double quotes in shell scripting


Hi All.

I am new to shell scripting,i need to understand the difference of lines below.

Below is the line used in shell scripting,
Code:
find /opt/app/${app} -name "\AutoTest*.log" -exec sh -c '>{}' \;
Code:
find /opt/app/${app} -name \AutoTest*.log -exec sh -c '>{}' \;

Files are listed as below
AutoTest.20140618-093457.log
AutoTest.20140618-095457.log
AutoTest.20140618-096457.log
AutoTest.Server1.log

Both are working.. i want to know the exact difference of using the double quotes and not using the quotes with an example if possible

Thanks a million in advance.
 
Old 07-24-2019, 08:01 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,674

Rep: Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734
Quote:
Originally Posted by nareshreddyn View Post
Hi All.
I am new to shell scripting,
Oh?? You've been asking about scripting for the past two years:
https://www.linuxquestions.org/quest...nt-4175613676/
https://www.linuxquestions.org/quest...us-4175614477/
https://www.linuxquestions.org/quest...pt-4175637514/
https://www.linuxquestions.org/quest...sh-4175638159/

After two years, how are you still a 'newbie'???
Quote:
i need to understand the difference of lines below. Below is the line used in shell scripting,
Code:
find /opt/app/${app} -name "\AutoTest*.log" -exec sh -c '>{}' \;
Code:
find /opt/app/${app} -name \AutoTest*.log -exec sh -c '>{}' \;
Files are listed as below
AutoTest.20140618-093457.log
AutoTest.20140618-095457.log
AutoTest.20140618-096457.log
AutoTest.Server1.log

Both are working.. i want to know the exact difference of using the double quotes and not using the quotes with an example if possible
Then you probably should post the rest of the script, since all you've posted is a single line. We have no idea what the rest of your script looks like/does with that. Running them from the command line doesn't yield surprising results, and the "${app}" you reference is meaningless in those lines posted.
 
Old 07-24-2019, 08:26 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Perhaps a more correct term intended was to say that they are a "novice" at shell scripting, OK?

I do not know the answer, my regex and/or shell language manipulations are all trial/error and if I really want to know the exact difference I do experiment with it a bit, attempt to look it up, and then might ask a question.

My suggestions here:
  • That is a find command. One line. You can run it on the command line by resolving the variable ${app} to something valid.
  • I'd try it with and without the \
  • I'd try it with single quotes, double quotes, and no quotes; expecting that one or more of those may not work, and also still use/not use the \
  • I'd also not perform the -exec as part of any of that, what this question is about, is how find treats the ... regex? The name it is searching for, for me having a lack of better term.
Once you learn a bit about how it behaves, I'd proceed with perhaps a more detailed question.

And by the way (I don't know), but the answer may be that in some case there is no difference.
 
1 members found this post helpful.
Old 07-24-2019, 08:48 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,742

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
Beware the double globbing mechanism

The 'find' used by OP uses a glob (or wildcard), not RegEx. It is recommended to use at least double quotes to prevent the Shell from expanding the pattern. Then 'find' will use its own globbing mechanism to interpret the pattern. If you have files that actually start with a backslash "\AutoTest*.log" then you will have to use three backslashes in the quoted pattern:

Code:
find -name "\\\AutoTest*.log" ...
This is necessary because some backslashes are lost due to the multiple globbing mechanism that are involved here.

Read all of this thread to get a better understanding of how 'find' and Bash's globbing mechanisms might interere with each other and how a previously "working" solution might unexpectedly backfire, depending on the filenames in the directory where you run the 'find' command. I do not want to reproduce all information in it again, it is all there. Post if you still have questions.

PS:

Your unquoted version
Code:
find /opt/app/${app} -name AutoTest*.log -exec sh -c '>{}' \;
only works by accident. If you run the unquoted command from within '/opt/app/${app}' where those logs reside, i.e.,
Code:
cd /opt/app/${app}
find . -name AutoTest*.log -exec sh -c '>{}' \;
you will get an error.

Last edited by crts; 07-24-2019 at 09:03 AM. Reason: typos, formatting
 
1 members found this post helpful.
Old 07-24-2019, 09:07 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,943

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
you might add/execute set -xv before this find and you will see what will be really executed (=how the shell interprets the command)
 
Old 07-24-2019, 12:52 PM   #6
ehartman
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 806

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Quote:
Originally Posted by nareshreddyn View Post
Hi All.

I am new to shell scripting,i need to understand the difference of lines below.

Code:
find /opt/app/${app} -name \AutoTest*.log -exec sh -c '>{}' \;
Here's a third one, without quotes, that should work too
Code:
find /opt/app/${app} -name AutoTest\*.log -exec sh -c '>{}' \;
The \ in front of the A doesn't do anything (\A is the same as A), but the wildcard in the filename has to be escaped, either by prefixing it with \ or by quoting the filename
(just like the ; at the end has to be escaped).

And are you really sure you want to execute those log files (sh -c_?
 
Old 07-24-2019, 01:38 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
I'd say it is not executing but overwriting with empty file-content.
 
Old 07-24-2019, 03:31 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,153

Rep: Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525Reputation: 525
...with GNU find. Note that {} with adjacent characters is not portable.
Portable is
Code:
find ... -exec sh -c '>"$1"' sh {} \;
WARNING: it trashes what it finds!

Last edited by MadeInGermany; 07-24-2019 at 03:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 09:02 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 PM
Double Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 10:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration