LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-22-2017, 01:54 PM   #1
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Rep: Reputation: Disabled
need help with bash script


I don't have a clue, which is why I'm here. The following must be a common situation that has an easy answer, but I don't know enough to be able to search for or understand potential answers, so please be explicit and patient...

I want to keep the discussion on an abstract level because I want to learn about general principles (i.e. "teach a man to fish") rather than get some particular script to work (as in "give a man a fish").

I have a bash script that contains a variable, lets call it ~cmd~ (I'm using the symbol ~ as a delimiter -- like begin, end). When I add ~echo $cmd~ to the script then stuff gets printed out to the xterm in which I run my script. If I cut and paste (using my mouse) the stuff back into the xterm, then the stuff gets executed as a command and produces output. I want to capture that output and assign it to a variable called ~result~ in my bash script and achieve that all within my script (so I don't have to cut and paste anything). There must be some general way to do that sort of thing, but what is it?

If there is not some general method which just always works, then perhaps there is some method for my particular situation (but I'ld prefer a general solution). In my particular case, what gets printed out is ~cat /tmp/foo | sed -n -e 's@.*"documentation at" location="\([^"]*\).*@wget -o - "\1"@p'~. When I cut and paste that into an xterm what happens is that ~wget -o - "http://myhome"~ is produced as output. I want that output to be assigned to the variable ~result~ in my bash script.

I tried ~result=$( $cmd )~ but that produces the error ~cat: invalid option -- 'o'~
 
Old 04-22-2017, 02:29 PM   #2
hopeless_n00b
LQ Newbie
 
Registered: Aug 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
I lucked into an answer...

~result=$(eval $cmd )~

Don't have a clue as to the errors involving cat, or why ~result=$( $cmd )~ is not a general solution, but I'm willing to accept that maybe nobody really knows how to explain to me what is going on so I should just be happy using things I don't understand so as to get things done...
 
Old 04-22-2017, 03:08 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051
i have been using something like this succesfully:
Code:
cmd="cat /some/file"
$cmd > /some/other.file
on recent bash versions.
 
Old 04-22-2017, 03:23 PM   #4
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,521

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
The tick "`" surrounding the command means "the output of the command".
 
Old 04-22-2017, 05:45 PM   #5
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
"I feel your ..." in #2 The magic search word is: bash command substitution here
And if you replace your ~ idea with [CODE]...[/CODE], you will get pretty like #3!

Code:
cat /tmp/foo | sed -n -e 's@.*"documentation at" location="\([^"]*\).*@wget -o - "\1"@p'

Last edited by !!!; 04-22-2017 at 05:53 PM.
 
Old 04-22-2017, 05:58 PM   #6
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
You would probably need to practice on variable assignments and expansion first. Lots of tutorials online that can help you.
 
Old 04-22-2017, 06:41 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
backticks are $(deprecated)
You can use 'em, for now...
Lions and Tigers and Bears, oh my!

TBH: In the last seven years, I recall using eval in one script.
A zenity - choice driven program launcher.

I don't understand it.
And it's my opinion that the use of "~" as a delimiter in a ~Variable~ may be a train wreck
waiting to happen.

Just sayin'

Last edited by Habitual; 04-22-2017 at 06:43 PM.
 
  


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] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

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

All times are GMT -5. The time now is 06:42 PM.

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