Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to do the following while loop using a bash script

file_size = 0
temp_size = 1
output_file
while (file_size != temp_size)
     temp_size = file_size
     download file
     append to output_file if possible
     file_size = sizeof(output_file)

Thanks!

share|improve this question

closed as unclear what you're asking by terdon, slhck Jul 3 '13 at 17:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Please read up on bash scripting, try something then post a question if you have specific problems. –  terdon Jul 3 '13 at 13:57
add comment

1 Answer

It's unclear what exactly you are looking for, but I've taken a shot in hopes that this will help you.

The following program needs to be placed in a text file. Let's say you name it dl. You need to change it's permissions with

chmod u+x dl

You run it by passing in the output file name you are looking for.

./dl output

It downloads the image file, at the url I hard coded in, over and over appending the output to the output file until the size of the output file is equal to or greater than the size that you specify in the temp_size variable.

#!/usr/bin/env bash

STAT="stat -c%s"
if [ $(uname) == "Darwin" ]; then
  STAT="stat -f%z"
fi

if [[ $# -ne 1 ]]; then
  echo "Usage: $0 output_filename"
  exit 1
fi

output_file="$1"
file_size=$($STAT "$output_file")
temp_size=1000000
download_file="http://www.google.com/logos/doodles/2013/franz_kafkas_130th_birthday-1976005-hp.png"
while [[ $file_size -le $temp_size ]]; do
  wget -O - $download_file >> "$output_file"
  file_size=$($STAT "$output_file")
done
echo $output_file is now $file_size bytes.

If you run it again with the same parameter, it will do nothing because the output file is already larger than the temp_size. You can reset it by giving it another file name, or removing the output file.

rm output
share|improve this answer
2  
Why did you rollback the env? Believe me, on this site you will get upvotes just for using that and it makes your script portable to any *nix like system. It is a good habit to get into. –  terdon Jul 3 '13 at 14:33
1  
You carefully enclosed the strings between double quotes but on some lines with output_file you forgot them which would disallow using filenames with spaces. –  pabouk Jul 3 '13 at 14:54
    
Thanks pabouk, it's corrected. –  Chad Skeeters Jul 3 '13 at 16:23
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.