LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-14-2013, 03:56 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
which quotes and brackets to use within a ()


Hi,

I have a variable - lastfile which I need to set to the last modified tar file in a directory, named $PROJECTNAME.

The script below works without $PROJECTNAME just fine, but how can I include it as the filename within the ()?

lastfile=$(find . -type f -name '${PROJECTNAME}*.tar.gz' -printf '%p\n' | sort -nr | head -n 1 | sed 's/^..//')

Last edited by Batistuta_g_2000; 03-14-2013 at 04:01 AM.
 
Old 03-14-2013, 04:07 AM   #2
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Batistuta_g_2000 View Post
Hi,

I have a variable - lastfile which I need to set to the last modified tar file in a directory, named $PROJECTNAME.

The script below works without $PROJECTNAME just fine, but how can I include it as the filename within the ()?

lastfile=$(find . -type f -name '${PROJECTNAME}*.tar.gz' -printf '%p\n' | sort -nr | head -n 1 | sed 's/^..//')
Got it:

lastfile=$(find . -type f -name "$PROJECTNAME*.tar.gz" -printf '%p\n' | sort -nr | head -n 1 | sed 's/^..//')
 
Old 03-15-2013, 08:21 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

"$(..)" command substitution acts as a sub-shell. That means it's its own execution environment. Everything inside runs exactly like a command written on the main command line.

As for quoting, it's very important to understand how the shell processes them and arguments. See here:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


By the way, head isn't needed if sed is also being used:

Code:
... | sort -nr | sed -n '1s/^..//p')
If you don't need recursive searching, however (or perhaps even if you do), there are safer ways to get the final file in a list, generally by using an array. It may even be as simple as this:

Code:
files=( "$PROJECTNAME"*.tar.gz )
lastfile=${files[-1]}	#or "${files[0]}" depending on which end you need to take it from.
This one relies on the shell being able to naturally sort the files. It's highly recommended to ensure that any numbers in the filenames are always zero-padded, and that any dates in them use the proper, standardized ISO 8601 format, to allow the shell to automatically sort them for you. Otherwise you're back to using sort or some other technique.


How can I find the latest (newest, earliest, oldest) file in a directory?
http://mywiki.wooledge.org/BashFAQ/003

How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099
 
  


Reply



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
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 11:17 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 03:03 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 > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:02 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
Open Source Consulting | Domain Registration