linux / bash question

Discussion in 'Computer Science & Culture' started by Blue_UK, Nov 28, 2007.

Thread Status:
Not open for further replies.
  1. Blue_UK Drifting Mind Valued Senior Member

    Messages:
    1,449
    Hi,

    I want to write some scripts that affect what directory I'm in.

    The only problem is that when a script executes 'cd' it changes the directory that the script is running in, which is a subshell. To get around this I wrote an alias in my .bashrc. This is fine - except now my tick'ed statements are going wrong.

    Here's the code
    Code:
    alias build="cd `pwd | sed s'-libs-build/libs-'`"
    So 'build' changes 'my' current working directory. (The sed command inserts a string in to the path that pwd returns).

    The problem is that the command only works once because everything between the ticks (`) is evaluated only once per terminal session. So it's fine for one directory, but henseforth, calling it in other directories will go back to the old one.

    Any solutions from clever bash users out there? (Python would be fine if not bash)
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    I have to admit I'm not entirely sure what you are attempting to do (in regard to your overall end result you are looking for).

    I haven't done anything for a while, but I used sh some time back when running a dedicated, specifically when installing Apache:

    Code:
    #!/bin/sh
    echo Please enter PATH [With trailing slashes]
    echo [i.e. ../ ]
    [COLOR="Blue"]read XPATH[/COLOR]
    cd mod_ssl*
    ./configure -with-apache=[COLOR="Blue"]${XPATH}[/COLOR]apache_#.#.##
    cd ../apache*
    make
    
    The reason I show this is perhaps you can find a way to adapt how the Variable is inputted for use. Of course it does mean that you would likely have a specific script that would be executed every time. (There is also a method for dealing with 'flags' that are added to a executed script [i.e. ./script.sh -flag], however thats beyond my memory/notes.)
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. mapsdnasggeyerg fubar Registered Senior Member

    Messages:
    63
    Try....

    Code:
    alias build="cd \`pwd | sed s'-libs-build/libs-'\`"
    The backslash escapes the backquotes so that they remain in the alias definition, thus defering the evaluation of the backquotes to when the alias is invoked.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Blue_UK Drifting Mind Valued Senior Member

    Messages:
    1,449
    Cheer's for the snippet, Strydes. I tried using variables, having first set them to pwd but didn't work

    Please Register or Log in to view the hidden image!




    that's mapsdanfuckedupname, the escaped tick does what I want it to!

    I can now go from source directories to build directories with having to type "cd ../../../../build/libs/project/syn/.." and other long paths!

    Next script... directory tree traversal and 'cd' to first dir that matches substring!
     
Thread Status:
Not open for further replies.

Share This Page