LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2011, 12:01 PM   #1
iniuria
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Bash: Repeat commands until a condition is matched


Hi everybody, i need to modify some scripts to repeat the commands in them until a variable returns a proper value. I need it to add some redundancy to some scripts i use to upload files to a remote server.

This is an example of a portion of those scripts:

Code:
################## site UPLOAD ##################

site_login=$(curl -c $site_cookie -L -F user=$site_user -F pass=$site_password -F submit=Login http://site.com/login.html)
site_page=$(curl -b $site_cookie -c $site_cookie http://site.com)

site_action=$(echo "$site_page" | grep "site.com/upload.php" | cut -f2 -d"\"")
site_progress_key=$(echo "$site_page" | grep "progress_key" | grep -o "value=\"[0-9a-zA-Z]*\"" | cut -f2 -d"\"")
site_usergroup_key=$(echo "$site_page" | grep "usergroup_key" | grep -o "value=\"[0-9a-zA-Z]*\"" | cut -f2 -d"\"")
site_UPLOAD_IDENTIFIER=$(echo "$site_page" | grep "UPLOAD_IDENTIFIER" | grep -o "value=\"[0-9a-zA-Z]*\"" | cut -f2 -d"\"")

post=$(echo "-F APC_UPLOAD_PROGRESS=$site_progress_key -F APC_UPLOAD_USERGROUP=$site_usergroup_key -F UPLOAD_IDENTIFIER=$site_UPLOAD_IDENTIFIER")


results_site=$(curl -L -b $site_cookie $post $site_action -F file[]='' -F file[1]=@$scenes_directory/$f -F submit=Upload)

site_link=$(echo "$results_site" | grep -o "http://site.com/files\/[a-zA-Z./0-9_-]*" | sed q)
Now i need to verify if the variable site_link has a proper value (it should match the regex http://site.com/files\/[a-zA-Z./0-9_-]*), and if it hasnt, reapeat the previous commands to retry the upload UNTIL a proper link is set.

Any idea on how to do it?

I guess a WHILE is needed, but i never used it, so i'm not sure on where to place it or how to write it.

Thanks.

Last edited by iniuria; 03-08-2011 at 12:15 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-08-2011, 04:55 PM   #2
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
An until seems to fit better into the logic of what your script is doing.

The general format of an until loop is:

Code:
 until test-commands; do consequent-commands; done
See http://www.gnu.org/software/bash/man...ing-Constructs


In your case I'd say you'll want to modify your script along the lines of:

Code:
site_link=""

until [[ "$site_link" != "" ]]
do
   results_site=$(curl -L -b $site_cookie $post $site_action -F file[]='' -F file[1]=@$scenes_directory/$f -F submit=Upload)
   site_link=$(echo "$results_site" | grep -o "http://site.com/files\/[a-zA-Z./0-9_-]*" | sed q)
done

You'll most likely want to wait some between your posts so you don't flood the server and possibly also include some other exit conditions like max number of tries.
 
2 members found this post helpful.
Old 03-08-2011, 06:55 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
OT - I was just wondering what value the 'sed q' gives at the end of site_link command substitution?
 
Old 03-09-2011, 05:51 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by grail View Post
OT - I was just wondering what value the 'sed q' gives at the end of site_link command substitution?
AFAICS it should be the first line of the stream, like head -1.
 
Old 03-09-2011, 09:54 AM   #5
iniuria
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks z1p, yours is a good solution. Everything changes if i output the result_site to a txt file, use a WAIT, and then read the site_link from that TXT, i willpost some exampla later.

@ grail and reuti

The sed -q was used because the regex returned two or more lines, while i just needed one of them.
 
  


Reply

Tags
bash scripting, loop



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] Problem with BASH conditionals - passing condition as variable smaddox Programming 8 09-18-2009 11:21 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
multiple if condition in bash tikit Programming 6 11-12-2008 01:34 PM
Script to repeat commands satimis Programming 8 08-12-2005 04:03 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 10:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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