LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-19-2004, 07:09 PM   #1
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
shell script backticks and vars?


I forget how, if at all, to put vars in my ``'s in a shell script.

Here's an example ...

Code:
#!/bin/sh
                                                                                
TW_REP_DIR="/var/lib/tripwire/report/"
HOST=localhost.localdomain
                                                                                
echo "Do you want to see latest report?"
read yn
                                                                                
if [ $yn = 'y' ]
then
        latest=`ls $TW_REP_DIR/$HOST-*.twr | tail -1`
        echo "latest=[$lastest]"
fi
The above code doesn't work because $TW_REP_DIR and $HOST are null I guess.
How can I use $ variables in my `` commands?

Thanks
 
Old 07-19-2004, 11:07 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
I just copied the code, and it works fine (sorta) for me. I don't have tripwire installed, so the ls doesn't work... There is a slight problem with the ls statement though... your path ends up having a double slash for the ls command... like so:

/var/lib/tripwire/report//localhost.localdomain

Anyways, like I said, it works on my system, because it gives the expected error.
Code:
$ ./test_script.bash 
Do you want to see latest report?
y
ls: /var/lib/tripwire/report//localhost.localdomain-*.twr: No such file or directory
latest=[]
If you want to see it written under a different style, I went ahead and re-wrote it. For the test I mentioned earlier, I used your original code.

Code:
#!/bin/bash

TW_REP_DIR="/var/lib/tripwire/report"   # Removed the trailing slash
HOST=localhost.localdomain

echo "Do you want to see latest report?"
read yn

# I always use {} for variable references... prevents any ambiguity
# and I always use the "${var}x" test to catch the chance that ${var} is null/empty
if [ "${yn}x" = "yx" ] ; then

  # The backtick pairs and $() do the same thing. I am trying to get in the
  # habit of using $() since I get lost easily if I have nested single/double quotes
  # with backticks
  latest=$( ls ${TW_REP_DIR}/${HOST}-*.twr | tail -1 )
  echo "latest=[${lastest}]"
fi
 
Old 07-20-2004, 03:05 AM   #3
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
To work or not as expect depends on your expectations. However, if you are trying to get the last based on creation date, you may be betrayed because of the sort order. The default is alphabetical and I don't know the filenames you expect to have. So, you may use the option "-t" of ls to ordinate it by the creation time.
 
Old 07-21-2004, 04:53 AM   #4
Goala
Member
 
Registered: May 2004
Location: Merida (Spain)
Distribution: Debian
Posts: 89

Rep: Reputation: 15
my contribution:

Code:
        latest=`ls -1tr ${TW_REP_DIR}/${HOST}-*.twr | tail -1`
 
Old 07-21-2004, 12:29 PM   #5
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Original Poster
Rep: Reputation: 15

I am surprise nobody caught my typo!
latest=`ls $VAR`
echo $lastest
I am so embrassed
 
  


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
Shell script inside shell script treotan Linux - General 4 02-19-2009 07:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM

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

All times are GMT -5. The time now is 10:41 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