ru Русский

Reticularium

NETWORKS PLACE

Well, quite self-explanatory. Only Bash and Ruby. Options “folder” and “name” are examples, replace them with what you need… Shared in hope this can help someone.

Read the article

The subject is extremely annoying. I am not that lazy to add a comment symbol myself. This would be a good idea though, if it wasn’t sometimes needed to insert blocks of code via clipboard. With auto-comment on, if I insert

some line
# some comment
some line

I get

some line
# some comment
# some line

because there is a CR at the end of commented line and the auto-comment function adds the # symbol to the next line automatically.

The real problem begins when you try to insert many lines with some of them commented. You end up with each line after the first comment commented, the next comment commented twice so the next lines all commented twice and so on.

The solution:

Add this to your vimrc file:


au FileType * setl fo-=cro

where:

  • au = autocmd
  • FileType * – any file type
  • setl = setlocal
  • fo = formatoptions
  • -=cro disables c, r and o options ( see documentation )

This is a really annoying problem we meet pretty often. You can meet it even if you don’t use Bash. For example, you upload an image named “my lovely cat.jpg” to your website and find no image there. Or it has no thumbnail. This may happen because you are using some program for making thumbnails that is being called from, say, PHP via backtick operator (that is, using unix shell) and that program interprets the file name passed to it as three different command line options: my, lovely and cat.jpg.

Read the article

It’s not easy to remember, so here’s the reference.

var=aaa.bbb.ccc

  % %% # ##
  .*  ${var%.*} => aaa.bbb ${var%%.*} => aaa ${var#.*} => aaa.bbb.ccc (doesn’t work) ${var##.*} => aaa.bbb.ccc (doesn’t work)
  *.  ${var%*.} => aaa.bbb.ccc (doesn’t work) ${var%%*.} => aaa.bbb.ccc (doesn’t work) ${var#*.} => bbb.ccc ${var##*.} => ccc
Read the article

I was doing a simple test of pwgen (handy utility to generate passwords) and since this test was creating a lot of files in a folder I decided to combine it with a test of ls speed vs find speed:

Read the article

For some reason there is no obvious way to enable it (Gnome 2.26.3)

  1. Open GConf editor: Applications -> System Tools -> Configuration Editor
  2. apps -> metacity -> general -> compositing manager (check box there)

That’s all. Enjoy numerous visual effects like shadows, real transparency (underlying windows become visible) etc.

Pretty easy:


mysql -u<username> -p<password> <dbname> -B -e "select * from '<tablename>'" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > <filename.csv>

If Amavis reports this scary error, it doesn’t necessarily means that your disk is cracked. Usually this means that the file it is talking about has wrong format. This happens, for example, if you upgraded Amavis or Spamassassin. Check current format with the file utility and upgrade it with dbX.X_upgrade where X.X is appropriate version. Or simply remove the auto-whitelist file, it will be re-created. Same with bayes_seen and bayes_toks files.

If you have Vim syntax highlighting enabled, the code example below will be highlighted incorrectly: 2, 3 and 4 lines (as well as the all lines below) will be all in blue as if they are comments. This happens because Vim considers everything after /* as a commentary.

1
2
3
4
for script in ${rpath}/*.mon.sh
do
   $script ${1}
done
Read the article

Error message:

1
2
Error occurred during initialization of VM
Could not reserve enough space for object heap

means: not enough Virtual Memory for Java VM to initialize. Weird for a first glance, but are you sure you have it enough? Quite probable that you have ulimits setup, which, in turn, is quite probable if you have cPanel.

Bash files test operators reference. Everybody using Bash actively should know at least -e -f and -d operators, but there are many others :)

Read the article

Bash special variables reference. Everybody using Bash actively should know at least positional variables, Return value variable, Process ID variable, but there are also many others..

Read the article

If you (like me) use mplayer to play music, you might want to make it possible to play songs in random order. Here is a simple script for this:

#!/bin/bash

tempfile=/tmp/randplay-$$
touch $tempfile
trap 'rm -f $tempfile; exit' 0
trap 'rm -f $tempfile; exit' 3
trap 'rm -f $tempfile; exit' 15

songsdir="${1}"
# Remove the finding playlist condition below if you want the list of files to be regenerated each time
# you run the script. This is useful if the content is changing often, but delays the script startup.
# You can also simply remove file m.playlist from the folder if its content changes. 
if [ -f "${songsdir}/m.playlist" ]
then
  echo "Playlist found!"
else
  n=1
  OFS=$IFS
  IFS='
'
  for song in `ls "${songsdir}"`
  do 
    echo "$n ${song}" >> "${songsdir}/m.playlist"
    n=`expr $n + 1`
  done
  IFS=$OFS
fi
songstotal=`cat "${songsdir}/m.playlist" | wc -l`
while true
do
  echo ">>> `cat ${tempfile}|wc -l`"
  sleep 1
  a=$RANDOM
  songsnumber=`expr $a - $a / $songstotal \* $songstotal`
  [ `cat ${tempfile} | grep -c ^${songsnumber}$` -gt 0 ] && echo "Skipping `grep "^$songsnumber" "${songsdir}/m.playlist" | sed "s|^$songsnumber ||"`" && continue
  echo ${songsnumber} >> ${tempfile}
  songname=`grep "^$songsnumber" "${songsdir}/m.playlist" | sed "s|^$songsnumber ||"`
  mplayer -quiet "${songsdir}/${songname}"
  [ $? -ne 0 ] && exit 0
done

The syntax is simple:
<script name> <folder>
Use ESC-ESC or PgUp to skip, Ctrl-C to stop.

There is the command eval available, that does the trick:

Read the article

To make possible managing your system clock on Xen VPS server:


sysctl -w xen.independent_wallclock=1

or:


echo 1 > /proc/sys/xen/independent_wallclock