Linux readÏÂÁЧÆÊÎöÓëʵÀýÑÝʾ
¡¶Linux readÏÂÁЧÆÊÎöÓëʵÀýÑÝʾ¡·
ÔÚLinuxϵͳÖУ¬readÏÂÁîÊÇÒ»¸öÓÃÓÚ´Ó±ê×¼ÊäÈë»òÎļþÖжÁÈ¡Êý¾ÝµÄÏÂÁî¡£Ëü¿ÉÒÔ×ÊÖúÓû§ÔÚShell¾ç±¾Öн»»¥µØ´ÓÓû§ÊäÈëÖлñÈ¡Êý¾Ý£¬²¢ÇÒ¿ÉÒÔÉúÑĵ½±äÁ¿Öй©ºóÐø´¦ÀíʹÓᣱ¾ÎĽ«¶ÔreadÏÂÁîµÄ¹¦Ð§¾ÙÐÐÆÊÎö£¬²¢ÌṩʵÀýÑÝʾÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£
Ò»¡¢readÏÂÁîµÄ»ù±¾Óï·¨
readÏÂÁîµÄ»ù±¾Óï·¨ÈçÏ£º
read [-options] [variable...]
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬[]ÌåÏÖ¿ÉÑ¡ÏoptionsÊÇreadÏÂÁîµÄÑ¡Ïî²ÎÊý£¬variableÊÇÓÃÓÚ´æ´¢ÊäÈëÊý¾ÝµÄ±äÁ¿Ãû¡£
¶þ¡¢readÏÂÁîµÄ³£ÓÃÑ¡Ïî
-p£ºÖ¸¶¨ÌáÐÑ·ûÐÅÏ¢£¬ÔÚÓû§ÊäÈëÊý¾Ý֮ǰÏÔʾÌáÐÑ·û¡£
-t seconds£ºÉèÖöÁÈ¡Êý¾ÝµÄ³¬Ê±Ê±¼ä£¬µ¥Î»ÎªÃë¡£
-n count£ºÖ¸¶¨¶ÁÈ¡Êý¾ÝµÄ×Ö·ûÊý£¬µ±¶ÁÈ¡µÄ×Ö·ûÊýµÖ´ïcountʱ£¬ÖÕÖ¹¶ÁÈ¡¡£
-s£º¾²Ä¬Ä£Ê½£¬Óû§ÊäÈë²»ÏÔʾÔÚÆÁÄ»ÉÏ¡£
-r£º½«·´Ð±¸Ü×Ö·û¿´³ÉͨË××Ö·û¿´´ý£¬²»×ªÒå¡£
Èý¡¢readÏÂÁîµÄ¹¦Ð§ÆÊÎö
ÎüÊÕÓû§ÊäÈë²¢´æ´¢µ½±äÁ¿ÖУºreadÏÂÁî¿ÉÒÔÎüÊÕÓû§µÄÊäÈ룬²¢½«ÊäÈëµÄÊý¾Ý´æ´¢µ½Ö¸¶¨µÄ±äÁ¿ÖУ¬Àû±ãºóÐø¾ç±¾´¦Àí¡£
ʵÏ־籾½»»¥£ºreadÏÂÁî¿ÉÒÔÍŽáÌáÐÑÐÅÏ¢£¬ÊµÏ־籾ÓëÓû§Ö®¼äµÄ½»»¥£¬»ñÈ¡Óû§ÊäÈëµÄÊý¾Ý¡£
³¬Ê±´¦Àí£ºÍ¨¹ýÉèÖÃ-tÑ¡Ï¿ÉÒÔʵÏÖ³¬Ê±´¦Àí£¬ÈôÊÇÓû§ÔÚָ׼ʱ¼äÄÚûÓÐÊäÈëÊý¾Ý£¬readÏÂÁ³¬Ê±²¢¿¢Ê¡£
×Ö·û¼ÆÊý£ºÍ¨¹ý-nÑ¡Ï¿ÉÒÔ¿ØÖƶÁÈ¡µÄ×Ö·ûÊý£¬µ±µÖ´ïÖ¸¶¨×Ö·ûÊýʱ£¬readÏÂÁÖÕÖ¹¶ÁÈ¡¡£
ËÄ¡¢readÏÂÁîµÄʵÀýÑÝʾ
ÏÂÃæͨ¹ý¼¸¸öʵÀýÑÝʾreadÏÂÁîµÄÏêϸÓ÷¨£º
ʾÀý1£º»ù±¾µÄÓû§ÊäÈë
#!/bin/bash echo "ÇëÊäÈëÄúµÄÐÕÃû£º" read name echo "ÄúÊäÈëµÄÐÕÃûÊÇ£º$name"
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬Óû§Í¨¹ýreadÏÂÁîÊäÈëÐÕÃû£¬²¢½«ÊäÈëµÄÊý¾Ý´æ´¢µ½name±äÁ¿ÖУ¬È»ºóÊä³öµ½ÆÁÄ»ÉÏ¡£
ʾÀý2£ºÉèÖó¬Ê±Ê±¼ä
#!/bin/bash read -t 5 -p "ÇëÊäÈëÄúµÄÄêË꣨5ÃëÄÚÊäÈ룩£º" age if [ -z $age ]; then echo "ÄúδÔÚ5ÃëÄÚÊäÈëÄêË꣬³¬Ê±¡£" else echo "ÄúÊäÈëµÄÄêËêÊÇ£º$age" fi
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÉèÖÃÁË5ÃëµÄ³¬Ê±Ê±¼ä£¬ÈôÊÇÓû§ÔÚ5ÃëÄÚûÓÐÊäÈëÄêË꣬Ôò»áÌáÐѳ¬Ê±ÐÅÏ¢¡£
ʾÀý3£º²»ÏÔʾÓû§ÊäÈë
#!/bin/bash read -s -p "ÇëÊäÈëÄúµÄÃÜÂ룺" password echo -e " ÄúÊäÈëµÄÃÜÂëÊÇ£º$password"
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÉèÖÃÁË-sÑ¡ÏÓû§ÊäÈëÃÜÂëʱ²»ÔÚÆÁÄ»ÉÏÏÔʾ£¬ÒÔ±£»¤ÃÜÂëÇå¾²¡£
ͨ¹ýÒÔÉÏʵÀýÑÝʾ£¬¶ÁÕß¿ÉÒÔ¸üÉîÈëµØÏàʶreadÏÂÁîµÄÓ÷¨¼°¹¦Ð§¡£ÔÚ±àдShell¾ç±¾Ê±£¬readÏÂÁîÊÇÒ»¸öºÜÊÇÊÊÓõŤ¾ß£¬¿ÉÒÔ×ÊÖúʵÏÖÓû§ÊäÈëÊý¾Ý½»»¥£¬ÌáÉý¾ç±¾µÄ½»»¥ÐÔºÍÎÞаÐÔ¡£
×ÜÖ®£¬ÕÆÎÕreadÏÂÁîµÄʹÓÃÒªÁ죬¹ØÓÚLinuxϵͳÓû§À´ËµÊÇÊ®·ÖÖ÷ÒªµÄ¡£Ï£Íû±¾ÎĶԶÁÕßÓÐËù×ÊÖú£¬Ð»Ð»ÔĶÁ£¡
ÒÔÉϾÍÊÇLinux readÏÂÁЧÆÊÎöÓëʵÀýÑÝʾµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡