50 Extremely Useful PHP Tools is just that: An awesome list of stuff that makes PHP development so much easier. A list like this would have saved so much time when I started learning PHP, but most of the stuff on that list didn’t even exist.
Posts Tagged: Programming
Being Greedy With Bash
Last night at my C/Unix class the professor quickly glossed over an interesting shell scripting technique that allows you to strip stuff off the beginning or end of a variable. I forgot about it until I saw the technique used again while editing a shell script at work today.
I didn’t know what the technique was called but I remembered the professor saying something about “greedy clobbering” and, since I cannot search Google for special characters, I Googled “Bash greedy” and luckily found 10 Steps to Beautiful Shell Scripts, which just so happened to contain the technique I was looking for (#5).
There are basically four versions of this technique:
${var#pattern}
Searchvarfrom left-to-right and return everything after the first occurrence ofpattern
${var##pattern}
Searchvarfrom left-to-right and return everything after the last occurrence ofpattern(be greedy)
${var%pattern}
Searchvarfrom right-to-left and return everything after the first occurrence ofpattern
${var%%pattern}
Searchvarfrom right-to-left and return everything after the last occurrence ofpattern(be greedy)
Here’s how it works. Let’s say you have a variable that contains the path to a file:
FILE=/home/raam/bin/myscript.sh
Now let’s say you wanted to extract the myscript.sh part from that variable. You could do some funky stuff with awk but there is a much easier solution built into Bash:
SCRIPTNAME=${FILE##*/}
Now $SCRIPTNAME will contain myscript.sh!
The ##*/ tells the shell to search left-to-right for everything before and including the slash (*/), be greedy while doing it so that all the slashes will be found (##), and then return whatever is left over (in this case, myscript.sh is the only thing remaining after the last slash).
AFAIK, this is a Bash-specific feature, but I’m not entirely certain and I wasn’t sure where I could look to find out. It’s amazing how four characters can do so much work so easily. The more I learn about what I can do with Bash, the more I wonder how I ever lived without all this knowledge!
I subconsciously converted a problem into a shell script
I have been writing a lot of shell scripts lately as part of the C/Unix class that I’m taking at Harvard Extension. My familiarity with how the Unix shell and the underlying system works has grown exponentially. When I came across a problem earlier today, I subconsciously turned the problem into a shell script without even thinking about it!
The problem: “How can I check to make sure my program is running every 30 minutes and restart it if it’s not?”
Answer:
# If myscript isn't running, restart it
ONLINE=`ps aux | grep -c myscript`
# 2 because grep myscript also finds 'grep myscript'
if [ $ONLINE -ne "2" ]; then
$MYSCRIPT_PATH/restart_service.sh
fi
I’m sure there are many better ways to solve this problem, but the fact that I instantly translated the problem into shell scripting code (and that it worked as expected on my first try) astonished me. I can see how good programmers who write in a particular language, and know the in’s and out’s the like the back of their hand can turn problems into code seamlessly (or know exactly where to look to find answers if they’re unsure).
It’s really amazing how easily you can solve simple problems when you have a deeper understanding of how the system works.
That’s all. I just wanted to share my excitement. :)
NetBeans for PHP
Sun Microsystems has added PHP support to their open-source Netbeans development IDE. I just tried the latest version (6.5) and I’m not impressed at all, at least with their OS X version: It’s slow and the Open File dialog takes a good 45 seconds (!) to load.
Fun uses of code
The International Obfuscated C Code Contest has some cool demonstrations of what the C programming language is capable of including a tiny web server (even though it was voted Best Abuse of the Guidelines), a BASIC interpreter, and some fun uses of code. Another site, 99 Bottles of Beer, holds a collection of the song “99 Bottles of Beer” written in 1,233 different programming languages, including some historic ones like Focal-8, plain crazy ones like Procmail and sed, and more current languages like Java and Python.
