Browsing AuthorCelso

Line Terminations

Line terminations for different operating systems:

unix 0x0a LF
Classic Mac 0x0d CR
Windows 0x0d 0x0a CR LF

To convert a text file with DOS line termination to UNIX line termination:


tr -d '15'  unixfile.txt

or


sed s/.$// winfile.txt > unixfile.txt

To convert a unix file to a DOS file:


sed s/$/x0d/ unixfile.txt > winfile.txt

Run Periodic Maintenance Scripts Manually

Mac OS X has to run some maintenance scripts in the middle of the night to do some important housekeeping tasks. If you have a Mac that sleeps in the middle of the night, as you do, you need to run these manually by running this from the shell:


sudo periodic daily weekly monthly

Screen Size vs. Viewing Distance

The following is a rule of thumb on the minimum viewing distance for a certain screen size:

Display Type Minimum Viewing Distance
16:9 HDTV 1.5x Diagonal
4:3 HDTV 1.8x Diagonal
Analog 3x Diagonal

Run as root only

To make sure that a script will be run by root only:


#!/bin/sh

if [ `id -u` != 0 ]; then
  echo "Permission denied, must be root"
  exit
fi

# Do the thing...

Authenticate Users

This snippet could authenticate users using their /etc/passwd or /etc/shadow entry. May have to run this with higher than normal privilege:


#!/usr/bin/env perl

print "Username: ";
chomp($uname = <stdin>);

$pwd = (getpwnam($uname))[1]; # get the user's pwd
die "invalid usern" unless defined $pwd and length $pwd;
$salt = substr($pwd, 0, 2);

system "stty -echo";
print "Password: ";
chomp($word = <stdin>);
print "n";
system "stty echo";

if (crypt($word, $salt) ne $pwd) {
    die "Sorry...n";
} else {
    print "okn";
}