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.

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
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.7.11.1706\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Super User works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscrip"], "copyright__copyright__copyright": ["ize -le $temp_size ]]; do\n  wget -O - $download_file >> \"$output_file\"\n  file_size=$($STAT \"$output_file\")\ndone\necho $output_file is now $file_size bytes.\n\nIf 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.\n\nrm output\nshare|improve this answer\nadd comment\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}