Linux, programming, computers and life

May 5, 2009

getting file system size in linux

Filed under: programming, linux

A reminder to myself, statvfs allows you yo get an information about a mount point, it’s probably what df uses.

, ,

April 13, 2009

getting amount of total/free memory in linux

Filed under: programming, linux

While you can always parse /proc/meminfo, there’s system call sysinfo which does the trick.

, ,

March 9, 2009

sorting by certain columns in a file

Filed under: CLI, linux

I always knew that ’sort’ command has ‘-k N’ flag which tells to sort based on columns N,…. I just recently find out that this flag is even more versatile, and it can be used as ‘-k N,M’ which will sort based on columns N,…,M. As usuall, it’s in the man page, but i always missed it so far :) .

, ,

December 2, 2008

Where are Linux distros for Eee?

Filed under: life, linux

I’m disappointed by choice of Linux distributions for my EeePc (called pingu). I installed Ubuntu EEE there which works, however, because of specifically built kernel, i cannot find/compile modules for VirtualBox which i really need. It looks like main stream distributions are Mandriva (which is supposed to work, but i personally don’t like it), EeeDora & Ubuntu EEE. There are a lot of smaller distros which all work partly on the Eee and require tweaking in order to make them usuable.
While i use ArchLinux at home, it’s too much work to install it on the Eee and maintain it working, i’d like to have something more stable and less attention requiring.

I do not intend to whine about open source software quality and choice, because i very much appreciate the choice, freedom of it and free time that a lot of people devte to it. I’m looking for advice.
What are you using on your Eee ;) ?

Update: ubuntu eee (temporary name) 8.10 will be released 1.1.09. Extremely exciting news!

, , , , , ,

November 8, 2008

overflow in shell script

Filed under: CLI, programming, linux

Overflow in shell script… while it sounds strange, it actually happened to me :) . I have a script which does recoding of mp3 files in order to reduce bitrate and space those files take. I tried to be smart and first detect the bitrate in order to avoid redundant recoding. Well, my script actually worked for a long time, but then it refused to recode file with bitrate 256 ;) .
It was my fault of course. In order to determine the bitrate, i used the following ash function:
# function to determine bitrate:
# expects a file as parameter
determine_birate() {
    if [ -z “$1″ ]
    then
        return 0 # invalid
    fi
   
    if [[ “${MP3INFO}” == “” ]]
    then
        return 1 # invalid
    fi

    return `”${MP3INFO}” -p “%r” “$1″`
}

MP3INFO is an application i use in order to read mp3 files meta data. Well, when the bitrate was 256 i got 0 returned, i guess bash has some limit of 1 byte somewhere :) . My quick fix was (and now it works great):
# function to determine bitrate:
# expects a file as parameter
determine_birate() {
    if [ -z “$1″ ]
    then
        return 0 # invalid
    fi
   
    if [[ “${MP3INFO}” == “” ]]
    then
        return 1 # invalid
    fi

    echo `”${MP3INFO}” -p “%r” “$1″`
}
….
CURRENT_BITRATE=`determine_birate “$file”`

, , , ,

July 22, 2008

searching your organization address book from command line

Filed under: CLI, programming

Let’s assume that you are working in a relatively small organization, 4-5 hundreds people at most. I’m sure you have some kind of intranet and there’s a phone book there. I’m pretty sure you have a page with most used phone numbers pinned on your wall somewhere.
Here’s idea how to search those phones from command line. The solution is based on two scripts. First downloads the names list and formats it as necessary (mine strips all HTML mumbo-jumbo). This one is called periodically by cron. Second is used for names list search.

Here are the scripts:
update_tel_db
#! /bin/sh

cd /your/path/to/database
wget http://your.path.here/file.here >& /dev/null
touch tel.txt ; \rm tel.txt
links -dump file.here | grep @ | grep -v — ——————– | grep -v mailto | grep -vi ‘Click here’ | grep -v javascript | grep -v IMG | grep -v file: | tr ‘|’ ‘ ‘ | sed ’s/^ *//’ | sed ’s/ / /g’ | sed ’s/\[[0-9]*\]//g’ > tel.txt
\rm file.here >& /dev/null
touch tel.txt

tel
#! /bin/bash

echo ‘NAME DPT OFFICE CELL-PHONE HOME E-mail’
grep -i $1 /tour/path/here/tel.txt

Now telephone search becomes tel name on the command line.
Enjoy :)

July 20, 2008

adding command output to each line of file

Filed under: CLI, programming, awk

I wrote about changing of certain columns in a file, but here let’s solve a simpler task. I’d like to add something, let’s say date, to every line of a file. I’ll emulate a file using `seq 1 10`.

The following is easy enough:
seq 1 10 | awk '{print "echo `date` "$0}' | bash

However, there’s simpler solution:
seq 1 10 | while read ; do echo `date` ${REPLY} ; done

It works because REPLY variable is set to the line of input read by the read builtin command when no arguments are supplied (from bash manpage).

March 3, 2008

parallel execution in bash scripts

Filed under: CLI, programming, linux

Recently, i had a problem - i needed to run several utilities in parallel in bash script, wait till they finish and then gather the information they produced. It’s sooooooooo easy!
Bash has ‘wait’ command which… (surprisingly) waits for all (or certain) background processes to finish. Knowing that, the rest is easy.
Here’s a bash script which simulates utilities with ’sleep’ command.
nParallelTasks=10
nStartedTasks=0
for i in `seq 1 100`
do
echo "-- $i --"
sleep 5&
nStartedTasks=$(($nStartedTasks + 1))
if [[ "$nStartedTasks" == "$nParallelTasks" ]]
then
echo "waiting"
wait
nStartedTasks=0
fi
done

Technorati Tags: , , , , ,

February 17, 2008

one citation from bash.org.ru

Filed under: internet, linux

bash.org.ru is bash.org in russian :) . Here’s one citation i felt i must translate (original in russian):

<@insomnia> u have to execute three commands to install Gentoo
<@insomnia> cfdisk /dev/hda && mkfs.xfs /dev/hda1 && mount /dev/hda1 /mnt/gentoo/ && chroot /mnt/gentoo/ && env-update && . /etc/profile && emerge sync && cd /usr/portage && scripts/bootsrap.sh && emerge system && emerge vim && vi /etc/fstab && emerge gentoo-dev-sources && cd /usr/src/linux && make menuconfig && make install modules_install && emerge gnome mozilla-firefox openoffice && emerge grub && cp /boot/grub/grub.conf.sample /boot/grub/grub.conf && vi /boot/grub/grub.conf && grub && init 6
<@insomnia> that’s first.

Technorati Tags: , , , ,

February 14, 2008

awk + bash = power

Filed under: CLI, programming, awk

I wanted to call this “advanced awk”, but it’s in fact simple idea with awk and bash integration. I got this idea from wonderful ArchLinux forums.
The problem is as follows. Given a file with several columns, i want to run a command on some columns and keep other columns as is.
For example, given a file with user names and some more information, i need to convert user names to user ids and keep other information unchanged. Example file is:

root a b c
ftp   d e f

The desired result is:
0   a b c
14 d e f
(of course, user id of ‘root’ is 0 and of ‘ftp’- 14)
Well, there’s a way to do this :) . First we’ll convert the file using awk or sed to ‘echo’ commands and then use bash to interpret those.Take a look at the output of the following command:
cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}'
And now, only thing left to do is to execute the commands awk created for us - i.e. pipe to bash ;)
The final command is:
cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}' | bash

Enjoy!

Technorati Tags: , , , ,

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers