Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Below is a script I've wrote to backup my routers and adsl modem, Any ideas to compact this script in less lines?

Each router have a different kind of structure as you can see.

date="$(date +%d-%m-%y)"

mkdir $local
cd "$local"
echo "Iniciando Backup"

for http_txt_for in $http_txt; do
    echo "$http_txt_for"
    wget --auth-no-challenge --user=$login --password=$pass $http_txt_for/config.dat -O $http_txt_for.dat

for cgi_for in $cgi; do
    echo "$cgi_for"
    wget --user=$login --password=$pass $cgi_for/cgi-bin/export_settings.cgi -O $cgi_for.dat

for http_for in $http; do
    echo "$http_for"
    wget --user=$login --password=$pass $http_for/rom-0 -O $http_for

cd ..
tar czf $archive_file $local
#rm -R $archive_file $local
echo "Backup Finalizado"
share|improve this question
This should probably be on CR.SE – l0b0 May 4 '14 at 21:32
I'm sorry for posting in wrong place. – André M. Faria May 5 '14 at 13:27
up vote 1 down vote accepted

Some possibilities:

  • You don't need to loop if the variable only has a single value.
  • Use verbose mode instead of echoing debug information. echos like this are like comments: They aren't code and they may get obsolete. Printing what's actually going on is much more useful for debugging.
  • You can use wget --output-document=/path/to/file to avoid cding before wgetting.
  • You can use tar -C .. to go to the parent directory before doing the tar operation without cd.
  • Why create a directory and a file name with the date? You only need a date indicator in the tarball name. The canonical solution for this sort of thing is to

    tmp="$(mktemp -d)"
    trap -- 'rm -frv -- "$tmp"' EXIT
    cd -- "$tmp"

    and store the temporary files there. This also removes the need for the previous two tips.

  • If you only need the date once, you can inline $date.
share|improve this answer
Thanks for your suggestions, I will edit my script and after I will post it in main topic, The Ip was a example, I will use more than one IP for variable. – André M. Faria May 5 '14 at 14:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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