You are not logged in.

#1 2011-01-17 20:04:52

jack.mitchell
Member
From: Ely, Cambridgeshire, UK
Registered: 2008-08-28
Posts: 156
Website

Quick Bash Question

I'm trying to write a simple bash script which downloads the compat-wireless drivers and installs them. The script works fine but i'm trying to learn bash and would like to make it a bit nicer to look at and add some more functionality. What I have done is used grep to pull out the percentage of the download, however every number prints on a new line like so:

1%
2%
3%
4%
5%
6%

etc.... What I'm trying to do is replace the last percentage with the next one, so it's all on one line. I've had a look around and found that a way to do what I'm trying to do is with:

tput cbt

or with the bash escape codes directly. Now the thing is I don't know where I should put this in my code, could anyone help?

#!/bin/bash

#Retrieve latest compat-wireless soruces
echo -e "Downloading sources\n" 
wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 |& grep -o "[0-9]\+%" 

if test -fs compat-wireless-2.6.tar.bz2 then
    echo "File Downloaded OK"
else
    echo "Download Failed, Exiting."
    exit
fi

#Untar folder
echo "Extracting sources" 
tar jxvf compat-wireless-2.6.tar.bz2 > /dev/null

#Enter build folder
cd compat-wireless-*

#Select ath9k as driver to build
./scripts/driver-select ath9k

#Compile sources
make

#Install sources
sudo make install

#Unload wireless modules
sudo make unload wireless

#Reload ath9k module
sudo modprobe ath9k

#Return to original directory
cd ..

#Remove traces of install files
sudo rm -r compat-wireless-*

Offline

#2 2011-01-17 21:07:32

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Quick Bash Question

I have no bash skills, so this is a shot in the dark, but try passing the -n flag to echo

man echo wrote:

-n     do not output the trailing newline

# edit: you could also take some pointers from here: http://mywiki.wooledge.org/BashFAQ/044


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2011-01-17 22:18:26

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: Quick Bash Question

You will want to echo a carriage return (\r) or a backspace character (\b).

$ echo -e "Hi\bello"
Hello

M*cr*s*ft: Who needs quality when you have marketing?

Offline

#4 2011-01-17 22:38:37

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Quick Bash Question

Using a carriage return is the right way to do this.

while read perc; do
  printf "\r%s%%" $x
done < <(command generating percentages)

Offline

#5 2011-01-18 12:26:18

jack.mitchell
Member
From: Ely, Cambridgeshire, UK
Registered: 2008-08-28
Posts: 156
Website

Re: Quick Bash Question

Ok, so I've tried this with no luck, I'm not sure where I should be putting it...

#Retrieve latest compat-wireless soruces
echo -e "Downloading sources\n" 
wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 |& grep -o "\r[0-9]\+%" 

As the command needs to be in the same execution cycle as the wget and grep, yes?

Last edited by jack.mitchell (2011-01-18 12:27:09)

Offline

#6 2011-01-18 13:27:39

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Quick Bash Question

You need to use "\r" with printf not grep :-)

Offline

#7 2011-01-18 15:46:22

jack.mitchell
Member
From: Ely, Cambridgeshire, UK
Registered: 2008-08-28
Posts: 156
Website

Re: Quick Bash Question

Ok, so I've tried

#Retrieve latest compat-wireless soruces
echo -e "Downloading sources\n" 
wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 |& printf "\r `grep -o "\r[0-9]\+%"`"

To no avail, am I doing this correctly, or at least on the right lines? I can't seem to find any examples where a command is used within a printf statement.

Thansk for the help so far!

edit: Just a thought, could I possibly encompass the grep command into a variable and then pipe it into a printf statement?

Last edited by jack.mitchell (2011-01-18 15:50:19)

Offline

#8 2011-01-18 15:52:41

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Quick Bash Question

Here's an example I use:

[karol@black ~]$ type stopwatch
stopwatch is a function
stopwatch () 
{ 
    zero=$(date "+%s");
    echo "Start!";
    while true; do
        now=$(date "+%s");
        time=$(($now - $zero));
        printf "\r${time}";
        sleep 0.2;
    done
}
jack.mitchell wrote:

Just a thought, could I possibly encompass the grep command into a variable and then pipe it into a printf statement?

Yes, exactly :-)

Last edited by karol (2011-01-18 15:57:38)

Offline

#9 2011-01-18 17:32:27

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: Quick Bash Question

Try this

while read p; do echo -en "\rPercent: $(grep -o '[0-9]\+%' <<< $p)"
done < <(wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 2>&1)
echo

Offline

#10 2011-01-18 18:14:52

jack.mitchell
Member
From: Ely, Cambridgeshire, UK
Registered: 2008-08-28
Posts: 156
Website

Re: Quick Bash Question

@rockinturtle

That works perfectly, I'll also have a go at implementing your method too karol for practice smile

Cheers all!

Offline

Board footer

Powered by FluxBB