Present Directory for Prompt

Discussion in 'Android Hacks and Help' started by mammlouk, Dec 17, 2009.

  1. mammlouk

    mammlouk Member

    Joined:
    Dec 13, 2009
    Messages:
    316
    Likes Received:
    0
    Trophy Points:
    16
    Ratings:
    +0
    Hey guys I know someone asked for this before, but I couldn't remember who or where. Here is how I setup my prompt to show the current directory. First you are basically writing a function called "nd" which in calls cd to the argument you pass and sets the primary prompt to the value of the command "pwd". For those not linux/unix savvy please note that those are backticks around the command pwd and not single quotes. ::Edit:: Forgot to say that the second line makes cd call the nd function! ::/Edit::

    Code:
     
    nd() { cd $* ; PS1="`pwd` #"; }
    alias cd=nd
    
    I've got it in a script so I don't have to type it everytime, but I'll leave that as an independent choice. It's good experience for those that don't already know how!
     
    Last edited: Dec 17, 2009
  2. lvlln

    lvlln Member

    Joined:
    Nov 10, 2009
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Ratings:
    +0
    That works quite well, thank you. But is it possible to make it so that it displays # only when you're in as root, and $ otherwise?
     
  3. mammlouk

    mammlouk Member

    Joined:
    Dec 13, 2009
    Messages:
    316
    Likes Received:
    0
    Trophy Points:
    16
    Ratings:
    +0
    You could build it into your script. Just use whoami in an if statement to check the userid, if it is 0 then you are root. You will probably need to use sed to get exactly what you want out of the whoami output. I'm not familiar enough with sed to guide you on that, but a little googling, or maybe someone else's help can get you what you need.

    ::Edit:: Alternatively I suppose you could just check against the whole return output. you can use "id -u" in place of "whoami". Something like this pseudocode
    Code:
     
    CUID=$(id -u)
    if [ $CUID = "uid=0(root) gid=0(root) ]; then
       nd() { cd $* ; PS1="`pwd` #"; }
    else
      nd() { cd $* ; PS1="`pwd` $"; }
    fi
    alias cd=nd
    
     
    Last edited: Dec 17, 2009
  4. mammlouk

    mammlouk Member

    Joined:
    Dec 13, 2009
    Messages:
    316
    Likes Received:
    0
    Trophy Points:
    16
    Ratings:
    +0
    Update - Finally tested the script I had written for this and it doesn't work. I forgot that scripts basically run in a subshell which means the function and alias are not applicable to the parent shell. Sorry, guess you'll just have to keep it handy for copy paste. Maybe a more apt scripter has a way to accomplish this.