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

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

Wow, a feature of C++ i did not know, ‘volatile’ methods. Well, i’d say extremely rare one, but still.

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”`
I wonder why memset is the only mean we have in libc in order to fill memory. We are limited to filling the memory with certain char. What if i want to use longer pattern? Is there anything like memset64? Generally, i’d like to fill the buffer with pattern which is 4 or 8 bytes long. I’m pretty sure, that’s the way memset is implemented anyway, for performance.
Maybe i’m missing something, but i was unable to find anything like this in the libc.
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
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).
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
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:
The desired result is:root a b c
ftp d e f
0 a b c(of course, user id of ‘root’ is 0 and of ‘ftp’- 14)
14 d e f
cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}'cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}' | bash
One more awk trick
. Let’s say you have a huge file. You only want to get all the lines of the file which are:
The solutions:
seq 1 30 | awk '/.*8.*/ { nextfile } { print $0 }'seq 1 30 | awk '/.*8.*/ { pr = 1 } { if (pr == 1) print $0 }'seq 1 30 | awk '/.*8.*/ { pr = 1; print $0 } { if (pr == 1) print $0 }'Short explanation: awk is instructed to look for regular expression (which is enclosed in ‘/’) and in (1) stops parsing the file before stopping each line is echoed as is. In (2) and (3) it sets pr variable to be 1 (it’s 0 by default and is short of “print”), and lines are echoed only if pr is 1.
Technorati Tags: awk, truncate file, cli, linux
Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers