Linux Tips and Tricks

Monday, May 29, 2006

Linux users that can only perform predefined tasks

One of my clients want to create a user in his server that can only perform certain tasks, idiot proof, using a menu written in bash. I have to make sure that:

1.) When the user logs in, the menu will be shown immediately
- This is easy, I'll just launch the menu script in .bash_profile or .bash_rc
2.) The user must not be able to terminate the script without logging out.
- This took me a while to figure out. But eventually, I found out that I can do this by changing the interrupt and the suspend key. This can be done by calling `stty intr ""` and `stty susp ""` respectively in the script.

Saturday, May 13, 2006

querying a mysql table that has a '-' in its name

simply querying the table like:

mysql>SELECT * FROM people-table;

will produce:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server ve rsion for the right syntax to use near '-table' at line 1

The answer is, we must enclose the table name with the tick ` symbol.

mysql> SELECT * FROM `people-table`;

will give the desired results.

How to read a file line by line in bash scripting?

This script reads demonstrate how to read a file line by line.

#!/bin/bash

FILE=$1

# Redirect the contents of the file to stdin. This line can be removed if the lines will be piped to this script.
exec<$FILE

read line
while [[ "$line" != "" ]]
do
# Process the line here
#
done

Bash/Sed implementation of PHP add_slashes function

This script will read lines from stdin end replace the ' with \' This is perfect for parsing text files and composing SQL queries.

#!/bin/bash

#filename: add_slashes.sh

read line
while [[ "$line" != "" ]]
do
echo "$line" | sed "s/'/\\\\'/g"
read line
done

Usage example: echo "Heherson's dog" | ./add_slashes.sh will produce "Heherson\'s dog"