Îå¸öÏÊΪÈËÖªµÄÏÖ´ú Bash ¾ç±¾±àдÊÖÒÕ
bashÏÂÁîÓïÑÔ³£±»³ÌÐòÔ±ÓÃÀ´±àдshell¾ç±¾£¬ÒÔʵÏÖÊÖ¶¯Ê¹ÃüµÄ×Ô¶¯»¯¡£ËûÃÇ»á±àдbash½ÅÔÀ´×Ô¶¯»¯ÖÖÖÖÉèÖá¢Îļþ²Ù×÷¡¢¹¹½¨Ð§¹ûÌìÉúºÍÓëdevopsÏà¹ØµÄÔ˶¯¡£ÏÕЩËùÓÐÀàunix»ò»ùÓÚunixµÄ²Ù×÷ϵͳ¶¼ÎªÓû§ÌṩԤװÖõÄbashÚ¹ÊÍÆ÷£¬Òò´ËÎÒÃÇ¿ÉÒÔʹÓÃbash±àд¸ü¾ß¿ÉÒÆÖ²ÐÔµÄ×Ô¶¯»¯¾ç±¾¡£
ÕýÈçÎÒÃÇÒѾ֪µÀµÄÄÇÑù£¬Bash¾ç±¾±àдÊÇָʹÓÃBashÏÂÁîÓïÑÔµÄÓï·¨¡¢ÄÚÖÃBashÏÂÁîºÍ½¹µã²Ù×÷ϵͳCLI³ÌÐò£¨ÈçGNU½¹µã¹¤¾ß£©±àдһϵÁÐÏÂÁî¡£Ò»Ñùƽ³£¶øÑÔ£¬×ñÕչŰåµÄBash¾ç±¾»áÖ´ÐÐһЩÏÂÁî²¢ÔÚÖÕ¶ËÉÏÊä³ö´¿Îı¾¡£
ÎÒÃÇ¿ÉÒÔʹÓü¸¸öÌرðµÄÀíÄîÀ´Ê¹×ðÁú¿Ê± Bash ¾ç±¾Ô½·¢ÊÊÓÃÓÚδÀ´ÇÒÔ½·¢Óû§ÓѺá£Ê¹ÓÃһЩ²»Ì«³£¼ûµÄÏÖ´úBash¾ç±¾±àд¼¼ÇÉ£¬¿ÉÒÔÁîÄúµÄ×Ô¶¯»¯¾ç±¾¸ü¾ßÏÖ´ú¸Ð£¬º¸ÇÒÔÏ¿´·¨¡£
ÏÔʾ¶¯»ÒÔָʾ³¤Ê±¼äÔËÐеÄʹÃü
ÔÚijЩÇéÐÎÏ£¬ÎÒÃÇÐèÒª´ÓBash¾ç±¾ÖÐÖ´Ðг¤Ê±¼äÔËÐеÄÏÂÁî¡£Óм¸ÖÖÒªÁì¿ÉÒÔָʾ³¤Ê±¼äÔËÐеÄʹÃü¡£ÏÔʾ´øÓÐÈý¸öµãµÄÐÂÎÅ£¨Ò²³ÆΪʡÂԺţ©£¬×î¼òÆÓºÍ×îÈÝÒ×µÄÒªÁìÊÇʹÓÃechoÏÂÁȻ¶ø£¬ÕâÖÖÊÖÒÕËù±¬·¢µÄÐÅÏ¢ÊǾ²Ì¬µÄ£¬È±·¦»¥¶¯ÐÔºÍÓû§ÓѺÃÐÔ£¬¹ØÓÚ¿ª·¢Ö°Ô±Ò²ÊÇÔÆÔÆ¡£
ÇáËÉչʾASCII¶¯»Ö»ÐèʹÓý¹µãUnix²Ù×÷ϵͳÏÂÁîºÍBashÄÚÖÃÏÂÁî¼´¿É¡£¿´ÏÂÃæÕâ¸öÖ»ÓÐÁ½¸ö¶¯»Ö¡µÄ¼òÆÓ¶¯»Ê¾Àý£º
#!/bin/bash# LinuxÃÔ www.linuxmi.comwhile true;do# Frame #1printf "r Loading..." sleep 0.5 done
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄBash¾ç±¾ÔÚÖÕ¶ËÉÏÏÔʾһ¸öÎÞ¾¡µÄÁ½Ö¡¶¯»¡£printfÓï¾äʹÓÃrתÒå×Ö·ûÀ´ÖØÖÃÄ¿½ñÐеÄÖն˹â±ê¡£ÉÏÃæµÄ¾ç±¾·ºÆðÁËÒÔÏ»ùÓÚÎı¾µÄ¶¯»£º
Ò»¸ö¼òÆÓµÄÁ½Ö¡Îı¾¶¯»
ÎÒÃÇ¿ÉÒÔÔÚ¶¯»ÖÐÌí¼Ó¸ü¶àÖ¡£¬²¢Ê¹ÓÃÒÔÏÂBash¾ç±¾ÔÚÌض¨ºÄʱʹÃüÍê³É֮ǰһֱÏÔʾ¶¯»¡£
#!/bin/bash# LinuxÃÔ www.linuxmi.comsleep 5 &pid=$!frames="/ | \ -"while kill -0 $pid 2&>1 > /dev/null;dofor frame in $frames;doprintf "r$frame Loading..."sleep 0.5donedoneprintf "n"
µÇ¼ºó¸´ÖÆ
¸Ã¾ç±¾½«ÏÔʾһ¸ö»ùÓÚÎı¾µÄÐýתÆ÷¶¯»£¬Ö±µ½¾ÓÉ5ÃëµÄsleepÏÂÁîÍê³ÉÖ´ÐС£Í¬ÑùµØ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÐýתÆ÷¶¯»À´È¡´ú¾²Ì¬ÐÂÎÅ£¬ÔÚÖ´ÐÐÈκÎÐèÒªºÄʱµÄʹÃüʱÏÔʾ¡£Ô¤ÀÀÈçÏ¡£
Ò»¸ö´øÓÐÐýתÆ÷Îı¾¶¯»µÄBash¾ç±¾£¬×÷ÕߵĽØͼ ÕâЩ»ùÓÚÎı¾µÄ¶¯»Ê¹ÏÂÁîÐгÌÐòÔ½·¢Óû§ÓѺúͻ¥¶¯£¬Òò´Ë´ó´ó¶¼ÏÖ´úÏÂÁîÐгÌÐò¶¼ÏÔʾÕâЩASCII¶¯»¡£ÏÖÔÚÄãÖªµÀÔõÑùͨ¹ý¸Ä±äÖ¡×Ö·ûÁÐ±í£¬ÎªÄãµÄBash¾ç±¾Ìí¼Ó¶¯»Ð§¹û¡£
´ÓBashÏÔʾÍâµØGUI֪ͨ
³£ÓõĿª·¢Ö°Ô±Í¨³£»áʹÓÃBash¾ç±¾ºÍÏÂÁîÐгÌÐòÀ´Ìṩ¹¤¾ßµÄÖն˽çÃæ¡£ÀýÈ磬Flutter¿ò¼ÜΪ¿ª·¢Ö°Ô±ÌṩÁËflutterÏÂÁîÐгÌÐò£¬ÓÃÓÚÖÎÀíFlutterÓ¦ÓóÌÐòºÍÉèÖÃFlutterÓ¦ÓóÌÐò¿ª·¢ÇéÐΡ£¼ÙÉèÄãÕýÔÚ¿ª·¢Ò»¸öºÄʱµÄÏÂÁîÐо籾¡£ÔÚÓû§²»Éó²éÖÕ¶ËÊä³öʱ£¬ÔõÑùÏÔʾÖ÷ÒªÐÂÎÅ£¿
GUI֪ͨÔÚËùÓÐUnixÀàºÍ»ùÓÚUnixµÄ²Ù×÷ϵͳÖж¼¿ÉÒÔʹÓÃÌض¨µÄÏÂÁîÀ´ÏÔʾ¡£ÎÒÃÇ¿ÉÒÔ´ÓBash¾ç±¾ÖÐŲÓÃÕâЩÏÂÁîÀ´ÏÔʾÍâµØ֪ͨ¡£ºÃ±È£¬ÔÚÒÔGNU/LinuxΪ»ù´¡µÄ²Ù×÷ϵͳÉÏ£¬Äã¿ÉÒÔʹÓÃnotify-sendÏÂÁʾÀýÈçÏ£º¡£
#!/bin/bash# LinuxÃÔ www.linuxmi.comsleep 10notify-send "notify.sh" "Task #1 ÒÑÀÖ³ÉÍê³É www.linuxmi.com"
µÇ¼ºó¸´ÖÆ
һЩGNU/Linux¿¯Ðаæͨ³£»áԤװnotify-send¹¤¾ßµÄ°æ±¾¡£ÉÏÃæµÄBash¾ç±¾ÔÚÊ®ÃëºóÏÔʾÁËÒ»¸öÍâµØ֪ͨ¡£Ô¤ÀÀÈçÏ£º
ÔÚUbuntuÉϵÄÍâµØ֪ͨ½Øͼ
macOSÓû§¿ÉÒÔͨ¹ýÔÚBashÖÐÖ´ÐÐAppleScriptÚ¹ÊÍÆ÷À´ÏÔʾÍâµØ֪ͨ£¬ÈçÏÂËùʾ£º
#!/bin/bashsleep 10osascript -e "display notification "Task #1 www.linuxmi.com ÒÑÀÖ³ÉÍê³É" with title "notify.sh""
µÇ¼ºó¸´ÖÆ
ÔÚBash¾ç±¾ÖеĶàÀú³Ì´¦Öóͷ£ºÜÊÇÓÐÖúÓÚͨ¹ý³¤Ê±¼äÔËÐеľ籾»òÓÀÊÀÔËÐеĺǫ́¾ç±¾ÏòÓû§Í¨ÖªÖ÷ÒªÊÂÎñ¡£
ÔÚBash¾ç±¾ÖеĶàÀú³Ì´¦Öóͷ£
ͨ³££¬³ÌÐòԱʹÓÃBash¾ç±¾°´Ë³ÐòÔËÐÐÏÂÁî¡£ÒÔÊÇ£¬BashÚ¹ÊÍÆ÷»áÒ»¸ö½ÓÒ»¸öµØÔËÐÐÿ¸öÓï¾ä£¬Ö±µ½µÖ´ïÔ´ÎļþµÄ×îºó»òÓöµ½Ò»¸öexitÓï¾ä¡£È»¶ø£¬ÎÒÃÇ¿ÉÒԸıäÕâÖÖ˳ÐòÖ´Ðеķ½·¨£¬ÊµÏÖ²¢ÐÐÖ´ÐÐÒÔ¼ÓËÙ×Ô¶¯»¯¾ç±¾µÄËÙÂÊ¡£
ÎÒÃÇ¿ÉÒÔ½«Õû¸ö Bash ½ÅÌìÖ°³É¼¸¸ö×ÔÁ¦µÄº¯Êý£¬´Ó¶øÈÃÿ¸öº¯Êý¿ÉÒÔÒì²½ÔËÐС£½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½«ËùÓÐBashº¯Êý×÷Ϊºǫ́ʹÃüÔËÐС£×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÄÚÖõÄwaitÏÂÁîÀ´¼á³Ö¾ç±¾Ö´ÐÐÀú³Ì´¦ÓÚÔ˶¯×´Ì¬£¬Ö±µ½ËùÓÐÒì²½Àú³ÌÖ´Ðп¢Ê¡£
Çë¿´ÒÔÏÂʾÀý´úÂ룺
#!/bin/bash# LinuxÃÔ www.linuxmi.comfunction task1() {echo "Running task1..."sleep 5}function task2() {echo "Running task2..."sleep 5}task1 &task2 &waitecho "www.linuxmi.com ËùÓÐÍê³É"
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«Á½¸ö²¢Ðк¯Êýtask1ºÍtask2×÷Ϊºǫ́ʹÃüÔËÐС£ÎÒÃÇ»¹Ê¹ÓÃÁËÄÚÖõÄwaitÏÂÁîÀ´È·±£¾ç±¾ÊµÀý¼á³ÖÔ˶¯×´Ì¬£¬Ö±µ½ºǫ́ʹÃüÍê³ÉÖ´ÐС£ÈôÊÇÄã¼ì²é¾ç±¾µÄÖ´ÐÐʱ¼ä£¬Äã»á·¢Ã÷ÕâÁ½¸öº¯ÊýÔÚԼĪÎåÃëÖÓÄÚ¿¢Ê£¬¶ø²»ÊÇÊ®ÃëÖÓ¡£
ʹÓÃBashÏÔʾGUI×é¼þ
ÎÒÃÇ֮ǰ̽ÌÖÁËÔõÑùʹÓÃBash¾ç±¾ÔËÐÐÒ»¶Îʱ¼äºóÏÔʾGUI֪ͨ¡£Í¬ÑùµØ£¬ÎÒÃÇ¿ÉÒÔչʾÆäËûµÄGUI×é¼þ£¬ÀýÈçÌáÐÑ¿ò¡¢Îı¾¿òºÍÎļþÑ¡ÔñÆ÷¡£ÓÐʱÎÒÃÇÐèҪΪ·ÇÊÖÒÕÖ°Ô±½¨Éè»ùÓÚGUIµÄ×Ô¶¯»¯³ÌÐò¡£ÈôÊÇÄãÒѾÏàʶBash¾ç±¾±àд£¬Äã²»ÐèҪʹÓÃÆäËûGUI¿ª·¢¹¤¾ßÀ´½¨Éè¼òÆÓµÄGUIÓ¦ÓóÌÐò¡ª¡ªÓÉÓÚÎÒÃÇ¿ÉÒÔʹÓÃBashÏÔʾGUIÔªËØ¡£
zenity³ÌÐòÌṩÁ˼¸¸öÏÂÁÓÃÓÚÔÚGNU/LinuxÏÂÁîÐÐÇéÐÎÖÐÏÔʾÖÖÖÖGUIÔªËØ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî·¿ªÎļþÑ¡Ôñ¶Ô»°¿ò¡£
zenity --file-selection
µÇ¼ºó¸´ÖÆ
Zenity°æ±¾Í¨³£Ô¤×°ÔÚ´ó´ó¶¼»ùÓÚGNOMEµÄGNU/Linux²Ù×÷ϵͳ¿¯ÐаæÖС£±ðµÄ£¬×÷ΪÌæ»»£¬Äã¿ÉÒÔÔÚ»ùÓÚKDEµÄGNU/Linux¿¯ÐаæÖÐʹÓÃkdialogÏÂÁî¡£
macOSÓû§¿ÉÒÔÏñÍù³£Ò»ÑùŲÓÃAppleScriptÚ¹ÊÍÆ÷¡£ÀýÈ磬ËûÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîͨ¹ýBash¾ç±¾ÏÔʾÎļþÑ¡Ôñ¶Ô»°¿ò¡£
osascript -e "POSIX path of (choose file)"
µÇ¼ºó¸´ÖÆ
ÄãÒ²¿ÉÒÔÏñZenityÄÇÑù¹¹½¨×Ô¼ºµÄ¶þ½øÖÆÎļþ£¬²¢´ÓBash¾ç±¾ÖÐÏÔʾÈκÎGUI×é¼þ¡£±ðµÄ£¬Ä㻹¿ÉÒÔʹÓÃNeutralinojsͨ¹ýBash¾ç±¾ÔÚÍâµØ/Ô¶³Ì´°¿ÚÖÐÏÔʾ½»»¥Ê½µÄHTML½çÃæ¡£
ʹÓÃÎı¾ÑùʽÏÖ´ú»¯ÖÕ¶ËÊä³ö
ÎÒÃǾ³£Ê¹ÓÃechoÏÂÁîÊä³ö¾ç±¾ÖеÄÐÂÎÅ¡£Ä¬ÈÏÇéÐÎÏ£¬echoÏÂÁîƾ֤ĬÈÏÖÕ¶ËÎı¾Ñùʽ´òÓ¡¸ø¶¨µÄÎı¾¡£ÔÚÌض¨ÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔƾ֤Ŀ½ñÇéÐÎÓ¦Óûù±¾µÄÓû§ÌåÑéÔÔòÀ´ÌáÉýÎı¾Ñùʽ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓúìÉ«ÏÔʾ¹ýʧÐÂÎÅ¡£±ðµÄ£¬Äú¿ÉÒÔʹÓôÖÌå×ÖÌåÑùʽǿµ÷¶ÎÂäÖеÄһЩҪº¦Ï¸½Ú¡£
tputÊÇÒ»¸öUnix³ÌÐò£¬Ìṩ¿ØÖÆÄ¿½ñÖÕ¶ËÆÁÄ»µÄÏÂÁî¡£ËüÌṩÁ˸ıäÖն˹â±êλÖᢻñÈ¡ÖÕ¶ËÐÅÏ¢ºÍ¸ü¸ÄÎı¾ÑùʽµÄÏÂÁî¡£ÎÒÃÇ¿ÉÒÔ½«tputÏÂÁîÓëechoÏÂÁîÍŽáʹÓã¬ÔÚËùÓÐÀàUnixºÍ»ùÓÚUnixµÄ²Ù×÷ϵͳÉÏ´òÓ¡ÖÖÖÖÎı¾Ñùʽ¡£
Çë¿´ÒÔÏÂʾÀý£¬´òÓ¡²î±ðµÄÎı¾Ñùʽ£º
#!/bin/bash# LinuxÃÔ www.linuxmi.combold=$(tput bold)underline=$(tput smul)italic=$(tput sitm)info=$(tput setaf 2)error=$(tput setaf 160)warn=$(tput setaf 214)reset=$(tput sgr0)echo "${info}INFO${reset}: This is an ${bold}info${reset} message"echo "${error}ERROR${reset}: This is an ${underline}error${reset} message"echo "${warn}WARN${reset}: This is a ${italic}warning${reset} message"
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÎÒÃÇʹÓÃtputÏÂÁîΪÿÖÖÎı¾Ñùʽ½ç˵ÁËһЩ±äÁ¿¡£×îºó£¬ÎÒÃÇʹÓÃechoÏÂÁîÖ´ÐÐÔÝʱÉúÑĵÄÏÂÁÒÔÏÔʾ²î±ðµÄÎı¾Ñùʽ£¬ÈçÏÂËùʾ¡£
BashÖеÄÎı¾ÑùʽÆÁÄ»½Øͼ
Äú¿ÉÒÔʹÓÃÉÏÊöÒªÁìΪBash¾ç±¾¹¹½¨×Ô¼ºµÄ¸öÐÔ»¯ÑÕÉ«¼Æ»®¡£ÎªÁËÑÚÊÎkillÏÂÁîµÄ¹ýʧÐÂÎÅ£¬ÉÏÊö´úÂëʾÀýʹÓÃÁË¿Õ×°±¸Îļþ£¨/dev/null£©¡£
ÒÔÉϾÍÊÇÎå¸öÏÊΪÈËÖªµÄÏÖ´ú Bash ¾ç±¾±àдÊÖÒÕµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡