Monday, September 29, 2008

Upper case to lower case (sed vs tr)

Let us convert uppercase characters in the file into lowercase

$ cat filename.txt
ABCDEFG

First of all the most complex way is using sed

$ cat filename.txt | sed 's/\(.*\)/\L\1/g'
abcdefg

More easier to use tr

$ cat filename.txt | tr [:upper:] [:lower:]
abcdefg

Sunday, August 10, 2008

Order pizza from Linux command line?

No problem:


Read more

Tuesday, July 15, 2008

SCP without interactive password input

If you want to make your scripts working with interactive programs without your participation use expect tool. Here is an expect script for downloading file from remote host to local directory via scp

1 #!/usr/bin/expect -f
2
3 set timeout 1
4
5 set username [lindex $argv 0]
6 set password [lindex $argv 1]
7 set host_filename [lindex $argv 2]
8 set new_dir [lindex $argv 3]
9
10 eval spawn scp $username@$host_filename $new_dir
11
12 expect "password: $"
13 send "$password\n"
14
15 expect "$ $"

It is very useful application while supporting many hosts, nobody really wants to make the same thing for all the hundreds machines. Let your script do that.

Thursday, March 6, 2008

Amazing bash scripting

Here is a simple bash script

1 #!/bin/bash
2
3 var=''
4
5 if [ -z ${var} ]; then
6 echo "ZERO"
7 else
8 echo "NOT ZERO"
9 fi
10
11 if [ -n ${var} ]; then
12 echo "NOT ZERO"
13 else
14 echo "ZERO"
15 fi
16

How do you think? What will be on your terminal after you execute it?
Suppose

ZERO
ZERO

?

Actually - no :)

ZERO
NOT ZERO

Why? For the moment I do know the solution for the problem, but I really can't explain why that is going on. Here is a solution:

1 #!/bin/bash
2
3 var=''
4
5 if [ -z "${var}" ]; then #we do make sure it's string
6 echo "ZERO"
7 else
8 echo "NOT ZERO"
9 fi
10
11 if [ -n "${var}" ]; then # ... and here
12 echo "NOT ZERO"
13 else
14 echo "ZERO"
15 fi
16

Amazing!?

Here is the answer: http://bash-hackers.org/wiki/doku.php?id=commands:classictest

Wednesday, January 16, 2008

Daemonizing a non-daemon application without nohup

Sometimes *nix man need to daemonize a non-daemon application and make a full init.d start/restart script. And we will.
First of all, nohup will not be used for these purposes, ‘cause it creates a process with a PID we can’t get to know without some extra efforts.
The idea is in a special init.d script.
  1. We start init.d script (#/etc/init.d/myscipt start).
  2. Init script executes itself in a background mode with a special parameter "start_internal" . The new created process have a new PID.
  3. The process executes the application to be daemonized using exec. Exec replaces the shell and no new process is created, so we do know the PID of our application and have no problems to kill the daemonized application.
Here is a simple implementation:

#!/bin/sh

. /etc/rc.d/init.d/functions

# Location of a non-daemon application to be daemonized
BIN="/usr/local/bin/hello_world"
# The username to run the process with
RELUSER="aolehnovich"
# Our PID file
PIDFILE="/var/run/hello_world.pid"
# Log file location
LOGFILE="/var/log/hello_world.log"

start()
{
rm -f $LOGFILE
# Execute ourself in a background mode
su -l $RELUSER -p -c "${0} start_internal &"
echo -n "Starting Application: "
success "Starting Application: "
echo
}

start_internal()
{
# Remember PID
echo "$$" > $PIDFILE
# Exec the non-daemon application
exec "$BIN" > $LOGFILE
}

stop()
{
if [ -f ${PIDFILE} ]; then
kill `cat $PIDFILE`
rm -f $PIDFILE
echo -n "Stopping Application: "
success "Stopping Application: "
echo
fi
}

case "$1" in
start)
start
;;
start_internal)
start_internal
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage $0 {start|stop|restart}"
exit 1
esac
exit 0


Thanks to Viktor Kovalevich. This article uses his knowledge, too ;)