LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Repeat commands until a condition is matched (http://www.linuxquestions.org/questions/programming-9/bash-repeat-commands-until-a-condition-is-matched-867250/)

iniuria 03-08-2011 12:01 PM

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.

z1p 03-08-2011 04:55 PM

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.

grail 03-08-2011 06:55 PM

OT - I was just wondering what value the 'sed q' gives at the end of site_link command substitution?

Reuti 03-09-2011 05:51 AM

Quote:

Originally Posted by grail (Post 4283406)
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.

iniuria 03-09-2011 09:54 AM

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.


All times are GMT -5. The time now is 06:25 AM.