#!/bin/bash
printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0)
for((i=0; i<=7; i++)); do
echo $(tput setaf $i)"show me the money"$(tput sgr0)
done
printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n'
for((i=0,j=7; i<=7; i++,j--)); do
echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0)
done
exit 0
输出格式控制函数:
#!/bin/bash
# $1 str print string
# $2 color 0-7 设置颜色
# $3 bgcolor 0-7 设置背景颜色
# $4 bold 0-1 设置粗体
# $5 underline 0-1 设置下划线
function format_output(){
str=$1
color=$2
bgcolor=$3
bold=$4
underline=$5
normal=$(tput sgr0)
case "$color" in
0|1|2|3|4|5|6|7)
setcolor=$(tput setaf $color;) ;;
*)
setcolor="" ;;
esac
case "$bgcolor" in
0|1|2|3|4|5|6|7)
setbgcolor=$(tput setab $bgcolor;) ;;
*)
setbgcolor="" ;;
esac
if [ "$bold" = "1" ]; then
setbold=$(tput bold;)
else
setbold=""
fi
if [ "$underline" = "1" ]; then
setunderline=$(tput smul;)
else
setunderline=""
fi
printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n"
}
format_output "Yesterday Once more" 2 5 1 1
exit 0
光标属性例子:
#!/bin/bash
# clear the screen
tput clear
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
# set a foreground colour using ANSI escape
tput setaf 3
echo "XYX Corp LTD."
tput sgr0
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
tput cup 7 15
echo "1\. User Management"
tput cup 8 15
echo "2\. service Management"
tput cup 9 15
echo "3\. Process Management"
tput cup 10 15
echo "4\. Backup"
# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
tput clear
tput sgr0
tput rc
exit 0