أحاول طباعة نص في المحطة باستخدام أمر الارتداد.
أريد طباعة النص باللون الأحمر. كيف أقوم بذلك؟
يمكنك استخدام رموز الهروب ANSI :
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
ثم استخدمهم مثل هذا في البرنامج النصي الخاص بك:
# .---------- constant part!
# vvvv vvvv-- the code from above
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"
الذي يطبع love
باللون الأحمر.
من تعليق @ james-lim ، إذا كنت تستخدم الأمر echo
، فتأكد من استخدام علامة -e للسماح بتكرار الخط المائل العكسي .
# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow"
(لا تقم بإضافة "\n"
عند استخدام echo إلا إذا كنت تريد إضافة سطر فارغ إضافي)
يمكنك استخدام الأمر tput
الرائع (المقترح في Ignacio's answer ) لإنتاج رموز التحكم الطرفية لجميع أنواع الأشياء.
تتم مناقشة الأوامر الفرعية tput
المحددة لاحقًا.
استدعاء tput
كجزء من سلسلة الأوامر:
tput setaf 1; echo "this is red text"
استخدم ;
بدلاً من &&
لذلك إذا tput
أخطاء ما زال النص يظهر.
خيار آخر هو استخدام متغيرات Shell:
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"
tput
ينتج تتابعات الأحرف التي يتم تفسيرها من قبل الطرف على أنها لها معنى خاص. لن يتم إظهارهم بأنفسهم. لاحظ أنه لا يزال من الممكن حفظها في ملفات أو معالجتها كمدخلات بواسطة برامج أخرى غير الجهاز الطرفي.
قد يكون أكثر ملاءمة لإدراج إخراج tput
مباشرة في سلاسل echo
الخاصة بك باستخدام أمر بديل :
echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"
ينتج الأمر أعلاه هذا على أوبونتو:
tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape
الألوان هي كما يلي:
Num Colour #define R G B
0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 Magenta COLOR_Magenta 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1
هناك أيضًا إصدارات غير ANSI من وظائف إعداد اللون (setb
بدلاً من setab
، و setf
بدلاً من setaf
) تستخدم أرقامًا مختلفة ، غير مذكورة هنا.
tput bold # Select bold mode
tput dim # Select dim (half-bright) mode
tput smul # Enable underline mode
tput rmul # Disable underline mode
tput rev # Turn on reverse video mode
tput smso # Enter standout (bold) mode
tput rmso # Exit standout mode
tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N # Move N characters forward (right)
tput cub N # Move N characters back (left)
tput cuu N # Move N lines up
tput ll # Move to last line, first column (if no cup)
tput sc # Save the cursor position
tput rc # Restore the cursor position
tput lines # Output the number of lines of the terminal
tput cols # Output the number of columns of the terminal
tput ech N # Erase N characters
tput clear # Clear screen and move the cursor to 0,0
tput el 1 # Clear to beginning of line
tput el # Clear to end of line
tput ed # Clear to end of screen
tput ich N # Insert N characters (moves rest of line forward!)
tput il N # Insert N lines
tput sgr0 # Reset text format to the terminal's default
tput bel # Play a bell
باستخدام compiz windows المتذبذب ، يجعل الأمر bel
المحطة الطرفية تتذبذب لثانية واحدة لجذب انتباه المستخدم.
tput
يقبل البرامج النصية التي تحتوي على أمر واحد لكل سطر ، والتي يتم تنفيذها بالترتيب قبل خروج tput
.
تجنب الملفات المؤقتة عن طريق تكرار سلسلة متعددة الخطوط وأنابيب:
echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red
man 1 tput
man 5 terminfo
للحصول على قائمة كاملة من الأوامر ومزيد من التفاصيل حول هذه الخيارات. (يتم سرد الأمر المقابل tput
في العمود Cap-name
من الجدول الضخم الذي يبدأ في السطر 81.)# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
# High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White
| | bash | hex | octal | NOTE |
|-------+-------+--------+---------+------------------------------|
| start | \e | \x1b | \033 | |
| start | \E | \x1B | - | x cannot be capital |
| end | \e[0m | \x1m0m | \033[0m | |
| end | \e[m | \x1b[m | \033[m | 0 is appended if you omit it |
| | | | | |
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | \e[32m<text> | \x1b[32m<text> | \033[32m<text> | m is NOT optional |
| reset | <text>\e[0m | <text>\1xb[0m | <text>\033[om | o is optional (do it as best practice |
| | | | | |
إذا كنت ستستخدم هذه الرموز في متغيرات bash الخاصة
يجب عليك إضافة أحرف هروب إضافية حتى يتمكن bash من تفسيرها بشكل صحيح. بدون هذا إضافة أحرف الهروب إضافية تعمل ولكنك ستواجه مشاكل عند استخدامك Ctrl + r
للبحث في السجل الخاص بك.
يجب عليك إضافة \[
قبل بدء أي كود ANSI وإضافة \]
بعد أي رموز تنتهي.
مثال:
في الاستخدام العادي: \033[32mThis is in green\033[0m
لـ PS0/1/2/4: \[\033[32m\]This is in green\[\033[m\]
\[
هو لبدء سلسلة من غير قابلة للطباعة أحرف\]
هو في نهاية سلسلة من غير قابلة للطباعة أحرف
نصيحة: لحفظها ، يمكنك أولاً إضافة \[\]
ثم وضع رمز ANSI بينهما:
- \[start-ANSI-code\]
- \[end-ANSI-code\]
قبل الغوص في هذه الألوان ، يجب أن تعرف حوالي 4 أوضاع باستخدام هذه الرموز:
إنه يعدل نمط اللون وليس النص. على سبيل المثال جعل اللون مشرق أو أغمق.
0
reset1;
أخف من المعتاد2;
أغمق من المعتادهذا الوضع غير مدعوم على نطاق واسع. إنه دعم كامل على Gnome-Terminal.
هذا الوضع هو لتعديل نمط النص لا لون.
3;
مائل4;
5;
يومض (بطيء)6;
يومض (سريع)7;
معكوس8;
إخفاء9;
التقاطعويدعم تقريبا.
على سبيل المثال ، يدعم KDE-Konsole 5;
لكن Gnome-Terminal لا يدعم و Gnome يدعم 8;
لكن KDE لا يدعم.
هذا الوضع هو لتلوين المقدمة.
هذا الوضع هو لتلوين الخلفية.
يعرض الجدول أدناه ملخصًا لـ 3/4 بت إصدار ANSI-color
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| color-mode | octal | hex | bash | description | example (= in octal) | NOTE |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 0 | \033[0m | \x1b[0m | \e[0m | reset any affect | echo -e "\033[0m" | 0m equals to m |
| 1 | \033[1m | | | light (= bright) | echo -e "\033[1m####\033[m" | - |
| 2 | \033[2m | | | dark (= fade) | echo -e "\033[2m####\033[m" | - |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| text-mode | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 3 | \033[3m | | | italic | echo -e "\033[3m####\033[m" | |
| 4 | \033[4m | | | underline | echo -e "\033[4m####\033[m" | |
| 5 | \033[5m | | | blink (slow) | echo -e "\033[3m####\033[m" | |
| 6 | \033[6m | | | blink (fast) | ? | not wildly support |
| 7 | \003[7m | | | reverse | echo -e "\033[7m####\033[m" | it affects the background/foreground |
| 8 | \033[8m | | | hide | echo -e "\033[8m####\033[m" | it affects the background/foreground |
| 9 | \033[9m | | | cross | echo -e "\033[9m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| foreground | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 30 | \033[30m | | | black | echo -e "\033[30m####\033[m" | |
| 31 | \033[31m | | | red | echo -e "\033[31m####\033[m" | |
| 32 | \033[32m | | | green | echo -e "\033[32m####\033[m" | |
| 33 | \033[32m | | | yellow | echo -e "\033[33m####\033[m" | |
| 34 | \033[32m | | | blue | echo -e "\033[34m####\033[m" | |
| 35 | \033[32m | | | purple | echo -e "\033[35m####\033[m" | real name: Magenta = reddish-purple |
| 36 | \033[32m | | | cyan | echo -e "\033[36m####\033[m" | |
| 37 | \033[32m | | | white | echo -e "\033[37m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 38 | 8/24 | This is for special use of 8-bit or 24-bit |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| background | ~ | | | ~ | ~ | ~ |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 40 | \033[40m | | | black | echo -e "\033[40m####\033[m" | |
| 41 | \033[41m | | | red | echo -e "\033[41m####\033[m" | |
| 42 | \033[42m | | | green | echo -e "\033[42m####\033[m" | |
| 43 | \033[43m | | | yellow | echo -e "\033[43m####\033[m" | |
| 44 | \033[44m | | | blue | echo -e "\033[44m####\033[m" | |
| 45 | \033[45m | | | purple | echo -e "\033[45m####\033[m" | real name: Magenta = reddish-purple |
| 46 | \033[46m | | | cyan | echo -e "\033[46m####\033[m" | |
| 47 | \033[47m | | | white | echo -e "\033[47m####\033[m" | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
| 48 | 8/24 | This is for special use of 8-bit or 24-bit | |
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
يعرض الجدول أدناه ملخصًا لـ 8 بت إصدار ANSI-color
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | \033[38;5 | \x1b[38;5 | \e[38;5 | standard. normal | echo -e '\033[38;5;1m####\033[m' | |
| 8-15 | | | | standard. light | echo -e '\033[38;5;9m####\033[m' | |
| 16-231 | | | | more resolution | echo -e '\033[38;5;45m####\033[m' | has no specific pattern |
| 232-255 | | | | | echo -e '\033[38;5;242m####\033[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
| 0-7 | | | | standard. normal | echo -e '\033[48;5;1m####\033[m' | |
| 8-15 | | | | standard. light | echo -e '\033[48;5;9m####\033[m' | |
| 16-231 | | | | more resolution | echo -e '\033[48;5;45m####\033[m' | |
| 232-255 | | | | | echo -e '\033[48;5;242m####\033[m' | from black to white |
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
اختبار سريع 8 بت:for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done
يعرض الجدول أدناه ملخصًا لـ 24 بت إصدار ANSI-color
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| foreground | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | R = red | echo -e '\033[38;2;255;0;02m####\033[m' | R=255, G=0, B=0 |
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | G = green | echo -e '\033[38;2;;0;255;02m####\033[m' | R=0, G=255, B=0 |
| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | B = blue | echo -e '\033[38;2;0;0;2552m####\033[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| background | octal | hex | bash | description | example | NOTE |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | R = red | echo -e '\033[48;2;255;0;02m####\033[m' | R=255, G=0, B=0 |
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | G = green | echo -e '\033[48;2;;0;255;02m####\033[m' | R=0, G=255, B=0 |
| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | B = blue | echo -e '\033[48;2;0;0;2552m####\033[m' | R=0, G=0, B=255 |
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
المقدمة ملخص 8 بت في .gif
خلفية 8 بت في .gif
blinking
على كيدي الطرفية أداة أكثر تطوراً قمت بتطويرها للتعامل مع هذه الألوان:
نعم تستطيع. واجهت في باش ، ج ، ج ++ ، دبيرل ، بيثون
أعتقد لا.
3/4-bit نعم ، إذا قمت بترجمة الكود بـ gcc
بعض لقطات الشاشة على Win-7
\033[
= 2 ، الأجزاء الأخرى 1
في أي مكان يحتوي على مترجم tty
xterm
، gnome-terminal
، kde-terminal
، mysql-client-CLI
وهكذا.
على سبيل المثال ، إذا كنت ترغب في تلوين مخرجاتك باستخدام mysql ، يمكنك استخدام Perl
#!/usr/bin/Perl -n
print "\033[1m\033[31m$1\033[36m$2\033[32m$3\033[33m$4\033[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^\w])/g;
قم بتخزين هذا الرمز في اسم ملف: pcc
(= Perl Colorize Character) ثم ضع الملف في PATH
صالحًا ثم استخدمه في أي مكان تريد.
ls | pcc
df | pcc
داخل mysql
قم أولاً بتسجيله لـ pager
ثم حاول:
[user2:db2] pager pcc
PAGER set to 'pcc'
[user2:db2] select * from table-name;
يفعللامقبض يونيكود.
لا ، يمكنهم القيام بالكثير من الأشياء المثيرة للاهتمام. محاولة:
echo -e '\033[2K' # clear the screen and do not move the position
أو:
echo -e '\033[2J\033[u' # clear the screen and reset the position
هناك الكثير من المبتدئين الذين يريدون مسح الشاشة باستخدام system( "clear" )
بحيث يمكنك استخدام هذا بدلاً من system(3)
call
نعم فعلا. \u001b
من السهل استخدام 3/4-bit
، لكن من الدقة والجميلة استخدام 24-bit
.
إذا لم تكن لديك خبرة مع html هنا ، يوجد برنامج تعليمي سريع:
يعني 24 بت: 00000000
و 00000000
و 00000000
. كل 8 بت هو لون معين.24..17
هو من أجل و
16..9
لـ و
8..1
لـ
حتى في أتش تي أم أل#FF0000
يعني وهنا هو:
255;0;0
في أتش تي أم أل#00FF00
يعني وهو هنا:
0;255;0
هل هذا منطقي؟ ما اللون الذي تريده مع هذه القيم الثلاثة 8 بت.
مرجع:
ويكيبيديا
تسلسل هروب ANSI
tldp.org
tldp.org
misc.flogisoft.com
بعض المدونات/صفحات الويب التي لا أتذكرها
استخدم tput
مع القدرة setaf
ومعلمة 1
.
echo "$(tput setaf 1)Hello, world$(tput sgr0)"
echo -e "\033[31m Hello World"
يتحكم [31m
في لون النص:
30
-37
sets المقدمة اللون40
-47
sets الخلفية اللونقائمة أكثر اكتمالا من رموز الألوان يمكن العثور عليها هنا .
من الممارسات الجيدة إعادة تعيين لون النص إلى \033[0m
في نهاية السلسلة.
هذا هو تبديل اللون \033[
. انظر التاريخ .
يشبه اللون الأكواد رمز 1;32
(الضوء الأخضر) ، 0;34
(الأزرق) ، 1;34
(الضوء الأزرق) ، إلخ.
نقوم بإنهاء تسلسل اللون باستخدام مفتاح اللون \033[
و 0m
، رمز اللون {no-. تمامًا مثل فتح علامات التبويب وإغلاقها بلغة الترميز.
SWITCH="\033["
NORMAL="${SWITCH}0m"
YELLOW="${SWITCH}1;33m"
echo "${YELLOW}hello, yellow${NORMAL}"
وظيفة حل echo
بسيطة اللون:
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo "$text"
}
cecho "Normal"
cecho y "Yellow!"
هناك طريقة دقيقة لتغيير اللون لأحد echo
هي تحديد هذه الوظيفة:
function coloredEcho(){
local exp=$1;
local color=$2;
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
Magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput setaf $color;
echo $exp;
tput sgr0;
}
الاستعمال:
coloredEcho "This text is green" green
أو يمكنك استخدام أكواد اللون المذكورة مباشرة في إجابة Drew :
coloredEcho "This text is green" 2
استخدم tput
لحساب أكواد اللون. تجنب استخدام رمز الهروب ANSI (على سبيل المثال \E[31;1m
باللون الأحمر) لأنه أقل قدرة على الحركة. Bash على OS X ، على سبيل المثال ، لا يدعمها.
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
Magenta=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
BOLD=`tput bold`
RESET=`tput sgr0`
echo -e "hello ${RED}some red text${RESET} world"
تمت الإجابة على هذا السؤال مرارًا وتكرارًا :-) ولكن لماذا لا.
أول استخدام tput
هو أكثر قابلية للنقل في البيئات الحديثة من الحقن اليدوي ASCII الرموز من خلال echo -E
فيما يلي وظيفة bash سريعة:
say() {
echo "[email protected]" | sed \
-e "s/\(\(@\(red\|green\|yellow\|blue\|Magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\[email protected]/g" \
-e "s/@red/$(tput setaf 1)/g" \
-e "s/@green/$(tput setaf 2)/g" \
-e "s/@yellow/$(tput setaf 3)/g" \
-e "s/@blue/$(tput setaf 4)/g" \
-e "s/@Magenta/$(tput setaf 5)/g" \
-e "s/@cyan/$(tput setaf 6)/g" \
-e "s/@white/$(tput setaf 7)/g" \
-e "s/@reset/$(tput sgr0)/g" \
-e "s/@b/$(tput bold)/g" \
-e "s/@u/$(tput sgr 0 1)/g"
}
الآن يمكنك استخدام:
say @[email protected][[Success]]
للحصول على:
tput
تم تحميل شفرة المصدر tput(1)
لأول مرة في سبتمبر 1986
tput(1)
كانت متوفرة في دلالات لعنة X/Open في التسعينات (1997 معيار له دلالات المذكورة أدناه).
لذلك ، ( تماما ) في كل مكان.
لقد قمت للتو بدمج المصيد الجيد في جميع الحلول وانتهى بي الأمر:
cecho(){
RED="\033[0;31m"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
# ... ADD MORE COLORS
NC='\033[0m' # No Color
printf "${!1}${2} ${NC}\n"
}
ويمكنك فقط تسميتها باسم:
cecho "RED" "Helloworld"
بفضل @ k-five لهذه الإجابة
declare -A colors
#curl www.bunlongheng.com/code/colors.png
# Reset
colors[Color_Off]='\033[0m' # Text Reset
# Regular Colors
colors[Black]='\033[0;30m' # Black
colors[Red]='\033[0;31m' # Red
colors[Green]='\033[0;32m' # Green
colors[Yellow]='\033[0;33m' # Yellow
colors[Blue]='\033[0;34m' # Blue
colors[Purple]='\033[0;35m' # Purple
colors[Cyan]='\033[0;36m' # Cyan
colors[White]='\033[0;37m' # White
# Bold
colors[BBlack]='\033[1;30m' # Black
colors[BRed]='\033[1;31m' # Red
colors[BGreen]='\033[1;32m' # Green
colors[BYellow]='\033[1;33m' # Yellow
colors[BBlue]='\033[1;34m' # Blue
colors[BPurple]='\033[1;35m' # Purple
colors[BCyan]='\033[1;36m' # Cyan
colors[BWhite]='\033[1;37m' # White
# Underline
colors[UBlack]='\033[4;30m' # Black
colors[URed]='\033[4;31m' # Red
colors[UGreen]='\033[4;32m' # Green
colors[UYellow]='\033[4;33m' # Yellow
colors[UBlue]='\033[4;34m' # Blue
colors[UPurple]='\033[4;35m' # Purple
colors[UCyan]='\033[4;36m' # Cyan
colors[UWhite]='\033[4;37m' # White
# Background
colors[On_Black]='\033[40m' # Black
colors[On_Red]='\033[41m' # Red
colors[On_Green]='\033[42m' # Green
colors[On_Yellow]='\033[43m' # Yellow
colors[On_Blue]='\033[44m' # Blue
colors[On_Purple]='\033[45m' # Purple
colors[On_Cyan]='\033[46m' # Cyan
colors[On_White]='\033[47m' # White
# High Intensity
colors[IBlack]='\033[0;90m' # Black
colors[IRed]='\033[0;91m' # Red
colors[IGreen]='\033[0;92m' # Green
colors[IYellow]='\033[0;93m' # Yellow
colors[IBlue]='\033[0;94m' # Blue
colors[IPurple]='\033[0;95m' # Purple
colors[ICyan]='\033[0;96m' # Cyan
colors[IWhite]='\033[0;97m' # White
# Bold High Intensity
colors[BIBlack]='\033[1;90m' # Black
colors[BIRed]='\033[1;91m' # Red
colors[BIGreen]='\033[1;92m' # Green
colors[BIYellow]='\033[1;93m' # Yellow
colors[BIBlue]='\033[1;94m' # Blue
colors[BIPurple]='\033[1;95m' # Purple
colors[BICyan]='\033[1;96m' # Cyan
colors[BIWhite]='\033[1;97m' # White
# High Intensity backgrounds
colors[On_IBlack]='\033[0;100m' # Black
colors[On_IRed]='\033[0;101m' # Red
colors[On_IGreen]='\033[0;102m' # Green
colors[On_IYellow]='\033[0;103m' # Yellow
colors[On_IBlue]='\033[0;104m' # Blue
colors[On_IPurple]='\033[0;105m' # Purple
colors[On_ICyan]='\033[0;106m' # Cyan
colors[On_IWhite]='\033[0;107m' # White
color=${colors[$input_color]}
white=${colors[White]}
# echo $white
for i in "${!colors[@]}"
do
echo -e "$i = ${colors[$i]}I love you$white"
done
آمل أن يكون هذا صورة تساعدك على اختيار لونك للباش الخاص بك: د
تعمل هذه الرموز في صندوق Ubuntu الخاص بي:
echo -e "\x1B[31m foobar \x1B[0m"
echo -e "\x1B[32m foobar \x1B[0m"
echo -e "\x1B[96m foobar \x1B[0m"
echo -e "\x1B[01;96m foobar \x1B[0m"
echo -e "\x1B[01;95m foobar \x1B[0m"
echo -e "\x1B[01;94m foobar \x1B[0m"
echo -e "\x1B[01;93m foobar \x1B[0m"
echo -e "\x1B[01;91m foobar \x1B[0m"
echo -e "\x1B[01;90m foobar \x1B[0m"
echo -e "\x1B[01;89m foobar \x1B[0m"
echo -e "\x1B[01;36m foobar \x1B[0m"
هذا يطبع الحروف a b c d جميع بألوان مختلفة:
echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"
لحلقة:
for (( i = 0; i < 17; i++ ));
do echo "$(tput setaf $i)This is ($i) $(tput sgr0)";
done
إذا كنت ترغب في تحسين إمكانية القراءة من التعليمات البرمجية ، يمكنك echo
السلسلة أولاً ثم إضافة اللون لاحقًا باستخدام sed
:
echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'
إجابتي المفضلة حتى الآن ملونة.
فقط لنشر خيار آخر ، يمكنك التحقق من هذه الأداة الصغيرة xcol
https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/
تستخدمه تمامًا مثل grep ، وسيقوم بتلوين stdin الخاص به بلون مختلف لكل وسيطة ، على سبيل المثال
Sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." LISTEN ESTABLISHED TIME_WAIT
لاحظ أنه يقبل أي تعبير منتظم يقبله sed.
تستخدم هذه الأداة التعريفات التالية
#normal=$(tput sgr0) # normal text
normal=$'\e[0m' # (works better sometimes)
bold=$(tput bold) # make colors bold/bright
red="$bold$(tput setaf 1)" # bright red text
green=$(tput setaf 2) # dim green text
fawn=$(tput setaf 3); beige="$fawn" # dark yellow text
yellow="$bold$fawn" # bright yellow text
darkblue=$(tput setaf 4) # dim blue text
blue="$bold$darkblue" # bright blue text
purple=$(tput setaf 5); Magenta="$purple" # Magenta text
pink="$bold$purple" # bright Magenta text
darkcyan=$(tput setaf 6) # dim cyan text
cyan="$bold$darkcyan" # bright cyan text
gray=$(tput setaf 7) # dim white text
darkgray="$bold"$(tput setaf 0) # bold black = dark gray text
white="$bold$gray" # bright white text
يمكنني استخدام هذه المتغيرات في البرامج النصية الخاصة بي مثل ذلك
echo "${red}hello ${yellow}this is ${green}coloured${normal}"
للتوسع في هذه الإجابة ، بالنسبة لنا كسول:
function echocolor() { # $1 = string
COLOR='\033[1;33m'
NC='\033[0m'
printf "${COLOR}$1${NC}\n"
}
echo "This won't be colored"
echocolor "This will be colorful"
لم يلاحظ أحد فائدة كود ANSI 7 الفيديو المقلوب .
يظل قابلاً للقراءة على أي ألوان مخططات طرفية أو خلفيات سوداء أو بيضاء أو لوحات خيالية أخرى عن طريق مبادلة ألوان المقدمة والخلفية.
مثال لخلفية حمراء تعمل في كل مكان:
echo -e "\033[31;7mHello world\e[0m";
هذا هو كيف يبدو عند تغيير مخططات المحطة الطرفية المدمجة:
هذا هو البرنامج النصي للحلقة المستخدم في GIF.
for i in {30..49};do echo -e "\033[$i;7mReversed color code $i\e[0m Hello world!";done
راجع https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
يجب عليك بالتأكيد استخدام tput على تسلسل التحكم ANSI الخام.
نظرًا لوجود عدد كبير من لغات التحكم الطرفية المختلفة ، عادةً ما يحتوي النظام على طبقة اتصال وسيطة. يتم البحث عن الرموز الحقيقية في قاعدة بيانات لنوع المحطة المكتشفة حاليًا ويمكنك تقديم طلبات موحدة إلى API أو (من Shell) إلى أمر.
أحد هذه الأوامر هو
tput
.tput
يقبل مجموعة من الاختصارات تسمى أسماء القدرات وأي معلمات ، إذا كان ذلك مناسبًا ، ثم يبحث عن تسلسل الهروب الصحيح للطرف الذي تم اكتشافه في قاعدة بيانات terminfo ويطبع الرموز الصحيحة (نأمل أن يفهم الجهاز الطرفي).
من http://wiki.bash-hackers.org/scripting/terminalcodes
ومع ذلك ، كتبت مكتبة مساعدة صغيرة تسمى bash-tint ، والتي تضيف طبقة أخرى أعلى tput ، مما يجعلها أسهل في الاستخدام (imho):
مثال: tint "white(Cyan(T)Magenta(I)Yellow(N)Black(T)) is bold(really) easy to use."
لقد كتبت غنيمة لتحقيق ذلك تماما.
يمكنك فقط القيام به
pip install Swag
يمكنك الآن تثبيت جميع أوامر الهروب كملفات txt إلى وجهة معينة من خلال:
Swag install -d <colorsdir>
أو حتى أسهل عبر:
Swag install
والتي سوف تثبيت الألوان إلى ~/.colors
.
إما أن تستخدمها مثل هذا:
echo $(cat ~/.colors/blue.txt) This will be blue
أو بهذه الطريقة ، التي أجدها أكثر إثارة للاهتمام في الواقع:
Swag print -c red -t underline "I will turn red and be underlined"
التحقق من ذلك على أسينيما !
وهذا ما اعتدت أن أراه كل المجموعة وأقرر ما الذي يقرأ بشكل رائع:
for (( i = 0; i < 8; i++ )); do
for (( j = 0; j < 8; j++ )); do
printf "$(tput setab $i)$(tput setaf $j)(b=$i, f=$j)$(tput sgr0)\n"
done
done
إليكم نص بسيط بسيط ، قمت مؤخرًا بوضعه ، والذي من شأنه تلوين أي مدخلات عبر الأنابيب بدلاً من استخدام "المرحاض".
File: color.bsh
#!/usr/bin/env bash
## A.M.Danischewski 2015+(c) Free - for (all (uses and
## modifications)) - except you must keep this notice intact.
declare INPUT_TXT=""
declare ADD_LF="\n"
declare -i DONE=0
declare -r COLOR_NUMBER="${1:-247}"
declare -r ASCII_FG="\\033[38;05;"
declare -r COLOR_OUT="${ASCII_FG}${COLOR_NUMBER}m"
function show_colors() {
## perhaps will add bg 48 to first loop eventually
for fgbg in 38; do for color in {0..256} ; do
echo -en "\\033[${fgbg};5;${color}m ${color}\t\\033[0m";
(($((${color}+1))%10==0)) && echo; done; echo; done
}
if [[ ! $# -eq 1 || ${1} =~ ^-. ]]; then
show_colors
echo " Usage: ${0##*/} <color fg>"
echo " E.g. echo \"Hello world!\" | figlet | ${0##*/} 54"
else
while IFS= read -r PIPED_INPUT || { DONE=1; ADD_LF=""; }; do
PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}")
INPUT_TXT="${INPUT_TXT}${PIPED_INPUT}${ADD_LF}"
((${DONE})) && break;
done
echo -en "${COLOR_OUT}${INPUT_TXT}\\033[00m"
fi
ثم نسميها باللون الأحمر (196):$> echo "text you want colored red" | color.bsh 196
تشير إلى:
echo_red(){
echo -e "\e[1;31m$1\e[0m"
}
echo_green(){
echo -e "\e[1;32m$1\e[0m"
}
echo_yellow(){
echo -e "\e[1;33m$1\e[0m"
}
echo_blue(){
echo -e "\e[1;34m$1\e[0m"
}