autoxinitrc.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env bash
  2. # Place a template xinitrc in ~/.xinitrc based on the system one without
  3. # default exec lines (twm, xterm etc.) This solves the problem for new users
  4. # who don't know how to base their ~/.xinitrc on /etc/X11/xinit/xinitrc. It
  5. # should make it easier for them use the template from the system file.
  6. # Written by Adnan Shameem; License: MIT (Expat)
  7. #
  8. # Usage:
  9. # - Make sure you have xorg, xinit installed on your system
  10. # - Optional, replace your desired exec command on the last echo statement of this script
  11. # - Run: ./autoxinitrc.sh
  12. # Automatically remove default exec lines from system xinitrc and put it on ~.
  13. # Add your custom lines afterwards to the destination file.
  14. # Params:
  15. # - $1: Source filename (default: /etc/X11/xinit/xinitrc) (optional)
  16. # - $2: Destination filename (default: $HOME/.xinitrc) (optional)
  17. function autoxinitrc() {
  18. local etcpath=$([ -d /usr/local/etc ] && echo /usr/local/etc || echo /etc)
  19. # Default vars
  20. [ -z "$1" ] && local filename="$etcpath/X11/xinit/xinitrc" || local filename="$1"
  21. [ -z "$2" ] && local destfile="$HOME/.xinitrc" || local destfile="$2"
  22. # To store sed expression for line deletion
  23. local exp=''
  24. # Line on which last paragraph started
  25. local para_start=1
  26. # Is last line blank
  27. local last_blank=1
  28. # Does paragraph has 'exec ' line
  29. local para_has_exec=0
  30. # Read file into array
  31. readarray -t lines < "$filename"
  32. for i in $(seq 1 "${#lines[@]}")
  33. do
  34. # Stores the current line contents
  35. line="${lines[$i]}"
  36. # Reached paragraph end
  37. if [ -z "$line" ]; then
  38. last_blank=1
  39. if [ "$para_has_exec" = "1" ]; then
  40. exp+="${para_start},${i}d;"
  41. fi
  42. para_has_exec=0 # reset exec search for paragraph
  43. # Inside a paragraph, encountered a non-empty line
  44. elif [ ! -z "$line" ]; then
  45. if [ "$last_blank" = "1" ]; then # first line of paragraph
  46. para_start=$i
  47. para_has_exec=0
  48. last_blank=0
  49. fi
  50. if echo "$line" | grep -e 'exec\s' &>/dev/null; then
  51. para_has_exec=1
  52. fi
  53. fi
  54. done
  55. if [ -f "$HOME/.xinitrc" ]; then
  56. mv "$HOME/.xinitrc" "$HOME/.xinitrc.bak"
  57. echo "Original ~/.xinitrc has been backed up as ~/.xinitrc.bak"
  58. fi
  59. cp "$filename" "$destfile"
  60. sed -i'' -e "$exp" "$destfile"
  61. }
  62. # Place a template xinitrc in ~/.xinitrc without default exec lines
  63. autoxinitrc
  64. # Actual exec commands we want when 'startx' is run
  65. echo "
  66. # Run openbox
  67. exec openbox-session
  68. " >> "$HOME/.xinitrc"