Tuesday, October 15, 2013

Ruby Logic

This code snippet creates the output followed below...

#! /usr/bin/env ruby

# Clemens Lee, 2013-10-15

puts
puts "In Ruby, what is true, and what is not."

candidates = [ true, false, Object.new, nil, 1, 0, 1.0, 0.0, "string", "" ]

puts
candidates.each do |x|
  puts "#{x.inspect} is #{x == true}"
  puts "#{x.inspect} is #{!(x == false)}"
end

puts
candidates.each do |x|
  true_or_false = false
  if x
    true_or_false = true
  end
  puts "if #{x.inspect} is #{true_or_false}"
end
...
In Ruby, what is true, and what is not.

true is true
true is true
false is false
false is false
#<Object:0x0000010108ab90> is false
#<Object:0x0000010108ab90> is true
nil is false
nil is true
1 is false
1 is true
0 is false
0 is true
1.0 is false
1.0 is true
0.0 is false
0.0 is true
"string" is false
"string" is true
"" is false
"" is true

if true is true
if false is false
if #<Object:0x0000010108ab90> is true
if nil is false
if 1 is true
if 0 is true
if 1.0 is true
if 0.0 is true
if "string" is true
if "" is true

Friday, August 17, 2012

Debugging Cron Jobs


Just as a reminder, here is the basic script invokation syntax in the crontab table entry:

*=min(0-59) *=hour(0-23) *=monthday(1-31) *=month(1-12) *=weekday(Sunday=0-6) command

[Update 2012-09-03:

First invoke the script in cron from bash embedded via bash -l -c '../my_script.sh', that will make sure cron gives you a realistic bash environment setup, e.g.:

* * * * * /bin/bash -l -c '/home/my_home/bin/my_script.sh'

You can also log all standard and error output in that line by appending .../myscript.sh' > /tmp/my_script.log 2>&1 at the end of the cron entry line.

The bash -l trick did make the difference when setting up the right environment for Ruby scripts that rely on their respective environment to be setup via RVM and their magic usage of the correct bash functions.

This tip is from Andre of the Scoutapp blog.]

Then, if still in cron trouble, put the following at the top of your shell script:

#! /bin/bash

exec &> $HOME/local/var/log/this_script.log

set -x

source $HOME/.bash_profile

env

# script content follows here

---

What does each line do?

- /bin/bash, we have reached 2012, which Unix doesn't doesn't have a bash installed by now?

- exec ... logs standard as well as error output in a file, so you have at least the last run documented (tip by DarkDust).

- set -x prints out all commands in your bash script, lots of information, right?

- ~/.bash_profile invoked in your script sets up your whole environment just like an interactive login shell, at least it should. Make sure you have a sane bash setup script (and whatever is invoked there), make sure you understand all the variations of .profile, .bashrc, or .bash_login or what have you (that's another topic, right?). Test them out so you have little surprises there, don't change directories there or do some other strange side effects. If so, now your cron script should have a very similar environment as when you test out your script from the commmand line.

env can show you what you have in your environment, is the log output the same or different when running the script from the command line? set is another alternative, which also shows all defined functions.


Some questions to ask yourself:

- First, does the script work from the command line?

- Again, does the script have the same environment in cron mode as well as when run from the command line?

- Ok, check the debug output.

- Should I use absolute paths for each and every command invoked in the script, e.g. /usr/local/mysql/bin/mysql instead of just mysql, /bin/grep, or /usr/bin/ruby if you want to go to an extreme. Can be important, if you have and need e.g. a MacPorts command in /opt/local/bin e.g. for the latest getopt command or so.

Tuesday, September 27, 2011

Standard Bash Aliases

alias l="ls -la|more"
alias r="ls -latr"
alias ldir="ls -la | egrep '^d'"
alias t=less
alias md=mkdir
alias ex=exit

Thursday, January 13, 2011

Bash Prompt

Here is my favorite PS1 variable setting for the bash prompt:

export PS1="\! \u@\h \D{%Y-%m-%d} \t \w:-)"

Comment:
:-)             - a smiley for a happy day!
\w - full working directory, an absolute necessity.
\D{%Y-%m-%d} \t - short iso date and current time including seconds.
\u@\h - who are you, and where... could be quite expensive to get this wrong.
\! - look up the bash documentation for the !<hist number> history expansion feature to conveniently reuse previous commands or command parameters. With \! in the prompt you need the history command less to look up previous commands, but otherwise ommit this information in the prompt if your prompt line has already become too wide.
And here is an example how the output looks like:
237 clemens@gavrilov 2011-01-13 19:43:44 ~/dev/src:-)echo 'hi'
hi
238 clemens@gavrilov 2011-01-13 19:43:48 ~/dev/src:-)
For original documentation check "man bash" and jump to the PROMPTING section.

Saturday, January 1, 2011

Shell Script App Dir

#! /bin/sh

if [ "$APPHOME" = "" ] ; then
# try to locate application home directory

## resolve links - $0 may be a link to the app home
PRG=$0
progname=`basename $0`

while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname $PRG`/$link"
fi
done

export APPHOME=`dirname "$PRG"`/..
export APPHOME=$(cd $APPHOME >/dev/null; pwd)
fi

Shell ISO Date and Time

ISO Date and Time in a command line:

date '+%Y-%m-%d %H:%M:%S'

Tuesday, October 26, 2010

Rails without ActiveRecord

Don't need a database for your Rails application or any extra object-relational mapping layer? :)

Then put this in your <RAILS_ROOT>/config/environment.rb file within the Rails::Initializer.run do |config| block:

config.frameworks -= [:active_record]