LinuxQuestions.org
Visit Jeremy's Blog.
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 07-22-2017, 01:17 PM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 334

Rep: Reputation: 8
Redirecting result of WGET to variable


I want to redirect output of wget command to variable. WGET call needs user/pass authentication. I use /bin/sh not bash.

this is a part of my script:

Code:
/usr/bin/wget -O --http-user=$user --http-password=$pass "http://localhost:$port/json.htm?type=command&param=getuservariable&idx=1"
and the result is creation of file named "--http-user=username" and output on screen like this:

Code:
--2017-07-22 20:08:35--  http://localhost:8855/json.htm?type=command&param=getuservariable&idx=1
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:8855... connected.
HTTP request sent, awaiting response... 401 Unauthorized

Username/Password Authentication Failed.
But if I run the same command without "-O" parameter, it create file with correct result, so authentication was successful.
So, how can I place result of WGET to variable instead file with correct authentication?
 
Old 07-22-2017, 01:33 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,900
Blog Entries: 3

Rep: Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584Reputation: 3584
The -O option always needs a filename. If you leave it blank then it will take the next string as a file name. So if you want to send to stdout, you need to specify the filename as a dash.

Then to capture that output into a variable, you can use command substitution.

Code:
html=$(wget -O - --http-user=$user --http-password=$pass "http://localhost:$port/json.htm?type=command&param=getuservariable&idx=1")

echo $html
 
Old 07-22-2017, 01:42 PM   #3
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 334

Original Poster
Rep: Reputation: 8
THX !!! It works!
 
  


Reply

Tags
authentication, shell script, variable, wget


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
redirecting a Command output to a Variable Ahmadhr Linux - Software 2 11-05-2013 04:31 AM
[SOLVED] Redirecting output to a file is giving wrong result ankitpandey Programming 11 12-13-2012 11:13 PM
Why does this not work when I want to have the result in a variable ? GerardM Linux - Newbie 8 06-11-2009 12:05 AM
Redirecting grep output to a variable dimako83 Linux - Software 6 11-17-2008 07:22 AM
mySQL redirecting query result to a .txt file! buttersoft Linux - Software 8 12-12-2003 03:05 AM

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

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