ÔõÑù¾ÙÐÐShell¾ç±¾²Ù×÷£¿
Ëæ×ÅÅÌËã»úÊÖÒÕµÄÒ»Ö±Éú³¤£¬shell¾ç±¾ÓïÑÔÒ²Ô½À´Ô½Êܵ½¿í´ó³ÌÐòÔ±µÄ½Ó´ý¡£shell¾ç±¾ÓïÑÔ¿ÉÒÔ×ÊÖú³ÌÐòԱʵÏÖ×Ô¶¯»¯²Ù×÷¡¢Åú´¦ÀíµÈ¹¦Ð§£¬´ó´óÌá¸ßÁ˳ÌÐòÔ±µÄÊÂÇéЧÂʺÍÊÂÇéÖÊÁ¿¡£¿ÉÊÇ£¬¹ØÓÚһЩÐÂÊÖÀ´Ëµ£¬shell¾ç±¾ÓïÑԵIJÙ×÷¿ÉÄܽÏÁ¿ÄÑÌ⣬ÏÂÃæ¾ÍÀ´ÏÈÈÝÒ»ÏÂÔõÑù¾ÙÐÐshell¾ç±¾²Ù×÷¡£
Ê×ÏÈ£¬Ê²Ã´ÊÇShell¾ç±¾£¿
Shell¾ç±¾ÊÇÒ»ÖÖ¿ÉÒÔÔÚShell£¨²Ù×÷ϵͳÏÂÁîÚ¹ÊÍÆ÷£©ÖÐÖ´ÐеÄÎı¾Îļþ¡£Ëüͨ³£ÓÃÓÚÔÚLinux¡¢UnixµÈ²Ù×÷ϵͳÖоÙÐо籾±à³Ì£¬¿ÉÒÔʵÏÖÑ»·¡¢Ìõ¼þ·ÖÖ§¡¢º¯ÊýµÈ¶àÖÖ¹¦Ð§¡£
È»ºó£¬Ñ§Ï°Shell¾ç±¾µÄÌõ¼þÊÇÐèÒªÊìϤLinux¡¢UnixµÈ²Ù×÷ϵͳµÄ»ù±¾ÏÂÁî¡£
½ÓÏÂÀ´£¬ÎÒÃǽ«ÏÈÈÝShell¾ç±¾µÄ²Ù×÷£º
½¨ÉèShell¾ç±¾
ÔÚLinux¡¢UnixÖУ¬ÎÒÃÇͨ³£Ê¹ÓÃÎı¾±à¼Æ÷Èçvim¡¢geditµÈÀ´½¨ÉèShell¾ç±¾¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÖ¸Áî¼´¿É½¨ÉèÒ»¸öÃûΪtest.shµÄ¾ç±¾£º
$ vim test.sh
µÇ¼ºó¸´ÖÆ
Ìí¼ÓÏÂÁî
ÔÚ½¨ÉèºÃShell¾ç±¾Ö®ºó£¬ÎÒÃÇÐèÒªÌí¼ÓһЩÏÂÁîÀ´ÊµÏÖijЩÌض¨µÄ¹¦Ð§¡£ÔÚShell¾ç±¾ÖУ¬Ã¿¸öÏÂÁÐèÒª×ÔÁ¦Ò»ÐУ¬ÏÂÁîÐÐÖеĵÚÒ»¸ö×Ö·û±ØÐèÊÇ¡°#¡±»ò¿Õ¸ñ£¬²»È»¸ÃÐÐËùдÄÚÈݻᱻµ±×öÏÂÁî´¦Àí¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£º
#!/bin/bash echo "Hello World"
µÇ¼ºó¸´ÖÆ
ÕâÊÇÒ»¸ö¼òÆÓµÄShell¾ç±¾£¬ÓÉÁ½ÐÐ×é³É¡£µÚÒ»ÐÐÖ¸¶¨ÁËʹÓÃbash×÷ΪShellÚ¹ÊÍÆ÷£»µÚ¶þÐÐʹÓÃechoÏÂÁîÊä³öÁËHello WorldµÄ×Ö·û´®¡£
¸¶Óë¾ç±¾Ö´ÐÐȨÏÞ
ÔÚ½¨ÉèºÃShell¾ç±¾Ö®ºó£¬ÎÒÃÇÐèÒª¸¶ÓëËüÖ´ÐÐȨÏÞ¡£ÔÚLinux¡¢UnixÖУ¬Ê¹ÓÃchmodÏÂÁÓë¾ç±¾Ö´ÐÐȨÏÞ£º
$ chmod +x test.sh
µÇ¼ºó¸´ÖÆ
ÔËÐÐShell¾ç±¾
ÔÚ¸¶Óë¾ç±¾Ö´ÐÐȨÏÞÖ®ºó£¬ÎÒÃǾͿÉÒÔÔËÐÐShell¾ç±¾ÁË¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÖ¸Áî¼´¿ÉÔËÐÐtest.sh¾ç±¾£º
$ ./test.sh
µÇ¼ºó¸´ÖÆ
Õï¶ÏShell¾ç±¾
ÔÚÔËÐÐShell¾ç±¾µÄÀú³ÌÖУ¬ÎÒÃÇ¿ÉÄÜ»áÓöµ½Ò»Ð©¹ýʧºÍÎÊÌ⡣ΪÁ˸üºÃµØÕï¶ÏShell¾ç±¾£¬¿ÉÒÔʹÓÃsetÏÂÁî·¿ªµ÷ÊÔģʽ£¬ÔÚµ÷ÊÔģʽÏ¿ÉÒÔÊä³ö¸üÏêϸµÄÐÅÏ¢£º
#!/bin/bash set -x echo "Hello World" set +x
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇͨ¹ýset -xÏÂÁî·¿ªµ÷ÊÔģʽ£¬ÔÚÊä³öHello World֮ǰÄܹ»¿´µ½¸üÏêϸµÄÐÅÏ¢£¬Àû±ãÅÅÅÌÎÊÌâ¡£setÏÂÁ¿ÉÒÔ¿ªÆôÆäËüµ÷ÊÔģʽ£¬Èçset -e¿ÉÒÔÔھ籾ִÐÐʧ°Üʱ×Ô¶¯Í˳ö¡£
Ñ»·ºÍÌõ¼þ·ÖÖ§
³ýÁ˼òÆÓµÄÏÂÁî²Ù×÷£¬Shell¾ç±¾»¹Ö§³ÖÑ»·ºÍÌõ¼þ·ÖÖ§µÈ¸ß¼¶²Ù×÷¡£ÀýÈ磬¿ÉÒÔʹÓÃforÑ»·À´±éÀúÒ»¸öÊý×飺
#!/bin/bash fruits=("apple" "banana" "orange") for fruit in "${fruits[@]}" do echo "I like $fruit" done
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÊý×éfruits£¬Ê¹ÓÃforÑ»·Êä³öÁËÊý×éÖÐÿ¸öÔªËصÄÄÚÈÝ¡£
Shell¾ç±¾»¹Ö§³Öif¡¢elseµÈÌõ¼þ·ÖÖ§Óï¾ä£¬ÀýÈ磺
#!/bin/bash password="123456" if [ "$password" == "admin" ] then echo "Welcome" else echo "Access Denied" fi
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÈôÊÇ$password¼´ÊÇadmin£¬ÔòÊä³öWelcome£¬²»È»Êä³öAccess Denied¡£
×ܽá
Shell¾ç±¾ÊÇÒ»ÃźÜÊÇÊÊÓõıà³ÌÓïÑÔ£¬Ëü¿ÉÒÔ×ÊÖú³ÌÐòԱʵÏÖ×Ô¶¯»¯²Ù×÷¡¢Åú´¦ÀíµÈ¶àÖÖ¹¦Ð§¡£ËäÈ»£¬ÒªÑ§Ï°ºÃShell¾ç±¾ÓïÑÔ£¬ÐèÒª¹ØÓÚLinux¡¢UnixµÈ²Ù×÷ϵͳµÄ»ù±¾ÏÂÁîÓÐÒ»¶¨µÄÏàʶ¡£Í¨¹ýѧϰÉÏÊöShell¾ç±¾²Ù×÷µÄÒªÁ죬ÐÅÍжÁÕßÒѾÓÐÁËÒ»¶¨µÄÏàʶºÍÕÆÎÕ¡£Ï£Íû¸÷ÈËÄܹ»Ñ§ºÃShell¾ç±¾ÓïÑÔ£¬Ìá¸ß×Ô¼ºµÄÊÂÇéЧÂʺÍÊÂÇéÖÊÁ¿¡£
ÒÔÉϾÍÊÇÔõÑù¾ÙÐÐShell¾ç±¾²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡