LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with script bash to show the current folder (http://www.linuxquestions.org/questions/programming-9/problem-with-script-bash-to-show-the-current-folder-4175452386/)

Ibis 03-02-2013 08:58 AM

Problem with script bash to show the current folder
 
Hi i'm a young novice linux user.
I found this script to show in the prompt bash the current directory

Code:

#!/bin/bash

function prompt_command {
        # save the current position
tput sc
        # backwash is where to position the cursor
        # to write the whole current working directory
        # we back up 2 more for the brackets
let backwash=$(tput cols)-$(echo $(pwd) | wc -m)-2
        # position the cursor at Y=0, X=calculated length
tput cup 0 ${backwash}
        # set foreground color, bold
tput setaf 4 ; tput bold
        # wrap the full path in brackets
echo -n "["
        # set the color of the current path
tput setaf 6
        # show the path
echo -n "$(pwd)"
        # set the color of the closing bracket
tput setaf 4 ; tput bold
        # show the closing bracket
echo -n "]"
        # return the cursor to the saved position
tput rc
}

PROMPT_COMMAND=prompt_command

GREEN="\[$(tput setaf 2 ; tput bold)\]"
WHITE="\[$(tput setaf 7 ; tput bold)\]"
NO_COLOUR="\[$(tput sgr0)\]"
case $TERM in
        xterm*|rxvt*)
        TITLEBAR='\[\033]0;\u@\h \007\]'
        ;;
    *)
        TITLEBAR=""
        ;;
esac

PS1="${TITLEBAR}\
$GREEN\u@\h \
$WHITE\$$NO_COLOUR "
PS2='> '
PS4='+ '

And that's the problem:


http://www.fileden.com/files/2008/1/4/1679401/term.jpg

Thanks for help and sorry for my bad english!

Habitual 03-02-2013 09:43 AM

http://wiki.bash-hackers.org/doku.php

Ibis 03-02-2013 10:06 AM

Thanks, I'll look for a solution ;)

EDIT:
It was very simple! I just added these lines:
Code:

tput cup 0 0
tput el

I don't think that it's useful, but if could help someone that's the code of the script:

Code:

#!/bin/bash

function prompt_command {
        # save the current position
tput sc
        # move the cursor at 0 0 and delete the line
tput cup 0 0
tput el
        # backwash is where to position the cursor
        # to write the whole current working directory
        # we back up 2 more for the brackets
let backwash=$(tput cols)-$(echo $(pwd) | wc -m)-2
        # position the cursor at Y=0, X=calculated length
tput cup 0 ${backwash}
        # set foreground color, bold
tput setaf 4 ; tput bold
        # wrap the full path in brackets
echo -n "["
        # set the color of the current path
tput setaf 6
        # show the path
echo -n "$(pwd)"
        # set the color of the closing bracket
tput setaf 4 ; tput bold
        # show the closing bracket
echo -n "]"
        # return the cursor to the saved position
tput rc
}

PROMPT_COMMAND=prompt_command

GREEN="\[$(tput setaf 2 ; tput bold)\]"
WHITE="\[$(tput setaf 7 ; tput bold)\]"
NO_COLOUR="\[$(tput sgr0)\]"
case $TERM in
        xterm*|rxvt*)
        TITLEBAR='\[\033]0;\u@\h \007\]'
        ;;
    *)
        TITLEBAR=""
        ;;
esac

PS1="${TITLEBAR}\
$GREEN\u@\h \
$WHITE\$$NO_COLOUR "
PS2='> '
PS4='+ '

P.S. it doesn't work as root

onebuck 03-03-2013 08:33 AM

Moderator Response
 
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 02:37 AM.