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 06-10-2014, 07:01 PM   #1
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Rep: Reputation: Disabled
curl grep script help


Can someon help me correct this please, run it until successful, five time max. The curl & grep work fine but adding it into a for loop I am unsure of

[CODE]for i in 1 2 3 4 5
do
if curl -s "http://www.blah.com" | grep -A 1 "Username: blah" | tail -n 2| cut -f2 -d " " | cut -f1 -d '<' > file.txt; then
break
sleep 10
fi
done/CODE]

Last edited by bilbonix; 06-11-2014 at 05:06 AM.
 
Old 06-10-2014, 07:57 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
The main problem I see is that cut won't return a useful return value. I would split the command up. Grep will return a useful return value, so you could move the rest into the body of the if statement.
 
Old 06-11-2014, 01:25 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
where have you closed the loop?
where have you used i?
Are you sure you have found the text "Username: blah" without logging in the site?
 
Old 06-11-2014, 01:37 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,120

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
and the grep|tail|cut|cut chain can be replaced by a single awk script (which can also do the loop for you)
 
Old 06-11-2014, 05:08 AM   #5
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
The currl grep bit works fine no need to change that as it writes the username and password to the file as specified.

I just want to loop the command upto five times until successful, specifically until the curl command is able to connect.
 
Old 06-11-2014, 05:25 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,120

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
the curl & grep chain does not work well because you have not get the return code back. That means the if actually check the return code of the last command (cut - and of course, it will never fail).
So combining that chain into one single command will allow you to handle this action much easier. The other way can be to check ${PIPESTATUS[*]}: http://aplawrence.com/Blog/B955.html
 
1 members found this post helpful.
Old 06-11-2014, 07:55 AM   #7
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Okay, I see what you mean about the cut.

This works:

Code:
for i in 1 2 3 4 5 6 7 
do
        if curl -s "http://www.blah.com" | grep -A 1 "Username: blah"; then
           curl -s "http://www.blah.com" | grep -A 1 "Username: blah" | tail -n 2| cut -f2 -d " " | cut -f1 -d '<' > /opt/tmp/user.conf
		   logger -t "($(basename $0))" $$ blah password updated.... " $0${*:+ $*}."
           break
        else
            sleep 15
        fi
		
done
 
  


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
Bash script help with cURL Weekend_warrior1999 Programming 4 02-09-2013 07:32 AM
Using CURL command to login Pandora and grep text rexineffect Linux - Newbie 2 08-24-2012 11:20 AM
cURL: Server has many IPs, how would I make a cURL script use those IPs to send data? guest Programming 0 04-11-2009 12:42 PM
Archive Downloader Using Curl Grep Cygwin Nimbuz Programming 1 04-19-2008 08:53 PM
script to parse variables to curl script and execute morphix Programming 17 11-30-2007 02:27 AM


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