Archive for the ‘cli’ tag
FizzBuzz
Hmmm…
In Bash, I would have done:
$ for i in `seq 100` ; do if [ `expr $i % 15` -eq 0 ] ; then echo FizzBuzz; elif [ `expr $i % 3` -eq 0 ]; then echo Fizz; elif [ `expr $i % 5` -eq 0 ]; then echo Buzz; else echo $i; fi; done
Then again, it took me about 10 minutes to do that. (I couldn’t get the ternary operator to work, somehow.) In C: about five minutes (rusty — I last used C way back in college {ages ago!}).
In a recent phone interview, I was asked to programmatically (in shell) rename a set of files. I blundered for about two minutes, and gave up in the end, saying I could probably do that by experimentation. I could fairly say I passed that interview. The point? Answering “FizzBuzz” questions does not reflect real-world situations — it’s how you approached the problem, even if you didn’t get the answer, that matters.
If I were an interviewer, I’d concentrate on the steps rather than the solution.
Trivial desktop customization
I’m currently playing with Devil’s Pie, a utility that matches windows and window events to a set of rules similar to Emacs’ (yikes! :P) S-expressions.
I installed Devil’s Pie on my work desktop (running FC6), thus:
$ sudo yum install devilspie
(I have previously organized my desktop with four workspaces: for browsing, remote SSH sessions, remote desktop sessions, and other tasks.) I then created a configuration file:
$ mkdir .devilspie && vi ~/.devilspie/workspaces.ds (debug) (if (is (application_name) "Firefox") (begin maximize (undecorate (set_workspace 1)))) (if (is (application_name) "Terminal") (begin maximize (undecorate (set_workspace 2)))) (if (matches (application_name) "^rdesktop.+") (begin center (maximize (set_workspace 3))))
and invoked Devil’s Pie: devilspie -d ~/.devilspie/workspaces.ds &. The (debug) line in the configuration is, heh, for debugging purposes so Devil’s Pie will print out events and other information, which I can then later use.
Seems pretty straightforward, though there isn’t much functionality that I can use. (Then again, my requirement is minimal: I just want to group apps to different workspaces to avoid clutter.) I can then drop Devil’s Pie in my X startup script, along with the other startup apps.
CLI shortcuts, 7
(This is part of an ongoing series on Linux CLI shortcuts and hacks.)
After editing .bashrc and .bash_profile, you can reload the values by running
$ source ~/.bash_profile
Ouch!
Given:
$ ll Archive/ total 8 -rw-r--r-- 1 d3m users 10 2007-01-26 16:29 file1.z -rw-r--r-- 1 d3m users 15 2007-01-26 16:29 file2.z
What happens if:
$ cd ~/Destination; cp ~/Archive/*.z
![]()
Writing a custom init script
It’s been a long time since I did a source tar-ball install. But the production servers at work require that applications that are not part of the standard install base (from Kickstart/Jumpstart and VMWare images) should be compiled from source. It was a very refreshing experience.
So after going through the usual configure; make; make install invocation, it was time to make sure that the app is persistent at boot. I would have copied an init script from my *nix desktop, but good thing SLES had a baseline script in /etc/init.d/skeleton.
In a nutshell, the steps are:
- Define the
INIT INFOsection, as per the LSB specs. - Fill in the variables.
- Use /etc/rc.status and rc_* functions for sanity checks.
- Create the basic
start,stopandstatusfunctions. - Use YAST (System > System Services (Runlevel)) — or, if available,
innserv— to enable the startup script.
Below is a script I wrote for Apache:
#!/bin/sh # # Author: Ian Dexter R. Marquez # # /etc/init.d/httpd # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Description: Start the Apache httpd daemon ### END INIT INFO HTTPD=/path/to/apache/2.2.4/bin/httpd APACHECTL=/path/to/apache/2.2.4/bin/apachectl PROG=httpd test -x $HTTPD || exit 5 test -s /etc/rc.status && . /etc/rc.status && rc_reset start() { echo -n "Starting $PROG: " $HTTPD -k start rc_status -v } stop() { echo -n "Stopping $PROG: " /sbin/killproc $HTTPD rc_status -v } status() { echo -n "Checking $PROG: pid " /sbin/pidofproc $HTTPD rc_status -v } case "$1" in start) start ;; stop) stop ;; restart) stop start rc_status ;; status) status ;; try-restart|condrestart) if test "$1" = "condrestart"; then echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}" fi $0 status if test $? = 0; then $0 restart else rc_reset # Not running is not a failure. fi # Remember status and be quiet rc_status ;; graceful|help|configtest|fullstatus) $APACHECTL $@ rc_status ;; *) echo "Usage: $PROG {start|stop|restart|status|graceful|help|configtest|fullstatus}" exit 1 esac rc_exit
References:
