More Unix Fun!
July 30th, 2008 Pekk
Just now I was poking through some files on my Slackware machine. I went roaming through /etc/profile to add an option I found online that adds auto-complete to commands using sudo. I decided to add the command at the bottom of my /etc/profile (to allow all users to have it). The line added was:
complete -cf sudo
Additionally, while poking through on my slack machine I discovered the source to one of the many joys of Slackware: random quotes when bash starts! On startup, the default profile (/etc/profile) script runs through all *.sh files in /etc/profile.d and executes them (if the files have +x permissions). One of these files is named bsd-games-login-fortune.sh (on my slack machine I also had a script for csh as well). So I opened up this script to have a look and found this:
#!/bin/sh
# Print a fortune cookie for interactive shells:
if [[ $- = *i* ]]; then
echo
fortune fortunes fortunes2 linuxcookie
echo
fi
At a quick glance you can see the main program (or possibly script) behind everything is this fortune thing. So I exit vim, and type ‘fortune‘ on the command line, and voila~ Then I typed ‘which fortune‘ and I see it’s installed in /usr/games/. ‘file /usr/games/fortune‘ tells me it’s an executable, so I scurry back to my mac to check macports for a “fortune” package. I bring up my terminal:
sudo port install fortune
I now have fortune on my mac
To add it to my start up, I take a look back at the bsd-games-login-fortune.sh script:
#!/bin/sh
# Print a fortune cookie for interactive shells:
if [[ $- = *i* ]]; then
echo
fortune fortunes fortunes2 linuxcookie
echo
The lines in bold are the ones I didn’t understand. Pretty terrible, right? First off I had to figure out what the double brackets ([[ and ]]) meant for bash. After some swift googling I found out that [[ and ]] can do a few things for us.
- We do not have to quote variables (i.e., “$MYVAR”) when doing checks, though it is still recommended.
- Word splitting and pathname expansion are not performed on the words between the [[ and ]];
- [[ and ]] allow us to use wildcarded comparisons *I THINK, this is an observation based on the context of this script*
Well there were a few other things too but I lost the page now and I can’t find it
for a much longer reading. For more information check man bash. Look in the section that starts:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the
conditional expression expression. Expressions are composed of
the primaries described below under CONDITIONAL EXPRESSIONS.
Word splitting and pathname expansion are not performed on the
words between the [[ and ]]; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution,
process substitution, and quote removal are performed. Condi-
tional operators such as -f must be unquoted to be recognized as
primaries.
The next thing I didn’t understand was the $. I found a page explaining $- on the net by searching for himBH which is the string I kept getting when I did echo $- in a terminal window. It turns out this returns the current shell options, which is kind of funny because I somewhat guessed this when I saw the output, I assumed i means interactive. This info is also available in the bash manpages.
Next up is the fortune fortunes fortunes2 linuxcookie line. What the hell was this supposed to mean? I did a man fortune as it turns out, this is just giving extra files for fortune to read from, which are all located in /usr/share/games/fortune/ (on Slackware) and if you’re really bored one day you can just cat them and read away :)—but I think it’s better not to spoil the surprise
unfortunately when I copied this script line-for-line (I just put it directly in my /etc/profile, instead of making a separate script for it) I was getting errors that my fortune binary couldn’t be found. Well the reason is that its in /opt/local/bin, and (for me anyway) this path hasn’t been added to my path yet (and doesn’t get added until my ~/.profile is sourced). To fix that I just put the full path name /opt/local/bin. The same will be necessary for the files, too. On my macports MacX install they’re located by default in /opt/local/share/games/fortune/. Have yourself a look in this path and you can even add a couple more fortune files that aren’t in by default!
MacX Users:
ls /opt/local/share/games/fortune/
Linux Users:
ls /usr/share/games/fortune/
Again, those are just defaults. If you chose to do a manual install (which I didn’t explain here anywhere) then you obviously know where your fortune files should be
But wait! There’s more! I was now more curious than ever. I did a grep Welcome /etc/* and found exactly what I was looking for in /etc/motd. That annoying “Welcome to Darwin!” message. I wanted to change it to say “Welcome to `hostname`.” which is also tacky—I felt was a little improvement on the original, but apparently the motd (”message of the day” in case anyone is wondering) doesn’t seem to evaluate anything like a bash script would. I changed mine so now it says “Welcome to Darwin!! :D” After doing some more research I found out that the motd can be surpressed by creating a file named .hushlogin in your home directory. So I brought up my terminal to test it out:
touch ~/.hushlogin
And then I hit Command + N for a new terminal (MacX) and *poof* my motd is gone
check man motd for more info.
BUT WAIT THERE’S MORE!
I also figured out how to change the sensitivity on my mouse on my Slackware so it’s not so insane. My friend at work helped me with this one:
xset m 1 1
Thanks, Nikolay!
Alright that’s really all I got for you now. Later!
Posted in Mac OS X, Slackware, Linux | 1 Comment »