Wednesday, August 1, 2007

Standard Input and Output in Linux

Every process in Linux has 3 connections to the outside world.  They are:

  • Standard Input (stdin): default to the keyboard
  • Standard Output (stdout): default to the monitor screen
  • Standard Error (stderr): default to the monitor's screen
In Unix/Linux each of these connections is also associated with a file descriptor:
  • 0: stdin
  • 1: stdout
  • 2: stderr
To redirect a process's output to a file (only normal messages, no errors):
Standard input can also be represented by '-'.
This is equivalent to copy con outputfile in DOS.
$ command > outputfile 
$ command - > outputfile

To direct a file as standard input: 
command < inputfile

To direct standard input to a file:
cat  > outputfile

To direct a process's output to another process (command) as standard input, use pipe:
command_out | command_in
e.g. cat filename | more

To direct error to a file:
   $ command 2>errors.txt  # 2 indicates the error stream in Linux
   $ command 2>/dev/null   # /dev/null is a device where anything you send simply disappears.

To direct both standard output and error to a file:
   $ command > error.log 2>&1  # '&1' represent stdout.
To direct stdout to output.log and stderr to error.log, and print only error to the screen:
   $ command 2>&1 > output.log | tee error.log

   # append error to the same output.log file instead of error.log.
   $ command 2>&1 > output.log | tee -a output.log

Monday, July 2, 2007

Changing Passwords in MySQL

To set the root's password the first time:
$ mysqladmin -u root password NEWPASSWORD
To change the root's password:
$ mysqladmin -u root -p'OLDPASSWORD' password NEWPASSWORD
Or you can use the MySQL Client:
mysql> USE mysql;
mysql> UPDATE user SET PASSWORD=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
To reset root's password, run mysqld_safe --skip-grant-tables:
$ /etc/init.d/mysqld stop
$ mysqld_safe --skip-grant-tables &
$ mysql -u root
mysql> UPDATE mysql.user SET PASSWORD=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit;

# Restart mysqld
$ /etc/init.d/mysqld stop
$ /etc/init.d/mysqld start

Sunday, June 3, 2007

How to make the 'less' command not clear screen after exit?

You can use the use option:
-X or --no-init

e.g. alias less='less -X'
If you want to use it long term, the PAGER is a useful variable to set in your .bashrc files:
export PAGER='less -X'
To make less exit if the content fits on one screen, use:
-F or --quit-if-one-screen

export ACK_PAGER='less -RFX'