Category Archives: UNIX

Posts regarding UNIX and Linux systems

Project Euler

So I was looking around for some programming information and found Project Euler. They have a number of problems to program. You can choose any programming language, it’s just to enhance your programming abilities.

So here is the answer to problem number one.

#!/usr/bin/perl

use warnings;
use strict;

my $sum;
my $count;

for ($count = 0; $count < 1000; $count ++) {
        if (($count % 3 == 0) || ($count % 5 == 0)) {
                $sum = $count + $sum;
                print "\$sum is $sum\n"; }
        }

It’s a really simple exercise. You just need to sum of all the multiples of 3 or 5 below 1000.

Savings script

So I was thinking about writing a script to calculate the savings over a certain matter of time and came up with the following.

#!/usr/bin/perl

use warnings;
use strict;

print "What is the yield?\n";
my $yield = <>;
print "How many years?\n";
my $years = <>;
print "How much money saved anually?\n";
my $savings = <>;
my $i;
my $new_savings = 0;

for($i = 1; $i <= $years; $i++) {
	$new_savings = $savings + $new_savings;
	$new_savings = $new_savings + $new_savings*($yield/100);
	printf "Savings for year $i are \$%.2f.\n", $new_savings;
}

Below is an usage example.

[19:43:04] xavi@ubuntu:/tmp $ ./yield.pl
What is the yield?
5.0
How many years?
5
How much money saved anually?
10000
Savings for year 1 are $10500.00.
Savings for year 2 are $21525.00.
Savings for year 3 are $33101.25.
Savings for year 4 are $45256.31.
Savings for year 5 are $58019.13.
[19:43:18] xavi@ubuntu:/tmp $

This just gives an idea of the money that can be saved over a short period of time with a five percent yield. It’s a really simple script.

Squeeze is now stable

So squeeze is now stable. Together with the new Debian stable release the guys also updated the website.

I have created a small bash script to perform the upgrade. Here it is.

#!/bin/bash
echo "Moving to /etc/apt"
cd /etc/apt
echo "Backing up sources.list"
cp sources.list sources.list.`date +%F`
echo "Changing the sources for squeeze"
sed 's/lenny/squeeze/g' sources.list > squeeze.sources.list
echo "Copying squeeze to sources.list"
cp squeeze.sources.list sources.list
echo "Performing the upgrade."
apt-get update
apt-get dist-upgrade

Run the script as root or use sudo when executing.

Resulting file should be something like the following.

## main & security repositories
deb http://ftp.us.debian.org/debian/ squeeze main
deb-src http://ftp.us.debian.org/debian/ squeeze main
deb http://security.debian.org/ squeeze/updates main
deb-src http://security.debian.org/ squeeze/updates main

References:

  1. Go2Linux
  2. Linode

‘Access denied for user ‘debian-sys-maint’@’localhost’

So the other day I made an aptitude safe-upgrade and MySQL was upgraded. Thing is I made some changes and when I tried to restart MySQL I saw the following error.

user@server:~$ sudo /etc/init.d/mysql restart
[sudo] password for user:
Stopping MySQL database server: mysqld failed!
Starting MySQL database server: mysqld already running.
/usr/bin/mysqladmin: connect to server at ‘localhost’ failed
error: ‘Access denied for user ‘debian-sys-maint’@’localhost’ (using password: YES)’
user@server:~$

Well fixing this error is fixed changing the password for MySQL user debian-sys-maint. We will retrieve the password from the file /etc/mysql/debian.cnf.


server:~# cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = password
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
user = debian-sys-maint
password = password
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
server:~#

We backup the MySQL databases.


user@server:~$ mysqldump -u root -p –all-databases > /tmp/backup.sql
Enter password:
user@server:~$

Now lets change the debian-sys-maint password in the database.

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘debian-sys-maint’@’localhost’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;

Now we should be able to safely restart the database server.

user@server:~$ sudo /etc/init.d/mysql restart
[sudo] password for user:
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..
user@server:~$

References:

  1. Ubuntuforums