2009年6月14日 星期日

bash介紹(2)

bash當中如有用到色碼,有時候會發現如果打的是較長的指令,有時候會發生換行錯誤的問題,如:
root@localhost> dddddddddddddddddddddddddddddddd

若繼續打字下去會變成
dddddddddlhost> dddddddddddddddddddddddddddddddd

可以發現的是,第一行最前面的提示字元被蓋住了也沒有正確地換行,正確的應該是:
root@localhost> dddddddddddddddddddddddddddddddddddddddd

如果是 bash 的 line-wrap 的問題可以使用

$> shopt -s checkwinsize

或是把下面這行加在 ~/.bashrc 裡

echo "shopt -s checkwinsize" >> ~/.bashrc


來打開 winsize 的功能

但也有可能是另一種情形:設定變數PS1又加上跳脫字元所產生的計算錯誤

解法:
在色碼前後加上\[\]才不會產生計算錯誤,如:

PS1='\033[01;34m\u@\033[01;31m\h \$'

要改為

PS1='\[\033[01;34m\]\u@\[\033[01;31m\]\h \$'



以下是一個簡單的 .bashrc :

# .bashrc
# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -al'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export LS_OPTIONS='--color=auto'
eval `dircolors`
alias ls='ls $LS_OPTIONS'

#Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
green='\e[0;32m'
GREEN='\e[1;32m'
yellow='\e[0;33m'
YELLOW='\e[1;33m'
blue='\e[0;34m'
BLUE='\e[1;34m'
purple='\e[0;35m'
PURPLE='\e[1;35m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
white='\e[0;37m'
WHITE='\e[1;37m'
gray='\e[1;30m'
NC='\e[0m' # No Color
# --> Nice. Has the same effect as using "ansi.sys" in DOS.

# Looks best on a black background.....
echo -e "${gray}This is BASH ${white}${BASH_VERSION%.*}${gray} - DISPLAY on
${white}$DISPLAY${NC}" date
if [ -x /usr/games/fortune ]; then
/usr/games/fortune -s # makes our day a bit more fun.... :-)
fi

function _exit() # function to run upon exit of shell
{
echo -e "${WHITE}Good bye, root!${NC}"
}
trap _exit 0

#---------------
# Shell prompt
#---------------

function fastprompt()
{
unset PROMPT_COMMAND
{
LOAD=$(uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g")
TIME=$(date +%H:%M)
}
case $TERM in
*term | rxvt )
PS1="[\[${WHITE}\]\$TIME]\[${YELLOW}\]\u\[${GREEN}\]@\[${CYAN}\]\w> \[$NC\]"
;;

linux )
PS1="${cyan}[\$TIME - \$LOAD]$NC\n[\h \#] \w > " ;;

* )
PS1="[\$TIME - \$LOAD]\n[\h \#] \w > " ;;
esac
}

fastprompt


[ref] : bash在終端機上換行的問題
The Non-Annoying Terminal Mini How-To & Fun with shopt


沒有留言:

張貼留言