Command disabled: revisions

Shell Scripts

Shell scripts can be used to paste together your own backup solutions or automated user management tasks.

WARNING: if you copy any scripts from a Windows / DOS environment, please be sure to change the encoding from dos/windows (\r\n) to unix (\n). Otherwise the “newline” token will ruin the execution of your scripts.

Change the line separators in JEdit under “Utilities > Buffer Options > Line Separator”.

Useful resources:

Add a Path in Bash

If you have your own special directory for scripts (say, ~/.scripts instead of ~/bin), you have to include the directory to $PATH to make your scripts executable wherever you are.

Add these lines to ~/.bash_profile (or ~/.bashrc in Ubuntu):

PATH=$PATH:~/.scripts
export PATH

Now log in again and you're good to go!

sed

Sed is a non-interactive editor which you can use in your shell scripts to edit files. For instance, I needed to edit the /etc/postfix/virtual file (which contains aliases) in an automated way. Here's a simplified version, based on a list of names from which you want to remove “tom”:

sed -i "/tom/d" "names.txt"

The d option after the pattern /tom/ means: delete the line “tom”. The sed parameter -i means: edit files in place (otherwise, the edited contents would have been sent to stout).

Gnome Launcher With Multiple Commands

Use bash commands to create a Gnome launcher which should execute multiple commands. The configuration file for a gnome launcher is stored in ~/Desktop. It looks something like this:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Games Dev
Type=Application
Terminal=true
Exec=bash -c "cd ~/rails/games;svn update;~/rails/games/script/server"
Name[en_US]=Games Dev
Icon[en_US]=/usr/share/pixmaps/gnome-computer.png
Icon=/usr/share/pixmaps/gnome-computer.png
GenericName[en_US]=
Comment[en_US]=SVN update Games; Start Webrick with Games Dev

You can easily create a new configuration file by right clicking on your (Gnome) desktop, and then choosing “Create Launcher…”. The key here is the command field. This is where you set the Exec field in the configuration file.

To use a single command, simply input the command. To create a launcher which executes multiple commands, specify that a bash shell be used:

bash -c "cd ~/rails/games;svn update;~/rails/games/script/server"

The -c parameter means: execute all commands in the string inside the bash shell.

Replace Spaces In File Names

Non Recursive Oneliner

rename -v 's/\ /\_/g' *

I tested it. This works.

Recursive Version

#!/bin/bash
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1` 
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
do
  for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
  do
    newname=`echo "$name" | tr " " "_"`
    echo "$name" "$newname"
    mv "$name" "$newname"
  done
done

Not tested, but working according to several Ubuntu forum posters.

Here's another recursive version, but this is a oneliner:

find . -iname '*.jpg' -exec rename "s/\(|\)//g" {} \;
find . -iname '*.jpg' -exec rename "s/ /_/g" {} \;
find . -iname '*.jpg' -exec rename "s/__/_/g" {} \;

These code examples replace ( and ) and spaces in jpg file names.


Personal Tools