LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2013, 09:02 AM   #16
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286

First thing is that curly braces are not recommended for variables. Whenever you want to print or use a variable, you can simply use:
Code:
$variable_name
Second thing, look at:
Code:
2>&1 | tee -a ${LOG_FILE}
2>&1 would redirect standar error as well as standard output to some file (you mention filename after it). tee -a ${LOG_FILE} would append output of command to two files.

Third, though I am not sure, but the error of ambiguous redirect that you're getting could be because of ${ERROR_FILES}. What's there in ERROR_FILES? Are there two or more files saved in this variable? Else as I said earlier, both your find commands are working fine. So did you try to invoke the second command without redirecting its output to a file?
Code:
~$ find ${BASE_DIRECTORY} -path \*/error/* -type f -not -name "SS*" -not -name "AA*" -not -name "PQ*" -mmin +60 -print
Last, which shell you're using.. is it bash? Then which version?

Just go through this book: http://tldp.org/LDP/abs/abs-guide.pdf

@David the H: Where are you? Please take some time on this thread.
 
Old 05-03-2013, 11:52 AM   #17
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
@shivaa

Thank you for your input.

Well I learnt that always uses {} for variables so as to make them clearly visible/distinguish , this contradicts to your advice

ERROR_FILES only has name of files, that is absolute path of filename, that is it.
 
Old 05-03-2013, 12:08 PM   #18
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by sysmicuser View Post
ERROR_FILES only has name of files, that is absolute path of filename, that is it.
So what's result of:
Code:
~$ find ${BASE_DIRECTORY} -path \*/error/* -type f -not -name "SS*" -not -name "AA*" -not -name "PQ*" -mmin +60 -print
 
Old 05-03-2013, 12:22 PM   #19
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
@shivaa.

Result is
Code:
/h1/h2/ENFGateway/notifyVehicleResponse/error/abcd.dat
/h1/h2/RoutingGateway/RoutingPath/error/FAILED.dat
 
Old 05-03-2013, 12:28 PM   #20
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
@shiva

On the top of your head do you know what to do if I do not want to echo in the following loop. I forgot, Sir used to do something like ;; this but if I do it gives me error

Code:
if [[ $string == true ]]; then
echo -e "String is $string\n""
else
i don't want to print any message here and i don't know what to do
fi
[
 
Old 05-03-2013, 12:28 PM   #21
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Alright, then error is because of ${ERROR_FILES}. To redirect error to two files, use tee, as:
Code:
~$ find ${BASE_DIRECTORY} -path \*/error/* -type f -not -name "SS*" -not -name "AA*" -not -name "PQ*" -mmin +60 -print | tee -a ${ERROR_FILES} 
OR
~$ find ${BASE_DIRECTORY} -path \*/error/* -type f -not -name "SS*" -not -name "AA*" -not -name "PQ*" -mmin +60 -print | tee -a file1 file2
 
Old 05-03-2013, 12:39 PM   #22
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
@shivaa

If I use
Code:
set -o pipefail
Would be that of any use in my find command? I want the script to fail, should there be any trivial error.

Also I don't want the error to be redirected to two files but one. Also I have no idea when should I use 2>&1 ?

Last edited by sysmicuser; 05-03-2013 at 12:40 PM.
 
Old 05-05-2013, 01:51 PM   #23
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by shivaa View Post

@David the H: Where are you? Please take some time on this thread.
Thanks for the vote of confidence. Sorry it takes so long to come around, but have so many other brands in the fire now that I only have time and energy to come here every few days.

Frankly, I don't see all that much to comment on that hasn't been mentioned already. You seem to have done rather well on your own. I can only clarify what's been covered so far.

A lot of this does seem to be a problem with quoting. Quotemarks have a couple of functions. First, they indicate to the shell what should be considered a single, contiguous token, and second, it protects any shell-reserved characters from being interpreted, so that they get passed to the final command as-is.

(Actually, they're really the same, since whitespace has a special shell meaning.)

When you use double quotes around a variable, it protects the expanded contents from word-splitting and globbing expansion. This is especially important if it contains a filename.

As for globbing patterns, note that find does its own globbing, and you have to protect the globbing from the shell so that find gets it unaltered.

It's also possible to use backslash escapes for the same purpose, which is fine when you have only one or two characters to escape, but enclosing a longer string in quotes is usually easier to read and understand, since it creates a clear visual grouping. A good rule of thumb is simply to enclose the longest string possible, and to use single quotes with simple text and double quotes when there are variables or other substitutions involved.

As for the OP find command, I don't see anything really wrong with it, other than ensuring that the strings are properly protected.

Code:
find "$BASE_DIRECTORY" -path '*/error/*' -type f -not -name 'SS*' -not -name 'AA*' -not -name 'PQ*' -mmin +60 -print >> "$ERR0R_FILES"

In any case, it seems to me that the real key to the discussion is found in post #4, where the actual goal was apparently described; to find files that exist only in the '/input/' directories. For that, I think the -regex option would be a better choice, as simple globbing cannot limit the matches well enough.

Code:
find "$BASE_DIRECTORY" -type f -regex '.*/input/[^/]*'  -mmin +60 -print >> "$ERR0R_FILES"

Finally, as for brackets around variables, in the end it's really a matter of personal style, but I certainly recommend not using them when unneeded. They provide nothing but exercise for the fingers, while also cluttering up the code and make it harder to read, leading to an increased chance of mistakes. I went through a phase myself where I thought I should use them all the time, but after a while I realized it wasn't worth the extra effort.

See here for more on recommended formatting practice:
Scripting With Style


Re: post #20, just leave the else section out entirely (best), or use the ":" (true) command.

Re: post #22, pipefail just changes the behavior of the exit code produced when a pipeline of commands is run. I don't see any use for it here.

Edit: I almost forgot to add my standard links to how the shell processes arguments. When you fully understand the concept behind these half of your scripting problems should disappear.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Last edited by David the H.; 05-05-2013 at 01:57 PM. Reason: as stated + minor corrections
 
2 members found this post helpful.
  


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] Redirection question ZaphoidPA Linux - Networking 2 01-14-2013 02:08 PM
redirection question penguinlover2 Linux - Newbie 2 10-09-2012 08:27 AM
Redirection question mortonmorton Linux - Newbie 7 10-07-2009 01:55 PM
Redirection question ShaqDiesel Linux - General 3 01-06-2009 06:41 PM
Redirection question... Zeracles *BSD 0 02-21-2004 12:11 PM


All times are GMT -5. The time now is 04:18 AM.

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