I just found this great wget wrapper and I'd like to rewrite it as a python script using the subprocess module. However it turns out to be quite tricky giving me all sorts of errors.
download()
{
local url=$1
echo -n " "
wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
echo -ne "\b\b\b\b"
echo " DONE"
}
Then it can be called like this:
file="patch-2.6.37.gz"
echo -n "Downloading $file:"
download "http://www.kernel.org/pub/linux/kernel/v2.6/$file"
Any ideas?
Source: http://fitnr.com/showing-file-download-progress-using-wget.html
vim download.py
then you can call like:
I think you're not far off. Mainly I'm wondering, why bother with running pipes into
grep
andsed
andawk
when you can do all that internally in Python?If you are rewriting the script in Python; you could replace
wget
byurllib.urlretrieve()
in this case:Example:
It downloads the url to a file. If the file is not given then it uses basename from the url.
You could also run
wget
if you need it:I have not noticed any buffering issues with this code.
I've done something like this before. and i'd love to share my code with you:)
so, basically, you just need to modify the initial_cmd's value, in your case, it's
this script will first create a temp file, then put shell commands in it, and give it execute permissions. and finally run the temp file with commands in it.
In very simple words, considering you have
script.sh
file, you can execute it and print its return value, if any: