LinuxQuestions.org
Help answer threads with 0 replies.
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, 01: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 01:15 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-08-2011, 05: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, 07:55 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,690

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

Rep: Reputation: 252Reputation: 252Reputation: 252
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, 10: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


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


All times are GMT -5. The time now is 05:11 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration