/home

Icon

Informations, tips and technics.

Overwrite a console line in Bash

Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

#!/bin/bash
tput sc
echo -n “Working…”
for i in `seq 1 5000`;
do
echo $i > /dev/null
done
tput el1
tput rc
echo “Finished!”

As you can see, I use the tput command. Bascally, I use the echo -n command to display the first line of text, the -n parameter to prevent a carriage return (new line), then I save the current line/column position using tput sc. After that, I do some stuff, and then I clear the line on the left using tput el1 and finally restore the position of the line/column using tput rc. I can then write the line that I want, the previous one has been deleted.

Update

Progress bar example.

I have found this example on the net and here is the code. I could help you to acheive the same goal.

ref: http://tldp.org/LDP/abs/html/assortedtips.html#PROGRESSBAR

ref: http://forum.soft32.com/linux/echo-line-overwrite-ftopict470692.html

#!/bin/bash
# progress-bar.sh

# Author: Dotan Barak (very minor revisions by ABS Guide author).
# Used in ABS Guide with permission (thanks!).

BAR_WIDTH=50
BAR_CHAR_START=”[”
BAR_CHAR_END=”]”
BAR_CHAR_EMPTY=”.”
BAR_CHAR_FULL=”=”
BRACKET_CHARS=2
LIMIT=100

print_progress_bar()
{
# Calculate how many characters will be full.
let “full_limit = ((($1 – $BRACKET_CHARS) * $2) / $LIMIT)”

# Calculate how many characters will be empty.
let “empty_limit = ($1 – $BRACKET_CHARS) – ${full_limit}”

# Prepare the bar.
bar_line=”${BAR_CHAR_START}”
for ((j=0; j<full_limit; j++)); do
bar_line=”${bar_line}${BAR_CHAR_FULL}”
done

for ((j=0; j<empty_limit; j++)); do
bar_line=”${bar_line}${BAR_CHAR_EMPTY}”
done

bar_line=”${bar_line}${BAR_CHAR_END}”

printf “%3d%% %s” $2 ${bar_line}
}

# Here is a sample of code that uses it.
MAX_PERCENT=100
for ((i=0; i<=MAX_PERCENT; i++)); do
#
sleep 1
# … Or run some other commands …
#
print_progress_bar ${BAR_WIDTH} ${i}
echo -en “\r”
done

echo “”

exit

Filed under: Bash, Linux

Checking OS version on Linux or Unix

I work right now on a bash script utility and I have tried to find a way to check what is my system. The goal of the script is to runs some commands and can be run on RedHat, Suse Linux and Solaris.

I have use this code :

for base in `ls /etc/*release*` ; do
    if [ -f “$base” ]; then
        s=”$base”
    fi
done
z=$(cat $s | tr [a-z] [A-Z])
id=${z// /}
cri=”SOLARIS SUSE REDHAT”
for critere in $cri; do
    rd=`expr match “$id” “$critere”`
  if [ ! “$rd” -eq 0 ]; then
            systeme=”$critere”
        fi
done

Assuming that you have a release file in /etc then it should works.

If you are going to ask me why I do not use a findfunction to retrieve all the files in /etc it is because the version of findin Linux and Solaris does not have the same parameters and I can’t use the-maxdeph 1 method to search only in /etc.

So I ended up using an ls function and then test is the result is really a file or not (I do no manage folders and symbolic links). If it is then I can proceed some checks.

Maybe not the best code to know what can of the system the script in running onto but well, it works. But feel free to comment!

Filed under: Bash, Linux

Managing Compiz Fusion in OpenSuse 10.3

I like this distribution because this is the one I have started to learn Linux.

Those days, the beta 3 has been released but still I found frustrating the complex way you have to go to simply enable Compiz and set its options.

Enabling compiz is not so difficult, even if I think that there could be an icon somewhere in the control panel, but where are the options and how can you configure them?

Well, I have found an article that explain how to do this and use the ccsm which is the CompizFusion control panel. I have not yet tested it but you can find some infos here :

 http://news.opensuse.org/?p=167#more-167

I will post an update on this as soon as I have tested it.

Update

Well, the one click install system work well (as soo as you download the file on your system and then execute it) and I have then access to the ccsm utility.

But because I have an ATI 9800 Pro video card, I can’t get the 3D acceleration works on my system and the fglrx driver version 8.40.4 does not install well on beta 2. So no XGL for now.

I will wait for the latest ATI drivers (the 8.41.x I think) that should be released in the coming weeks to retest this utility.

Filed under: Linux

Copy/paste in Windows cmd: quick edit mode

Even If I have worked with Windows XP or Windows 2000-2003, there is one thing that anoying me a lot: thecopy and paste operations in the dos prompt (cmd).

Usually, you have to go to the right clik menu to select and then copy some text in your DOS windows to paste some lines elsewhere.

Well, there is an easier way to do a copy paste operation in dos under Windows, the quick edit mode!

You can activate it in the right click menu of the current command prompt window.

Just go to the properties menu and then check the option QuikEdit Mode 

When enable, to simply have to select your text with your mouse pointer and then hit Enter to copy it. Then you can paste it everywhere you want using CTRL+V or the menu of your application.

Simple but I forgot this option exists …

Filed under: Windows, , , ,

Checking memory on linux with simple scripts

I have found those little scripts very usefull to check memory on a linux system.

Check those scripts here : http://www.oreilly.fr/contenu/2007/04/26/surveiller-la-consommation-m%C3%A9moire-sous-linux

Filed under: Bash, Linux, , , ,