Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Appending in a switch case statement

by diffredential (Beadle)
on Dec 20, 2008 at 20:05 UTC ( [id://731795]=perlquestion: print w/replies, xml ) Need Help??

diffredential has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have a very simple idea, having a 'go' command, that cd's to the most used directories. As I want to 'go' in the shell , I need to write it as a shell function. So, in the .bashrc file I have:
function go { case $1 in (tmp) cd $HOME/tmp;; (home) cd $HOME/home;; esac }
So I can 'go tmp', 'go home' in the shell and I will go. OK. So say I want to add a new route, I have to append a new statment on the switch case, say:
function go { case $1 in (tmp) cd $HOME/tmp;; (home) cd $HOME/home;; (fd) cd $HOME/foodir;; esac }
With requires editing the .bashrc file. I would love to do instead(on the shell):
go --add fd $HOME/foodir
so the .bashrc file is edited the way I would do it (shown above). Then my approach is, of course, editing the file:
function go { if [ $1 = "--add" ];then perl -pi -ne 's/UNKNOWN/($2) cd $3;;/' ~/.bashrc fi case $1 in (tmp) cd $HOME/tmp;; (home) cd $HOME/home;; esac }
And here comes the extreme power of perl regex. I've been trying to solve the correct regex that would append a new entry on the switch case, but had no luck. If only you monks could help me getting this job done :)

Replies are listed 'Best First'.
Re: Appending in a switch case statement
by ikegami (Patriarch) on Dec 20, 2008 at 20:43 UTC
    Sounds like you want CDPATH. Given the following entry in .bash_profile,
    export CDPATH=.:~:~/www:~/tmp

    you get the desired behaviour with cd:

    [~]$ cd bin /home/ikegami/bin [~/bin]$ cd lib /home/ikegami/lib [~/lib]$ cd example.com /home/ikegami/www/example.com [~/www/adaelis.com]$

    It'll save you from writing a bash parser (although a half-assed one should work).

Re: Appending in a switch case statement
by ikegami (Patriarch) on Dec 20, 2008 at 21:02 UTC

    Now to answer your question directly.

    Adding guide comments simplifies the matter and makes it more reliable.

    function go { if [ $1 = "--add" -o $1 = "--rem" ] ; then perl -e' use strict; use warnings; my ($key, $dir) = @ARGV; local $^I = ".bak"; local @ARGV = "$ENV{HOME}/.bashrc"; while (<>) { my $f = /^\s*# -- BEGIN GO CASES --/ .. /^\s*# -- END GO CASES --/; if (!$f || $f == 1) { } elsif ($f !~ /E/) { $_ = "" if /^\s*\(\Q$key\E\)/; } elsif ($dir) { my ($prefix) = /^(\s*)/; print("$prefix($key) cd $dir;;\n"); } } continue { print } ' $2 $3 else case $1 in # -- BEGIN GO CASES -- (tmp) cd $HOME/tmp;; (home) cd $HOME/home;; # -- END GO CASES -- esac fi }

    Update: Switched to the desired syntax.

    go --add shortcut dir (replaces if it already exists) go --rem shortcut go shortcut

    I only tested the Perl components. The bash components were assumed to be correct.

    Update: Fixed bug in if [ ... ].

Re: Appending in a switch case statement
by zwon (Abbot) on Dec 20, 2008 at 21:53 UTC
    Maybe something like this
    function go { if [ $1 = "--add" ];then perl -pi -ne 's/^\(UNKNOWN/($2) cd $3;;\n(UNKNOWN/' ~/.bashrc fi case $1 in (tmp) cd $HOME/tmp;; (home) cd $HOME/home;; (UNKNOWN) true ;; esac }
    Though actually I don't like the idea
Re: Appending in a switch case statement
by diffredential (Beadle) on Dec 20, 2008 at 22:06 UTC
    Thanks so much :D!! ikegami I'll take a look at CDPATH!! And the script you attached works perfectly!! amazing!! Thanks again :)
Re: Appending in a switch case statement
by rir (Vicar) on Dec 22, 2008 at 20:05 UTC
    <Update> I meant to include my reason for not just using CDPATH. I play around in directory trees like: $HOME/<projectname>/(source|target)/<modulename>/CPAN_structured_tree where I am usually in the bottom of one project's source tree, and occasionally want to jump to the sister-directory in the target or into the source of a different project. I use shell history and a few exported variable names for directories.
    </Update>

    For background, I ran across this cute alias which I never use:

    realias='$EDITOR ~/.aliases; source ~/.aliases'
    In the case you give, I use exported variables and cd. I've never felt the need to have my environment evolve so rapidly that I couldn't use an editor in a spare screen to edit my .bashrc. So I would just:
    $ export tmp=/tmp $ cd $tmp
    To make this more suited to your style, I'd blend this together into something like:
    function reexport { echo export "$*" >> $HOME/.exports; source $HOME/.exports; }
    Given that you perl, perhaps you can stand some dollar signs.

    Be well,
    rir

Re: Appending in a switch case statement
by gube (Parson) on Dec 25, 2008 at 16:34 UTC
    Simply you can use this below code as well in .basrc
    function go { case $1 in (dashboard) cd $HOME/dashboard;; (2.8) cd $HOME/dashboard/2.8;; ($1) cd $HOME/$1;; esac; }
    It will take to your $HOME directory default and from their your path, i meant instead "cd ~/<directories>" :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://731795]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found