Nick's Tech Blog: script

Pages

Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, August 17, 2012

Use sed to remove '^M' characters from a file

Simple stuff, this post is here more as a reference than anything.

Problem

Windows puts '^M' characters throughout text files. I want to remove them.

Solution

The following commands will strip out the '^M' characters, and put the contents into a new file, then overwrite the original file.
sed -e s/^M//g oldfile.txt > newfile.txt
mv newfile.txt oldfile.txt
To get the '^M' character, type Ctrl+V,Ctrl+M

Thursday, September 22, 2011

Fedora > install Xvfb as a service

Problem

I required Xvfb to be installed, and running as a service on a server.

Assumptions: you know how to use chkconfig, yum, vi and chmod.

Solution
  1. yum install xorg-x11-server-Xvfb
  2. vi /etc/init.d/xvfbd (copy contents below into the file, and save it)
  3. chmod 755 /etc/init.d/xvfbd (now we have a service)
  4. chkconfig --add xvfbd
  5. chkconfig --level 2345 xvfbd on

You can now start your service with:
sudo service xvfbd start

Check it's running:
ps -ef | grep Xvfb

Check it's listed to startup automatically for runlevels 2,3,4,5:
chkconfig --list


/etc/init.d/xvfbd

#!/bin/bash
#
# description: Xvfb server
#


# Get function from functions library
. /etc/init.d/functions


# Start the service Xvfb
start() {
        echo -n "Starting Xvfb server: "
        /usr/bin/Xvfb &
        ### Create the lock file ###
        touch /var/lock/subsys/Xvfb
        success $"Xvfb server startup"
        echo
}


# Restart the service Xvfb
stop() {
        echo -n "Stopping Xvfb server: "
        killproc Xvfb
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/Xvfb
        echo
}


### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status Xvfb
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac


exit 0

See also:
http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

Tuesday, August 23, 2011

Linux > tag files in a directory by directory name

Work in progress...


#!/bin/bash
#
# "tagdirs.sh"
#
# Simple script which iterates over the first level folders in a given directory,
# and tags the files in those subdirectories with the parent folder's name.
#
# Usage: ./tagdirs.sh
#
# N.B. Requires 'tracker-tag'.
#

# The folder to iterate over.
FOLDER=$1

# Check param is a directory...
if [ -d "${1}" ] ; then
echo "Processing directory '${1}'..."
else
echo "Error: bad argument. Expected a valid directory name for the first argument"
echo "Bad directory name = ${1}"
exit 1
fi

(
IFS=$'\n'
# For each directory...
for dir in $(find $FOLDER -maxdepth 1 -mindepth 1 -type d); do
# Strip directory to folder name only...
tag=${dir##*/}
# For each file in the directory...
for file in $(find $dir -maxdepth 1 -mindepth 1 -type f); do
echo "Adding tag '${tag}' to file '${file}'...";
tracker-tag --add="${tag}" "${file}"
done
done
)

exit 0