LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > catkin
User Name
Password

Notices

Rate this Entry

Thunar custom actions including "root terminal here" without gksudo

Posted 11-18-2010 at 10:54 AM by catkin
Updated 11-20-2010 at 01:36 AM by catkin (Changed title to mention gksudo)

Here's a way to implement Thunar custom actions using rxvt unicode (urxvt) but could be adapted to other terminal emulators. It has been used to provide:
  • Terminal here
  • root terminal here
  • vi
  • vi as root
Optionally it allows the custom action to set background colour and a fullscreen window.

Caveats: the mechanism is "proof of concept" and not widely tested at the time of posting. It is designed for use on a workstation with a single user.

The key to the mechanism is a script called start_urxvt.sh. Here's its usage.
Code:
usage: start_urxvt.sh [-D] [-b bg] [-d dir] [-f] [-u root] [-v file]
    where:
      -D       Turn debugging on
      -b bg    Background colour.  bg must be valid for urxvt's -bg option. 
      -d dir   Start with dir as current working directory.
               If dir is not a directory its containing directory is used.
               For other than the current user, requires the user to
               service /tmp/start_urxvt.sh.<username>.cwd in .bashrc
      -f       Start fullscreen.
      -h       Print usage and exit
      -u user  Login as user
      -v file  Edit file using vi and exit. 
               For other than the current user, requires the user to
               service /tmp/start_urxvt.sh.<username>.vi in .bashrc

    Sample .bashrc code:
      # Co-operate with start_urxvt.sh
      cwd_file=/tmp/start_urxvt.sh.$USER.cwd
      if [[ -f $cwd_file ]]; then
          cd "$( < $cwd_file )"
          rm $cwd_file 2>/dev/null # Works only for root and same user 
      fi
      unset cwd_file
      vi_file=/tmp/start_urxvt.sh.$USER.vi
      if [[ -f $vi_file ]]; then
          file_to_vi="$( < $vi_file )"
          rm $vi_file 2>/dev/null # Works only for root and same user 
          vi "$file_to_vi"
          exit
      fi
      unset vi_file
Using start_urxvt.sh, a sample Thunar custom action for "root terminal here" is
Code:
start_urxvt.sh -b '#d3e5ef' -d %f -f -u root
It can be configured under Appearance Conditions for all file types, not just directories. If it is called to operate on a file it will start urxvt with the file's containing directory as current working directory.

A vi custom action should only be configured under Appearance Conditions for file types that vi can work with, that is Text Files and maybe Other Files. Here's a sample
Code:
start_urxvt.sh -v %f
Here's start_urxvt.sh
Code:
#!/bin/bash

# Starts rxvt unicode terminal emulator with choice of current working 
# directory, fullscreen, user and vi.
# For usage see usage function or use option -h

# Developed and tested on Slackware 64 13.1 with urxvt unicode (urxvt) 9.07

# 18nov10 Charles
#   * Creation
# 19nov10 Charles
#   * Moved "get current screen size" code into this script
#   * Modified smaple .bashrc vi code to delete file before starting vi
#   * Removed background override for root

# Wish list
#   * Add option for choice of icon (when works in urxvt and environment)

# Programmer's notes
#   * Full paths to commands are used for performance

#--------------------------
# Name: usage
# Purpose: prints usage message
#--------------------------
function usage {
    local msg

    msg="usage: $my_nam [-D] [-b bg] [-d dir] [-f] [-u root] [-v file]"
    if [[ ${1:-} = '' ]]
    then
        msg="$msg$lf(use -h for help)"
        echo "$msg" >&2
    else
        msg="$msg$lf    where:
      -D       Turn debugging on
      -b bg    Background colour.  bg must be valid for urxvt's -bg option. 
      -d dir   Start with dir as current working directory.
               If dir is not a directory its containing directory is used.
               For other than the current user, requires the user to
               service /tmp/$my_nam.<username>.cwd in .bashrc
      -f       Start fullscreen.
      -h       Print usage and exit
      -u user  Login as user
      -v file  Edit file using vi and exit. 
               For other than the current user, requires the user to
               service /tmp/$my_nam.<username>.vi in .bashrc

    Sample .bashrc code:
      # Co-operate with start_urxvt.sh
      cwd_file=/tmp/$my_nam.\$USER.cwd
      if [[ -f \$cwd_file ]]; then
          cd \"\$( < \$cwd_file )\"
          rm \$cwd_file 2>/dev/null # Works only for root and same user 
      fi
      unset cwd_file
      vi_file=/tmp/$my_nam.\$USER.vi
      if [[ -f \$vi_file ]]; then
          file_to_vi=\"\$( < \$vi_file )\"
          rm \$vi_file 2>/dev/null # Works only for root and same user 
          vi \"\$file_to_vi\"
          exit
      fi
      unset vi_file
"
        echo "$msg"
    fi
}

# Configure script environment
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export PATH=/usr/bin:/bin
set -o nounset
unalias -a

# Utility constants
# ~~~~~~~~~~~~~~~~~
readonly false=
readonly lf=$'\n'                           # ASCII linefeed, a.k.a newline
readonly my_nam=${0##*/}
readonly true=true

# Parse command line
# ~~~~~~~~~~~~~~~~~~
bg=
cwd=
debugging=
emsg=
file_to_vi=
fullscreen=$false
user=$( /usr/bin/whoami )
me=$user
while getopts :Db:d:fhu:v: opt
do
    case $opt in
        D )
            debugging=$true
            ;;
        b )
            bg="$OPTARG"
            ;;
        d )
            cwd="$OPTARG"
            ;;
        f )
            fullscreen=$true
            ;;
        h )
            usage -v
            exit 0
            ;;
        u )
            if [[ $OPTARG != '' ]]; then
                user=$OPTARG
            else
                emsg="${lf}No user name given on -u option. Option ignored"
            fi
            case $OPTARG in 
                root )
                    root=$true
                    ;;
                '' )
                    ;;
                * )
                    emsg="${lf}Invalid -u option argument '$OPTARG' ignored"
            esac
            ;;
        v )
            file_to_vi=$OPTARG
            ;;
        * )
            emsg="${lf}Invalid option '-$OPTARG' ignored"
    esac
done

# Test for extra arguments
# ~~~~~~~~~~~~~~~~~~~~~~~~
shift $(( $OPTIND-1 ))
if [[ $* != '' ]]; then
    emsg="${lf}Invalid extra argument(s) '$*'"
fi

# Logging and debug
# ~~~~~~~~~~~~~~~~~
log_afn=/tmp/${0##*/}.$$.log
if [[ $debugging ]]; then
    exec 1>>"$log_afn"
    exec 2>>"$log_afn"
    set -xv
fi

# Report any command line errors
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if [[ $emsg != '' ]]; then
    usage -v > "$log_afn"
    echo "$emsg" >> "$log_afn"
fi

# Build and run the urxvt command
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[[ $bg ]] && bg="-bg $bg" || bg=
if [[ $fullscreen ]]; then
    buf=$( xrandr --current )
    buf=${buf##*current }
    buf=${buf%%,*}
    geometry="-geometry ${buf// /}"
else
    geometry=
fi
[[ -d $cwd ]] || cwd=${cwd%/*}
case "$user" in
    $me )
        if [[ $file_to_vi != '' ]]; then
            exec /usr/bin/urxvt $bg $geometry -e /usr/bin/vi "$file_to_vi" &
        elif [[ $cwd != '' ]]; then
            exec /usr/bin/urxvt --chdir "$cwd" $bg $geometry &
        else
            exec /usr/bin/urxvt $bg $geometry &
        fi
        ;;
    * )
        if [[ $file_to_vi != '' ]]; then
            tmp_file="/tmp/$my_nam.$user.vi"
            echo "$file_to_vi" > "$tmp_file"
        elif [[ $cwd != '' ]]; then
            tmp_file="/tmp/$my_nam.$user.cwd"
            echo "$cwd" > "$tmp_file"
        fi
        if [[ $user = root ]]; then
            # root's .bashrc is expected to remove the temporary file
            exec /usr/bin/urxvt $bg $geometry -e /bin/su - $user &
        else
            # The user cannot remove the temporary file
            /usr/bin/urxvt $bg $geometry -e /bin/su - $user
            rm -f "$tmp_file"
        fi
esac

exit 0
If custom actions are configured using the -u (user) option in combination with the -d (current working directory) or -v (vi) options then that user's .bashrc must be modified to include lines functionally similar to the ones shown when start_urxvt.sh is called with the -h (help) option.

If the -D (debug) option is used, debugging information will be written to /tmp/start_urxvt.sh.<PID>.log where PID is the process id when it was run. The script does not remove these files; it must be done manually.

start_urxvt.sh can be used in desktop launchers too. Here's a launcher command to get a fullscreen, pale blue terminal window as root.
Code:
start_urxvt.sh -b '#d3e5ef' -f -u root
Posted in Uncategorized
Views 1186 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:10 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration