Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Wednesday, March 13, 2013

comm: another way to diff

I recently had a need to compare two files of two different versions.
This is usually what you use <enter your favorite diff tool here> for.
However, these files had similar entries (lines) in different places in the file, and most diff tools are not smart enough to cope with that. What I wanted to do is to eliminate those and to be able to compare only the relevant changes.

These specific files had some irrelevant lines (read: comments), which (conveniently enough) begins with a '#' character. So first I stripped the comments from both files:
grep -Ev '#' .config > .config_no_comments

Now that we have two files that contain only the changes we're interested in, we can use comm to compare them.
comm can be used on sorted files only, so we need to sort our two files:
sort -o .config_no_comments_sorted .config_no_comments

Now, suppose I have those two files and I want to see only the unique lines that file1 has (that are not present in file2), I would do:
comm -23 file1 file2

If I want to see only the unique lines that file2 has (that are not present in file1), I would do:
comm -13 file1 file2

And this will output the lines that appear in both files:
comm -12 file1 file2


RTFM for more options.

Thursday, December 1, 2011

Adding a new service to Fedora 16

Suppose that you have a new service that you want to control with the 'service' command, or that you want to start every time the system boot.
This post explains how to do it in a few simple steps.

Step 1 - Create a service script
Create a file under /etc/init.d, let's call it 'myservice', and add the following to it:
#!/bin/bash
#
# chkconfig: 1000 50 50
#
# description: myservice service
#

start() {
  /usr/share/myservice/start.sh
}

stop() {
  /usr/share/myservice/stop.sh
}

restart() {
  stop
  start
}

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

exit 0
The 'chkconfig' and 'description' lines are mandatory for the script to be used with the chkconfig tool.

From chkconfig's man page:
For example, random.init has these three two  lines:
       # chkconfig: 2345 20 80
       # description: Saves and restores system...
This  says  that the random script should be started in levels 2, 3, 4, and 5, that its start priority should be 20, and that its stop priority should  be  80.

Don't forget to make the script executable:
sudo chmod +x /etc/init.d/myservice

Step 2 - Using the script
sudo service myservice start
sudo service myservice stop
sudo service myservice restart

Step 3 - Run the script automatically at boot time (optional)
sudo systemctl enable myservice.service