VirtualBox

Changeset 36658 in vbox for trunk/src/VBox/Installer


Ignore:
Timestamp:
Apr 12, 2011 3:40:51 PM (14 years ago)
Author:
vboxsync
Message:

Additions/linux/installer: shell script refactoring

Location:
trunk/src/VBox/Installer/linux
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Installer/linux/runasroot.sh

    r36626 r36658  
    11#!/bin/sh
     2# $Id$
    23## @file
    34# VirtualBox privileged execution helper script for Linux and Solaris
     
    1617#
    1718
    18 # Deal with differing "which" semantics
    19 mywhich() {
    20     which "$1" 2>/dev/null | grep -v "no $1"
    21 }
    22 
    23 # Get the name and execute switch for a useful terminal emulator
    24 #
    25 # Sets $gxtpath to the emulator path or empty
    26 # Sets $gxttitle to the "title" switch for that emulator
    27 # Sets $gxtexec to the "execute" switch for that emulator
    28 # May clobber $gtx*
    29 # Calls mywhich
    30 getxterm() {
    31     # gnome-terminal uses -e differently to other emulators
    32     for gxti in "gnome-terminal --title -x" "konsole --title -e" "xterm -T -e"; do
    33         set $gxti
    34         gxtpath="`mywhich $1`"
    35         case "$gxtpath" in ?*)
    36             gxttitle=$2
    37             gxtexec=$3
    38             return
    39             ;;
    40         esac
    41     done
    42 }
    43 
    44 # Quotes its argument by inserting '\' in front of every character save
    45 # for 'A-Za-z0-9/'.  Prints the result to stdout.
    46 quotify() {
    47     echo $1 | sed -e 's/\([^a-zA-Z0-9/]\)/\\\1/g'
    48 }
     19#include sh-utils.sh
    4920
    5021ostype=`uname -s`
  • trunk/src/VBox/Installer/linux/sh-utils.sh

    r36626 r36658  
    1 #!/bin/sh
     1# $Id$
     2# Shell script include file
    23## @file
    3 # VirtualBox privileged execution helper script for Linux and Solaris
     4# Shell script routines which are likely to be useful for different scripts
    45#
    56
     
    4546# for 'A-Za-z0-9/'.  Prints the result to stdout.
    4647quotify() {
    47     echo $1 | sed -e 's/\([^a-zA-Z0-9/]\)/\\\1/g'
     48    echo "$1" | sed -e 's/\([^a-zA-Z0-9/]\)/\\\1/g'
    4849}
    49 
    50 ostype=`uname -s`
    51 if test "$ostype" != "Linux" && test "$ostype" != "SunOS" ; then
    52   echo "Linux/Solaris not detected."
    53   exit 1
    54 fi
    55 
    56 case "$#" in "2"|"3")
    57     ;;
    58     *)
    59     echo "Usage: `basename $0` DESCRIPTION COMMAND [ADVICE]" >&2
    60     echo >&2
    61     echo "Attempt to execute COMMAND with root privileges, displaying DESCRIPTION if" >&2
    62     echo "possible and displaying ADVICE if possible if no su(1)-like tool is available." >&2
    63     exit 1
    64     ;;
    65 esac
    66 
    67 DESCRIPTION=$1
    68 COMMAND=$2
    69 ADVICE=$3
    70 PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11/bin
    71 
    72 case "$ostype" in SunOS)
    73     PATH=$PATH:/usr/sfw/bin:/usr/gnu/bin:/usr/xpg4/bin:/usr/xpg6/bin:/usr/openwin/bin:/usr/ucb
    74     GKSU_SWITCHES="-au root"
    75     ;;
    76     *)
    77     GKSU_SWITCHES=""
    78     ;;
    79 esac
    80 
    81 case "$DISPLAY" in ?*)
    82     KDESUDO="`mywhich kdesudo`"
    83     case "$KDESUDO" in ?*)
    84         eval "`quotify "$KDESUDO"` --comment `quotify "$DESCRIPTION"` -- $COMMAND"
    85         exit
    86         ;;
    87     esac
    88 
    89     GKSU="`mywhich gksu`"
    90     case "$GKSU" in ?*)
    91         # Older gksu does not grok --description nor '--' and multiple args.
    92         # @todo which versions do?
    93         # "$GKSU" --description "$DESCRIPTION" -- "$@"
    94         # Note that $GKSU_SWITCHES is NOT quoted in the following
    95         "$GKSU" $GKSU_SWITCHES "$COMMAND"
    96         exit
    97         ;;
    98     esac
    99     ;;
    100 esac # $DISPLAY
    101 
    102 # pkexec may work for ssh console sessions as well if the right agents
    103 # are installed.  However it is very generic and does not allow for any
    104 # custom messages.  Thus it comes after gksu.
    105 PKEXEC="`mywhich pkexec`"
    106 case "$PKEXEC" in ?*)
    107     eval "\"$PKEXEC\" $COMMAND"
    108     exit
    109     ;;
    110 esac
    111 
    112 # The ultimate fallback is running 'su -' within an xterm.  We use the
    113 # title of the xterm to tell what is going on.
    114 case "$DISPLAY" in ?*)
    115     SU="`mywhich su`"
    116     case "$SU" in ?*)
    117         getxterm
    118         case "$gxtpath" in ?*)
    119             "$gxtpath" "$gxttitle" "$DESCRIPTION - su" "$gxtexec" su - root -c "$COMMAND"
    120             exit
    121             ;;
    122         esac
    123     esac
    124 esac # $DISPLAY
    125 
    126 # Failure...
    127 case "$DISPLAY" in ?*)
    128     echo "Unable to locate 'pkexec', 'gksu' or 'su+xterm'. $ADVICE" >&2
    129     ;;
    130     *)
    131     echo "Unable to locate 'pkexec'. $ADVICE" >&2
    132     ;;
    133 esac
    134 
    135 exit 1
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette