Control-Ms and zipping in UNIX

August 14th, 2009

This is one of those “So’s I Remember For Next Time” posts. A couple of obscure UNIX how-tos.

Tip 1 – Entering Control-Ms in vi

When you’re editing a text file in vi and the file was created in a Windows editor, you’ll see control-M (^M) characters at the end of each line.control-Ms
If you want to add a new line and you want it to show up as a separate line in Windows, you’re going to have to add a control-M at the end of the line. The ^M is a single character, so you can’t just type ^ and then type M. You might think, given the name of the character, you could just hold down the Ctrl key and press m, but it’s not quite that simple.

What you need to do, in vi, is press Ctrl+v followed by Ctrl+m.

Tip 2 – Zipping files in UNIX

Usually when I’m working in UNIX and I want to compress a file, I use gzip:

gzip filename

This replaces the named file with a compressed version of it, with .gz added onto the end of the file name. To unzip the file do:

gunzip filename

This replaces the .gz file with the original file.

However, sometimes you need to create a .zip file. To do this use the zip command, but you need to know the syntax:

zip –r outputfile inputfile

For example:

zip –r temp.zip temp.txt

In fact you don’t need to specify the .zip file extension. If you give a file name without a file name extension, the .zip extension gets added automatically.

A new zip file is created and the original file is not deleted. To unzip just do:

unzip zipfile

This creates a new, unzipped file but does not delete the zip file.

I just tried this out and the following stats for compressing a small text file suggest that gzip is the more effective compression program:

temp.txt    677K
temp.txt.gz    416K
temp.zip    504K

Leave a comment

 

PHP (or Perl) one line if/then/else statements

August 8th, 2009

If you're toggling something between two states in PHP or Perl it's often handy to use an if/then/else one liner.

In pseudocode this goes like this:

<if this evaluates to TRUE> then <parse this> else <parse this>

All you need to do is replace the "then" with a question mark and the "else" with a colon:

<if this evaluates to TRUE> ? <parse this> : <parse this>

For example:

print  $trueOrFalse ? "you're telling the truth" : "you're lying";

Ignore the print command, it's not part of the if/then/else statement, it's just here to do something with the outcome of that statement.

The expression immediately to the left of the question mark is evaluated. The expression between the question mark and the colon is parsed if the expression evaluates to TRUE, otherwise the expression immediately to the right of the colon is parsed. So in the above example, either "you're telling the truth" or "you're lying" is printed, depending on whether $trueOrFalse is ... you guessed it ... TRUE or FALSE.

But perhaps a more common situation is toggling the value assigned to a variable. For example, toggling between TRUE and FALSE:

$trueOrFalse = $trueOrFalse ? FALSE : TRUE;

Here's a practical example of the use of if/then/else one liners. There's two in this chunk of PHP. The scroll box list below the code is the kind of thing this PHP produces.

<div style="overflow:auto; height:100px; width:300px; border:3px groove #DDD; padding:0">
<?php
    $alternateLine = FALSE;
    while($presidentsArray) {
        print "<div style=\"background-color:";
        print $alternateLine ? "#F5F8F9" : "white";
        print "; padding-bottom: 1px\"> &nbsp; &nbsp; <a href=\"someURL\" title=\"This link goes nowhere\">" .
          $presidentsArray['name'] . "</a></div>";
        $alternateLine = $alternateLine ? FALSE : TRUE;
    }
?>
</div>

Leave a comment



A handy PHP date() checker

July 30th, 2009

A handy site to remember if you’re writing PHP is http://php-date.com/. It provides everything you need to know about the date() function in PHP and has an interactive form for testing your formatting until you get your dates/times just the way you want them.

php-date-function

Potentially similar posts

Leave a comment



WordPress for Blackberry

July 8th, 2009

I'm just testing posting to this blog from my phone, using the new WordPress for the BlackBerry application - now available, in beta: http://bit.ly/6lXcM

Leave a comment



The sad and silent death of Yahoo’s EasyListener

July 7th, 2009    10 Comments

Right now, as I write, there is an ugly gap at the top right of all ITauthor.com pages. It used to be filled by an audio player called EasyListener, provided by Yahoo, that I just embedded in my side panel. It was the best thing of its kind because it was neat enough to fit in a slim side panel and it was simple. It read from an RSS feed, pulled out the MP3 files and listed them in the player. You just clicked on the item you wanted and it played the audio.

But Yahoo quietly choked it and hoped that nobody noticed. I’m not sure why they would do this. I’m sure they could handle the bandwidth of people pulling down the Shockwave file off some server that had been left to serve up the old webjay.org pages. Maybe someone at Yahoo just pulled the plug on that server and not enough people have complained. I wish I’d gone and grabbed the files while they were still there. I looked at archive.org but the URL was never spidered.

So I’m going to have to find another solution, but it won’t be for a while because I know there’s nothing quite like it out there, so I’ll have to do some switching around and PHP-ing to sort it out.

It’s symptomatic of the Web though: it seems like a solid, reliable structure, but it’s really entirely transitory and kept in working order by a lot of people and a lot of effort. It’s a bit like a car: as soon as you fix one thing, something else breaks.

Leave a comment



^ back to top ^

Page 5 of 92« FirstNewer«34567»OlderLast »