LinuxQuestions.org
Help answer threads with 0 replies.
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 06-10-2014, 06: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 04:06 AM.
 
Old 06-10-2014, 06:57 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 12:25 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
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, 12:37 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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, 04: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, 04:25 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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, 06: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



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 06:32 AM
Using CURL command to login Pandora and grep text rexineffect Linux - Newbie 2 08-24-2012 10: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 11:42 AM
Archive Downloader Using Curl Grep Cygwin Nimbuz Programming 1 04-19-2008 07:53 PM
script to parse variables to curl script and execute morphix Programming 17 11-30-2007 01:27 AM

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

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