Register
It is currently Wed Apr 23, 2014 11:33 am

startx wrapper script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Mar 19, 2010 3:15 pm   

Joined: Tue Mar 16, 2010 11:45 pm
Posts: 2
I run multiple X sessions, my primary WM ratpoison on TTY7, then a development ratpoison on TTY8. Since I like to boot different configuration files per which RP enviroment I want I wrote this little script to allow me to specify what configuration I want when calling startx. The script uses the directory ~/.xinit as the place holder for all configurations following the naming convention of xinit0, xinit1, xinitN. I suggest you backup your original ~/.xinitrc before running this.

I plan to expand on this script and add some features sometime(some rainy day) but for now it suites my fancy so I shall leave it be. I would love to hear any and all feedback on it.

If the desired screen isnt specified on the command line the script will attempt to detect if a session of X is allready running, if so it will increment the screen number by one. if not it will default to 0.

Code:
#!/bin/bash                                                                                                                                                                                                                       

# Synopsis:   Small wrapper for startx to allow for easy command line specification
#             of startx options
# Author:     thims (root DOT packet AT gmail DOT com)
# Date:       20090316
# Comments:   
#
#
# Configuration file directory tree and naming convention
# By default the configuration directory is set to ~/.xinit but
# this can be changed in the "$confdir" variable.
# $ tree ~/.xinit
#.xinit
# |-- xinit0
# |-- xinit1
# |-- xinit2
# |-- xinit3
# `-- ...


# ToDo:
# - Add the ability to detect a custom naming convention from the name of "$defaultConf"
# - Add the ability to check and make a backup of original .xinitrc file?


defaultConf="xinit0"
confdir="$HOME/.xinit"


while [ $# -gt 0 ]
do
  case "$1" in
    "-h"|"--help")
 
    ;;
       
    "-s"|"--screen")
      if [ -z "$2" ] ;then
        echo "Error: -s requires exactly one argument, 0 provided"
        exit 1
      else
        Xsessions=$(ls /tmp/.X11-unix/ | grep "$2" >/dev/null ;echo $? )
        if [ $Xsessions == 0 ] ;then
          echo "Error: that X screen slot is allready filled, try another"
        else
          screen="$2"
        fi
      fi
    ;;

    "-c"|"--config")
      if [ -z "$2" ] ;then
        echo "Error: -c requires exactly one argument, 0 provided"
      elif [ ! -e "$confdir/xinit$2" ] ;then
        echo "Error: $confdir/xinit$2 does not exist, please create it or specify another config"
      else
        ln -snf "$confdir/xinit$2" "$HOME/.xinitrc"
        conf=true
      fi
    ;;

  esac
  shift
done


if [ -z $screen ] ;then
  if [ $(ls /tmp/.X11-unix/ | wc -l) -lt 1 ] ;then
    screen=0
  else
    screen=$(ls /tmp/.X11-unix/ | tail -1 | sed 's/[a-zA-Z=]//g')
    screen=$((screen + 1))
  fi
fi

if [ -z $conf ] ;then
  ln -sf "$confdir/$defaultConf" "$HOME/.xinitrc"                                                                                                                                                                                 
fi

startx :$screen


Top
 Profile  
 PostPosted: Fri Mar 19, 2010 10:40 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
The only external command really needed is ln, any other is there because you can't use bash !
Code:
#!/bin/bash

defaultConf="xinit0"
confdir="$HOME/.xinit"
usage="TODO"

while [ $# -gt 0 ]
do
  case "$1" in
    -h|--help) echo "$usage"
    ;;
    -s|--screen) if [ -z "$2" ] ;then
                    echo "Error: -s requires exactly one argument, 0 provided"
                    exit 1
                 else
                    [ -f "/tmp/.X11-unix/$2" ] && screen="$2" || echo "Error: that X screen slot is allready filled, try another"
                 fi
    ;;
    -c|--config) if [ -z "$2" ] ;then
                    echo "Error: -c requires exactly one argument, 0 provided"
                 elif [ ! -e "$confdir/xinit$2" ] ;then
                    echo "Error: $confdir/xinit$2 does not exist, please create it or specify another config"
                 else
                    ln -snf "$confdir/xinit$2" "$HOME/.xinitrc"
                    ln -sf "$confdir/$defaultConf" "$HOME/.xinitrc"
                 fi
    ;;
  esac
  shift
done

if [ -z "$screen" ] ;then
   shopt -s nullglob
   X11unix=( /tmp/.X11-unix/* )
   shopt -u nullglob
   [ "${#X11unix[@]}" -lt 1 ] && screen=0 || {
      screen="${X11unix[@]: -1}"
      screen="${screen//[a-zA-Z]/}"
      screen=$((screen++))
   }
fi

startx :$screen
I didn't tested it, don't hesitate to tell what/where/when it fails


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: deb and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP