Friday, November 8, 2013

Change date and time the easy way

I recently had the task to simulate some time jumps on the system to test software features which only kick in when I certain amount of time has passed. In my case this was a month. Since I'm a bit lazy and hate to calculate with dates I found this command line to easily change the system date without a lot of thinking

date "+%Y%m%d %H:%M:%S" -s "$(date "+%Y%m%d" -s 'now + 1 month') $(date +%H:%M:%S)"

The argument in the single quotes do the stuff I need to set the current clock to 1 month in the future and keeps the actual time. ( if you change the day alone date sets the time to 00:00)

Friday, September 6, 2013

Make binaries static

This is the second time I need a static compiled executable and of course I forgot how to do it. Actually it is really easy just add "-static" to your build process. The tricky part is where to put it. Some configuration scripts provide a "--enable-static" or "--disable-shared" but this is rarely the case. So if you have some source first configure it as usual (autogen,configure,...) after that edit the Makefile and "-static" to the CFLAGS. That should do the trick. Following block is from emacs 24.3 lib-src Makefile to get a static linked emacsclient.

EMACS = ../src/emacs
EMACSOPT = -batch --no-site-file --no-site-lisp
# ==================== Things `configure' will edit ====================
CC=gcc -std=gnu99
CFLAGS=-g3 -O2 -static
version=24.3
## Used in $archlibdir.
configuration=x86_64-unknown-linux-gnu
EXEEXT=
C_SWITCH_SYSTEM=

Tuesday, September 3, 2013

BASH scripts and white spaces ...

Oh well, it happened again. I was crying of frustration and anger , bit into the desk and shouted wild curses through the office space. Reason for this was a small shell script which iterates through a list of database tables which should be dropped. To do so I called psql with some arguments and a very simple "DROP TABLE $i CASCADE;". This is how the initial attempt of the script looked like.
for i in $TABLES
do
  ${PSQL} -U ${PSQLUSER} -p ${PORT} -c "DROP TABLE ${i} CASCADE;" ${PGDB}
done
This exploded right into my face with
psql: warning: extra command-line argument "postgres.testtable" ignored
psql: warning: extra command-line argument "CASCADE;" ignored
psql: warning: extra command-line argument "postgres" ignored
psql: FATAL:  database "TABLE" does not exist
Eeeek, of course it does that because BASH (in scripts,not command line) ignores my attempt to tell it that my SQL command is one argument and not four. The fact that used quotes is nice but futile. After a lot of try and only errors  of escaping, quoting, variable encapsulating , forking shells, using eval and cursing I gave up and started to read documentation.

So finally the right way to do so ,without fiddling with $IFS, is to construct the command you want to run as an array. Following code works as expected.

for i in $TABLES
do
  PSQLARGS=( -U ${PSQLUSER} -p ${PORT} -c )
  PSQLARGS+=("DROP TABLE ${i} CASCADE;")
  PSQLARGS+=(${PGDB})
  ${PSQL} "${PSQLARGS[@]}"
done
The first call of PSQLARGS defines the array with five elements (separated by spaces), then I add the SQL command as only one element to the array and finally the database. Now running psql  with the array of arguments results in a working version of the whole command line.

Wednesday, August 21, 2013

Yet Another

Hi, I try it again with a blog but this time I have a clearly defined goal. Since I'm a Linux administrator I stumble upon all kind of little (or larger) bits of information. Some are really worth to remember and here is comes the problem. I forget things. This blog should help me to keep track of all this knowledge.
I'm now working for more than 14 years with Linux and it always surprises me what things I'm not aware of. Some are really embarrassing when I discover them and I ask myself "You call yourself Admin and don't know this". So be prepared :)

Maybe someone else out there might find a post here useful. Enjoy

Greetings