Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning command returns to a variable

Status
Not open for further replies.

hubud

Instructor
Oct 18, 2002
123
GB
Hi there, new to unix and all that using solaris 2.8 bash.

I constantly have to vi the last file in a large list of log files following ls -rt. This means highlighting and pasting the selection what a pain. I've tried to put together a script that will find the pwd, run ls -rt | tail -1 and then pass this onto the vi command to open the file automatically.

I was going to alias this to something like 'alias lf="ksh lastfile.ksh"'

My problem at the moment is that I can't seem to find a way to return the result of ls -rt | tail -1 to a variable that I can pass to vi command within the file.

In fact I want to pass the value of pwd to a variable in order to cd to the current directory in order to perform the ls -rt | tail -1.

How do you go about doing this

Any ideas on this one.



cheers

simmo
 
Hi Simmo,
have you tried:

CURDIR=`pwd`
LAST_LOG_FILE=`ls -rt|tail -1`

?

Bye
Gia
 
I think the syntax that you are looking for is....

alias lf='vi $(ls -tr | tail -1)'
 
cheers Ygor,

thats perfect. Does the $ denote to ls -rt in the pwd?

cheers

simmo
 
Why bother with a variable? You can always
just run the command

vi `ls -rt | tail -1`

to edit the most recent file in a directory.

If that's all you want to do you could set
that up as a script or even as an alias.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top