Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Playing with code found on Perlmonks

by oko1 (Deacon)
on Apr 13, 2008 at 16:10 UTC ( #680105=snippet: print w/replies, xml ) Need Help??

When I answer questions here, I often want to see exactly what kind of errors the posted code is going to throw - so I copy it, open a file in 'vi', paste it in, and (following a careful look at it to make sure that it's not going to do anything nasty to me) run it. The additional bits - e.g., adding on a Perl shebang and running 'chmod +x' on the file - are already shortcuts in my 'vi', but I thought that it would be nice to automate this part, at least. I've got "pmedit" linked to an icon on my Gnome toolbar, so all I have to do now is select the code and click the icon. The displayed file will already contain the code that I highlighted.

The following is a Bourne shell script, and requires 'Xdialog'. Please feel free to modify for other OSes and situations. Constructive comments are highly welcomed. :)

# Created by Ben Okopnik on Sun Apr 13 11:22:45 EDT 2008

cd /tmp
label="New filename:"
while :
    fname=`Xdialog --stdout --inputbox "$label" 7 40`
    # Weird: '-f' doesn't handle '~', so we'll do it by hand
    fname=`echo $fname|sed 's/~/\/home\/ben/g'`

    if [ -f "$fname" ]
        label="\"$fname\" already exists. New name:"
        [ "$fname" = "" ] && exit
        xclip -o > "$fname"
        xterm -e vi "$fname"

Replies are listed 'Best First'.
Re: Playing with code found on Perlmonks
by ysth (Canon) on Apr 14, 2008 at 03:30 UTC
    I'm surprised Xdialog doesn't have a specific direct filename mode. I installed it to try it out, and (for me, anyway) it dragged in gtk 1.2 libraries, so I poked around for a gtk 2.0 equivalent and found zenity, which has handy "--file-selection --save" options.

      That's really odd - I thought (based on what it looks like) that 'Xdialog' was one of the old guard; the Athena Toolkit or something like that. Bleh. :(

      On the other hand, modifying it to use none of those libraries, at least for the dialog end, is trivial: change 'Xdialog' to 'dialog', remove the 'xterm -e' line from the 'vi' invocation, and launch the whole script as 'xterm -e pmedit'.

      Human history becomes more and more a race between education and catastrophe. -- HG Wells
Re: Playing with code found on Perlmonks
by skx (Parson) on Apr 14, 2008 at 11:53 UTC

    This could be rewritten:

    fname=`echo $fname|sed 's/~/\/home\/ben/g'`


    fname=`echo $fname|perl -pe "s#~#$HOME#g"`

      Oh - I hadn't realized we were golfing. In Unix, as in Perl, TMTOWTDI.

      # Bourne shell and derivatives fname=`eval echo $fname` # Modern shells (e.g., Bash) fname=${fname/\~/$HOME}

      Your turn, m'sieu? :)

      Human history becomes more and more a race between education and catastrophe. -- HG Wells
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://680105]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2018-04-19 10:20 GMT
Find Nodes?
    Voting Booth?