LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-10-2006, 08:27 PM   #1
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Rep: Reputation: 15
Question how to save output from a command to a variable?


I have a readline script. I want to use its output which by name is one line of a text file. how do I or even can I save this to a variable in a script file?

Thanks,
Tom
 
Old 04-10-2006, 09:32 PM   #2
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
I got it. play around long enough.

sh readline myfile 41 will read line 41. I'm doing a ls > myfile.

so, grep if `sh readline myfile 41` works.

the ` was the trick.

the goal is to do an ls * > myfile. from any directory. anyhow.
 
Old 04-10-2006, 10:38 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
10
Backtick--yes--good thing
Absolute best newbie---finds out answer before lumbering graybeards get around to it.
 
Old 04-11-2006, 07:10 AM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
An alternative to backticks in bash is $(cmd). I generally use backticks myself, but our work servers use and older version of bash, and I came across something that didn't work with backticks, but did with the $().
 
Old 04-13-2006, 03:48 PM   #5
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
yes, are nested backticks acceptable?

for instance...

n=`add $2 1`
then compare `sh readline $1 $2` and `sh readline $1 $n` is that where I might use $()?

readline is a head -nM and then a tail -n1 to get the Mth line. the file is a listing of files, no repeats. so this comparison is sort of like an end of file.

-Tom
 
Old 04-17-2006, 03:32 PM   #6
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
the finished product: th

lastline:
n=`sh add $2 1`
test `sh readline $1 $2` = `sh readline $1 $n`

readline:
# $1 filename
# $2 line number

head -n$2 $1 > temp1
tail -n1 temp1

grep*:

# $1 list of directories to start search
# $2 search pattern for grep


n=1

while ! `sh lastline $1 $n`
do
mydir=`sh readline $1 $n`
find $mydir -type f > filelistings
n=`sh add $n 1`
m=1
while ! `sh lastline filelistings $m`
do
myfile=`sh readline filelistings $m`
m=`sh add $m 1`
grep -H $2 $myfile
done
myfile=`sh readline filelistings $m`
grep -H $2 $myfile
done
# still have one directory to search!
mydir=`sh readline $1 $n`
m=1

find $mydir -type f > filelistings
while ! `sh lastline filelistings $m`
do
myfile=`sh readline filelistings $m`
m=`sh add $m 1`
grep -H $2 $myfile
done
myfile=`sh readline filelistings $m`
grep -H $2 $myfile
done
 
Old 04-19-2006, 03:04 PM   #7
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
this isn't homework, but it's hawkeye college, in waterloo, IA
 
  


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
passing command output to variable Infernal211283 Linux - Newbie 12 01-11-2006 05:54 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
Save last modified to a variable twantrd Programming 2 03-31-2004 03:57 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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