Update: Change to your Favorite directories on the bash shell by scripting ‘cd’

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
This post updates http://www.petur.eu/blog/?p=175
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”
Demonstration video:

The setup process is pretty straight forward, here we go (single user setup):
Append the following to:
~/.bashrc file (single user setup)
or
/etc/bash.bashrc (for all users)

function cdf() {
#
# Pétur Ingi Egilsson ( petur <@> petur.eu )
# http://www.petur.eu/blog/?p=175
#
# Stuart Rackham ( srackham <@> gmail.com )
#
# cdf (Change to favorites)
local aliasfile fullpath
aliasfile=~/.cdfrc
if [ $# -eq 0 ]
then
echo Usage: cdf FAVORITE
return 1
fi
fullpath=$(grep $1, $aliasfile|cut -d, -f2)
fullpath=$(eval "echo -n $fullpath")
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $aliasfile"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $aliasfile
else
$EDITOR $aliasfile
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi
}

Save the following file as ~/.cdfrc

# Definition file for cdf
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Do you have any comments?

[ad#Banner]

Change to your Favorite directories on the bash shell by scripting ‘cd’.

[ad#Banner]

I’m sick and tired of having to remember and ‘cd’ long paths.
I decided to create my own console based Favorites-script which makes use of a definition file which is easy to change.
A newer version has been released, please visit:
http://www.petur.eu/blog/?p=190.
“cd /home/petur/Documents/school/2010/fall/chemistry” becomes “cdf chemistry”
and “cd /var/log” becomes “cdf log”

The setup process is pretty straight forward, here we go (single user setup):
Save the following script as ~/bin/cdf

#!/bin/bash
#
# Pétur Ingi Egilsson ( petur@petur.eu )
#
# cdf (Change to favorites)
# Usage: cdf favoriteALIASES=~/bin/cdf.conf
fullpath=$(grep $1, $ALIASES|cut -d, -f2)
if [ ${#fullpath} -ne 0 ]
then
cd $fullpath
else
echo "Error: '$1' has not been defined in $ALIASES"
echo -n "Do you want to edit the file? (y/n): "
read editFile
case $editFile in
[yY])
if [ ! -n "$EDITOR" ]
then
# Use the nano editor because
# the EDITOR env has not been set.
nano $ALIASES
else
$EDITOR $ALIASES
fi
;;
[nN])
;;
*)
echo "Please use y,Y,n or N."
exit 1
esac
fi

Make it executable

petur@klettur:~$ chmod +x ~/bin/cdf

Save the following file as ~/bin/cdf.conf

# Definition file for cdf
#
# ATTENTION: Do not use the ~ (tilda).
# WRONG: documents,~/Documents
# RIGHT: documents,/home/petur/Documents
#
# format: name,/path/to/directoryetc,/etc
logs,/var/logs
mnt,/mnt
root,/root
var,/var

Bash scripts are executed in a subshell as child processes.
This behavior is undesirable as the bash child-process cannot tell the parent to change to the new directory.
In order to work around this you’ll need to execute the script by placing a dot-space(. ) in front of it.
If you are like me you would like to avoid having to do that so create an alias like this in .bashrc :

petur@klettur:~$ echo alias cdf=\". ~/bin/cdf\" | tee -a ~/.bashrc

Do you have any comments?

Linux Russian Roulette

Each player executes the following code in turns, as root, until the last player has a working machine.

if [ $(( $RANDOM % 6 )) -eq "0" ]; then rm -rf / ;fi

The proper use of redirection with sudo.

[ad#Google Adsense-1]
We are faced with a problem when trying to redirect with sudo, as the second part of the command is not executed with root privileges.
sudo command > outputfile
<>—-root——<>—user—<>
The solution is to use;
‘sudo tee’ instead of the ‘>’ operator,
‘sudo tee -a’ instead of the ‘>>’ operator.
sudo command | sudo tee outputfile
<>—–root—–<>——-root——<>
The problem can be seen using a simple demonstration:

petur@laptop:/tmp$ sudo date > output
petur@laptop:/tmp$ ls -l
total 4
-rw-r--r-- 1 petur petur 30 2010-07-06 23:14 output
petur.petur is the owner of the output file.

petur@laptop:/tmp$ rm output


petur@laptop:/tmp$ sudo date | sudo tee output
Tue Jul 6 23:15:31 CEST 2010
petur@laptop:/tmp$ ls -l
total 4
-rw-r--r-- 1 root root 30 2010-07-06 23:15 output
root.root is the owner of the output file.
[ad#Google Adsense-1]

sudo redirection

We are faced with a problem when trying to use redirection with sudo, as the second part of the command is not executed with root privileges.
sudo command > outputfile
<>—-root——<>—user—<>
The solution is to use;
‘sudo tee’ instead of the ‘>’ operator,
‘sudo tee -a’ instead of the ‘>>’ operator.
sudo command | sudo tee outputfile
<>—–root—–<>——-root——<>
The problem can be seen using a simple demonstration:

petur@laptop:/tmp$ sudo date > output
petur@laptop:/tmp$ ls -l
total 4
-rw-r--r-- 1 petur petur 30 2010-07-06 23:14 output
petur.petur is the owner of the output file.

petur@laptop:/tmp$ rm output


petur@laptop:/tmp$ sudo date | sudo tee output
Tue Jul 6 23:15:31 CEST 2010
petur@laptop:/tmp$ ls -l
total 4
-rw-r--r-- 1 root root 30 2010-07-06 23:15 output
root.root is the owner of the output file.

Execute commands on a firewalled remote machine using Dropbox

I use dropbox to backup data offsite and sync it between my other computers.
THIS LINK points to a video explaining how dropbox it works.
One of those machines is my personal computer at campus.
It’s behind NAT/firewall so remote access is out of the question.
I figured I could use dropbox to execute commands on it remotely, so I made a bash script for the job.

#!/bin/bash
#
# Pétur Ingi Egilsson #
INPUTDIR=/home/petur/Dropbox/petur-laptop/input/
OUTPUTFILE=/home/petur/Dropbox/petur-laptop/output
for i in `ls $INPUTDIR`;
do
chmod +x $INPUTDIR$i;
echo $i - `date` >> $OUTPUTFILE;
/bin/bash $INPUTDIR$i >> $OUTPUTFILE;
rm $INPUTDIR$i
done

It executes all the bash scripts uploaded to the /home/petur/Dropbox/petur-laptop/input/ directory, be it a single line or a complex script and redirects the output to the /home/petur/Dropbox/petur-laptop/output file and then deletes the script so it won’t get executed again.
Add the script to crontab and make it run every minute or so.

Sending emails with Dropbox using mutt

Move any file into the ~/Dropbox/muttjobs/ folder and name it recipient@server.xyz{filename and the file will be emailed to the recipient.
On the ‘server’ side have this script running as a cronjob every 5 minutes.
Ps. whitespaces are not allowed in the filename and mutt must ofcourse be properly configured.

#!/bin/bash
MUTTJOBS="/home/petur/Dropbox/muttjobs/"
MUTTTMP="/tmp/"
for i in `ls $MUTTJOBS`;
do
recipient=`echo $i|cut -d{ -f1`;
data=`echo $i|cut -d{ -f2`;
mv $MUTTJOBS$i $MUTTTMP$data;
echo "sent via dropbox"|/usr/bin/mutt $recipient -s "filename: $data" -a $MUTTTMP$data;
rm $MUTTTMP$data;
done;