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

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.

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

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

Problem: in my script I need to do some operations on a list of files stored in a file. I can’t use the ‘while read’ loop because I do these operations on a variable that may or may not be a list of files depending on some condition.

Without variable there is a simple method, e.g.


cp {file1,file2,file3} /some/folder

but if I do something like


myvar="{file1,file2,file3}"; cp $myvar /some/folder

- this won’t work.

The solution is using an array:


myvar=( file1 file2 file3 ); cp ${myvar[*]} /some/folder

This works!