||= (or equal operator) in bash?
If you’re used to writing code in ruby, perl, or other languages that provide an ||= (or equal) type operator, you may someday find yourself wanting to do the same in a shell script. While this isn’t directly provided in bash, for example (at least not that I could find), you can emulate the same functionality.
The answer lies in using a little bit of of tomfoolery from the Advanced Bash Scripting Guide, specifically under the Parameter Substitution section.
The part we’re interested in at this point is the ability to use a default parameter. These constructs in bash look like so:
${parameter-default}
and
${parameter:-default}
Note that as pointed out in the documentation, the difference between the two is that the former will not evaluate if parameter is null whereas the latter will.
So just how do we accomplish something like:
$foo ||= $bar ||= $baz
Like so:
FOO=${FOO:-$BAR}
FOO=${FOO:-$BAZ}
FOO=${FOO:-foo}
So this short circuits just like the ||= type operator above. So say, for example, you wanted to spit out a diff of some code before confirmation of a submit or something. Ideally you want to fire up the diff program the user prefers. So why not do something like:
DIFF=${DIFF:-$MYOTHERDIFF}
DIFF=${DIFF:-diff}
In this example, we use what is in $DIFF first. If there’s nothing in $DIFF, we use $MYOTHERDIFF. Finally, if there is nothing in either, we use straight up diff. This way if certain diff variables are set to programs like gvimdiff, tkdiff, etc., they will be invoked.
Clearing konsole history with dcop
Here’s another quick tip for manipulating your Konsole sessions using DCOP. Need to clear the history buffer in an automated fashion for some reason? Use DCOP!
There are three steps to this endeavor:
- Find out the reference to the application.
- List the available methods (just so you know what’s available).
- Send your Konsole session a command!
Here’s an example:
rwoodrum@frums:~$ echo $KONSOLE_DCOP_SESSION DCOPRef(konsole-12065,session-1) rwoodrum@frums:~$ rwoodrum@frums:~$ dcop konsole-12065 session-1 QCStringList interfaces() QCStringList functions() bool closeSession() bool sendSignal(int signal) void clearHistory() void renameSession(QString name) QString sessionName() int sessionPID() QString schema() void setSchema(QString schema) QString encoding() void setEncoding(QString encoding) QString keytab() void setKeytab(QString keyboard) QSize size() void setSize(QSize size) rwoodrum@frums:~$ rwoodrum@frums:~$ dcop konsole-12065 session-1 clearHistory rwoodrum@frums:~$
Voila! History cleared.
another electoral college debacle
Today we see yet again the effects of undemocratic institutions like pledged delegates and the electoral college akin to the elections of 2000. Caucuses even more so.
We do not need these antiquated, obfuscated mechanisms.
It’s time to move to a popular vote system.
iPhone laptop internet access
Long time no post. I’ve been in the process of both moving and changing jobs, so I’ve definitely neglected the blog. The move, however, has left me without Internet access (akin to being without oxygen) and so I’ve been surviving on my iPhone. Of course, given that my iPhone functions as yet another *nix box to me, I set out to get my laptop some internet by way of my iPhone. If I’m breaking terms of agreement here, then this is all just pretend.
This post is for linux users. It doesn’t explain how to do it on your crappy windows box, although if you’re at all savvy, you should be able to figure it out in the same way. Or just google around for keywords like iphone tether laptop.
In my scenario, I’ve got a linux laptop running Debian/sid and a jail-freed iPhone. There are quite a few howto’s already out there, such as this one which I followed for awhile… until I thought I found a less complicated way. There was also this one, but again, I thought it seemed easier to do it my way. Not saying, of course, that my way is the best way… this is just how I did it. I also stumbled upon a couple of gotchas that I had not seen outlined elsewhere, so read on for more awesomeness.
I use the Cydia package installer by Jay Freeman on my iPhone because it’s gloriously built on top of one of my favorite tools, APT. He has made the keen move of not reinventing the wheel here, and instead improving it. Good design principles. Via Cydia, I have simply installed ssh on my iPhone.
So the steps of setting up your little iPhone <-> laptop network will be:
- create an ad-hoc wireless network between the iPhone and the laptop
- ssh from the laptop using dynamic application-level port forwarding and subsequently make your browser (Iceweasel in my case) use this connection
- make sure your routing table is kosher on the laptop
- mind the gotchas
First, let’s create the ad-hoc wireless network. On your linux laptop, assuming you’ve ever used your wireless before, you should have ‘iwconfig’ installed. The goal here is to create an ad-hoc network that your iPhone will be able to join. You can do so with something similar to the following:
In this example, my wireless interface is ‘wlan0′.
rwoodrum@shamp:~$ sudo iwconfig wlan0 mode ad-hoc
rwoodrum@shamp:~$ sudo iwconfig wlan0 essid "iphone_adhoc"
rwoodrum@shamp:~$ sudo ifconfig wlan0 up
rwoodrum@shamp:~$ sudo iwconfig
lo no wireless extensions.
eth2 no wireless extensions.
wmaster0 no wireless extensions.
wlan0 IEEE 802.11g ESSID:"iphone_adhoc"
Mode:Ad-Hoc Frequency:2.412 GHz Cell: 8A:B3:63:C0:DB:B2
Tx-Power=27 dBm
Retry min limit:7 RTS thr:off Fragment thr=2352 B
Encryption key:off
Link Quality:0 Signal level:0 Noise level:0
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
So you can see above that my wireless card is configured in ad-hoc mode and that the ssid is “iphone_adhoc”.
Now if you search for wireless networks with your iPhone, you should see the “iphone_adhoc” network in your network list under Settings -> Wi-Fi. Choose to join the network by tapping it. Note that by joining an ad-hoc network, your iPhone will retain its network configuration on the Edge network (interface ip1) and it will retain a default route to the Internet based on the Edge network. This is important.
Now you need to get them on the same network. If you elect to use the link-local addresses in the 169.254/16 network, just note the address on your iPhone and subsequently configure your laptop to be on the same network. For example, if your iPhone comes up with address 169.254.105.88/16, you could put your laptop as 169.254.105.89/16. Alternatively, you could go the extra step to put them on a different network of your choosing. Because extra steps are for the uncool, I’ll just let the iPhone choose it’s address in the 169.254/16 network and put my laptop on the same network like this:
sudo ifconfig wlan0 169.254.105.89 netmask 255.255.0.0
You should now be able to ping your iPhone from your laptop.
At this point, I’ll bring up gotcha #1. When your iPhone auto-locks, your connection will die. From this point out, it’s probably a lot easier to simply disable this. To do so, go under Settings -> General -> Auto-Lock and change it to “Never.” Just don’t forget to change it back.
Now, we need to ssh into the iPhone using it as a socks proxy. This will allow the connections of our laptop to go through the iPhone. To establish this connection, ssh to your iPhone using something like the following. In my example, I’m simply ssh’ing as the ‘mobile’ user (the default user on the iPhone):
rwoodrum@shamp$ ssh -f -N -D 9999 mobile@169.254.105.88
This will put ssh into the background listening on tcp/9999. If a connection is received, it will forward it over the secure connection and subsequently use the application level protocol to determine where to connect from there. Before you can use this, however, you should configure your Iceweasel/Firefox to shunt DNS lookups over this channel as well. You do this by editing the ‘network.proxy.socks_remote_dns’ value to ‘true’ under ‘about:config’ in the browser.
You must also configure your socks proxy under Edit -> Preferences -> Advanced tab -> Settings button to point to your new ssh socks proxy. To do this, enter under ‘SOCKS Host’ the value 127.0.0.1 with port 9999. You must ensure that no other proxy values exist. All others must be blank.
Now, on your laptop, modify the routing table to add the iPhone as the default route to the Internet. If my iPhone has an address of 169.254.105.88, I would do this like so:
rwoodrum@shamp$ sudo route add -net 0.0.0.0/0 gw 169.254.105.88
You’re almost done.
You may need to shutdown Iceweasel/Firefox entirely just to make sure all the application level changes take effect.
You should now be able to fire up your browser and hit something like google.com. It’ll be slow… but it’ll work.
One thing you may experience is that during your internet browsing, Iceweasel/Firefox will suddenly complain about timeouts. You may also notice that the Edge icon on your iPhone has changed to a wireless connection. The Edge connection seems to periodically “shut down” when not used directly from the iPhone. To remedy this situation, I simply fired up the “weather” button and periodically switched it from city to city so the update would take place. This fires up the Edge connection from the iPhone itself and seems to breathe life back into the connection via the laptop. Not ideal… but at the time of this writing, it’s what worked. Alternatively from a shell on the phone you could just start a ping or something. Given limited bandwidth, however, you may want to increase to every x number of seconds or something.
Comments appreciated! I’d be happy to help anyone in need with a beloved iPhone and no beloved Internet!
Evan Bayh is spot on
I think this might be my last post on politics, although one can’t be certain of something like that. I’d like to focus the blog more on what drove me to create it: giving back to the Internet community with useful technology posts. I think that’s a laudible goal and I think a ton of great information comes out of blogs.
The world is well aware of Obama’s somewhat nasty comments about rural, working-class Americans. Read all about the vitriol Obama spewed here:
“So it’s not surprising then that they get bitter, they cling to guns or religion or antipathy to people who aren’t like them or anti-immigrant sentiment or anti-trade sentiment as a way to explain their frustrations,” Mr. Obama said, according to a transcript that appeared Friday on the Hufington Post Web site.
I just had to post about this piece in the New York Times Caucus Blog about Senator Evan Bayh’s comments on Obama’s “bitterness” fiasco. Here’s what he had to say:
“I’m concerned that statements like this, even if they’re taken out of context, can be used very effectively by the other side to keep us from getting the change that we need. Look at John Kerry – served in Vietnam, won medals. Look at what they ended up doing to him by the end of that campaign. Look at Al Gore, won the Nobel Peace Prize now, you know, an Oscar. They made him look like he was a serial fibber and so forth. So the important point here is that they can use this politically to damage Barack.”
Wow. He is spot on.
The Republican spin machine can work veritable miracles in duping the American people into fiction versus fact. Hillary Clinton has stood up to this crap for years. She can take it and she can deal with it. She can beat the Republicans at their own game. And that’s why it’s so important that we elect a President that can play the Republicans’ own game.
Hillary for President!
Happy Earth Hour!
It’s dark in here… at least my DS is charged…
McCain over Obama
I thought this was a very interesting poll by the New York Times regarding Democrats who will vote McCain if their candidate isn’t the nominee. I’m beginning to wonder if I will lump myself in this category. Some of the comments are particularly interesting. Take for example this one from an Obama supporter who clearly can’t make a intelligent policy argument/discussion:
Thanks for wrecking our party, Hillary. Then again, anyone who believes your bs should vote for McCain. He’s a liar, just like you, only older, and better looking.
— Posted by jeffp
Or this one from a Hillary supporter:
Not surprising. I am a non-democrat (ie, independent) Hillary supporter that would never pull the lever for Obama. It started with his lack of credentials, moved on to his hypocrisy and inability to take responsibility for his own actions and culminated in his racism (”typical white person”).
I’ll be voting for McCain if Hillary is not the democratic nominee in November.
— Posted by Ann
Why do I think Hillary should be President of the United States? Look at the “leadership” Obama has exhibited on the disenfranchisement of the Florida and Michigan voters? He is running the clock. What a crock. And yet he is praised by both the media and his supporters for being a ray of hope amid a tumultuous world. That, my friends, is a farce. America is in the gutter because of the cronyism of George W. Bush and crew and we need someone to lead this country and the rest of the world. This man Barack Obama is nothing but talk.
Nope… as a long time Democrat, I can proudly proclaim that I’m going to vote for the person I think would be the best leader. This is the first step for all American voters to take. Get over the crap the media feeds to you, get over wedge issues, and vote for a good leader.
Mukasey ’surprised’ by scope of terrorist threats
That’s the headline on this piece at CNN where Bush-Cheney puppet Attorney General Michael Mukasey reports to “have been taken aback by the scope and variety of potential terrorism threats facing the United States.”
Furthermore:
“I’m surprised by how surprised I am,” said Mukasey, who as a federal judge presided over terrorism-related trials in New York.
“It’s surprising how varied [the threat] is, how many directions it comes from, how geographically spread out it is,” he said.
You have got to be kidding me. Let me translate this whole piece of “news” trash for you:
Bush administration official thinks it’s “really crazy how scared we should be from threats of terrorists. For real, it’s really, really bad. It’s amazing how much we should give in and live in constant fear and continue to abridge our freedom and rights.”
This is nothing short of outright propaganda. This piece of “news” is indicative of how the Republicans have run this country under Bush-Cheney for the past 8 years. It is despicable. Every so often, the news is permeated with pieces like this from alleged jounralist Terry Frieden to “keep being scared and vote Republican.” There is zero substance here, naturally because - no surprise - it’s “secret”!
I, personally, have had it. Yes, terrorism exists. No, I won’t give up my rights and cower at every turn feeding into the Republican fearmongering machine.
I just heard thunder in Seattle
I moved here to the Seattle area (Kirkland, right now) almost 18 months ago to the date. I moved from Florida, where thunderstorms are often times a daily occurrence. Well I was just sitting here at the computer working on some stuff and I saw a flash (it’s raining outside, no surprise there). I thought to myself “was that lightning??” and began listening intently. A few seconds later… a low, short rumble.
18 months for my first dose of thunder in Seattle. Weird.
5 Years in Iraq? I don’t understand…
I don’t understand what all this hullaboo is recently about the War in Iraq having gone on for 5 years. The New York Times is running a whole bunch of pieces about having been at war in Iraq for 5 years. They even have this nifty interactive timeline of events in Iraq from 2002 to 2008. But I don’t understand any of it… The war ended on May 1st, 2003. And I even have proof:
