Linux readÏÂÁîµÄÆÕ±éÓÃ;£ºÌ½Ë÷¶àÖÖÓ¦Óó¡¾°
LinuxϵͳÊÇÒ»ÖÖ¿ªÔ´µÄ²Ù×÷ϵͳ£¬¾ßÓÐÇ¿Ê¢µÄ¹¦Ð§ºÍÎÞаÐÔ£¬¶øreadÏÂÁîÊÇÆäÖÐÒ»¸öºÜÊÇÊÊÓõÄÏÂÁîÖ®Ò»¡£±¾ÎĽ«Ì½Ë÷Linux readÏÂÁîµÄ¶àÖÖÓ¦Óó¡¾°£¬²¢ÌṩÏêϸµÄ´úÂëʾÀýÀ´×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÔËÓÃÕâ¸öÏÂÁî¡£
1. ¶ÁÈ¡Óû§ÊäÈë
readÏÂÁî×î³£¼ûµÄÓ÷¨ÊÇÓÃÀ´¶ÁÈ¡Óû§µÄÊäÈë¡£Óû§¿ÉÒÔͨ¹ý¼üÅÌÊäÈëÄÚÈÝ£¬readÏÂÁî¿ÉÒÔ½«ÕâЩÊäÈëÉúÑĵ½Ò»¸ö±äÁ¿ÖУ¬È»ºóÔھ籾ÖоÙÐд¦Àí¡£
#!/bin/bash echo "ÇëÊäÈëÄúµÄÐÕÃû£º" read name echo "ÄúºÃ£¬$name£¡"
µÇ¼ºó¸´ÖÆ
ÔËÐÐÉÏÊö¾ç±¾ºó£¬»áÌáÐÑÓû§ÊäÈëÐÕÃû£¬Óû§ÊäÈëºó¾ç±¾»áÊä³ö¡°ÄúºÃ£¬ÐÕÃû£¡¡±µÄÐÂÎÅ¡£
2. ¶ÁÈ¡ÏÂÁîÊä³ö
readÏÂÁ¿ÉÒԺ͹ܵÀÍŽáʹÓ㬶ÁÈ¡ÁíÒ»¸öÏÂÁîµÄÊä³ö£¬²¢ÉúÑĵ½±äÁ¿ÖС£
#!/bin/bash files=$(ls) echo "Ä¿½ñĿ¼ÏµÄÎļþÓУº" echo "$files"
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾ÖÐʹÓÃlsÏÂÁîÁгöÄ¿½ñĿ¼ÏµÄÎļþÁÐ±í£¬È»ºó½«ÕâЩÎļþÉúÑĵ½±äÁ¿filesÖУ¬×îºóÊä³öÎļþÁÐ±í¡£
3. ¶ÁÈ¡ÎļþÄÚÈÝ
readÏÂÁ¿ÉÒÔÓÃÀ´ÖðÐжÁÈ¡ÎļþµÄÄÚÈÝ£¬²¢¾ÙÐд¦Àí¡£
#!/bin/bash filename="example.txt" while IFS= read -r line do echo "ÄÚÈÝ£º $line" done < "$filename"
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾»áÖðÐжÁÈ¡example.txtÎļþÖеÄÄÚÈÝ£¬²¢Êä³öÿÐеÄÄÚÈÝ¡£
4. ¶ÁÈ¡¶à¸öÊäÈë
readÏÂÁ¿ÉÒÔÒ»´ÎÐÔ¶ÁÈ¡¶à¸öÊäÈ룬²¢ÉúÑĵ½¶à¸ö±äÁ¿ÖС£
#!/bin/bash echo "ÇëÊäÈëÐÕÃûºÍÄêË꣺" read name age echo "$name µÄÄêËêÊÇ $age Ëê"
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾»áÌáÐÑÓû§ÊäÈëÐÕÃûºÍÄêË꣬Ȼºó½«ÕâÁ½¸öÊäÈëÉúÑĵ½nameºÍageÁ½¸ö±äÁ¿ÖУ¬×îºóÊä³öÕâÁ½¸ö±äÁ¿µÄÖµ¡£
5. ´øÓг¬Ê±¹¦Ð§µÄread
readÏÂÁ¿ÉÒÔÉèÖó¬Ê±Ê±¼ä£¬ÈôÊÇÁè¼Ýָ׼ʱ¼äÓû§Ã»ÓÐÊäÈ룬Ôò³¬Ê±Í˳ö¡£
#!/bin/bash read -t 5 -p "ÇëÊäÈëÄúµÄÑ¡Ôñ£¨5ÃëÄÚ£©£º" if [ -z "$REPLY" ]; then echo "³¬Ê±" else echo "ÄúÑ¡ÔñÁË£º$REPLY" fi
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾ÖÐÉèÖÃÁË5ÃëµÄ³¬Ê±Ê±¼ä£¬ÈôÊÇÓû§ÔÚ5ÃëÄÚûÓÐÊäÈ룬Ôò»áÌáÐÑ¡°³¬Ê±¡±£¬²»È»»áÊä³öÓû§µÄÑ¡Ôñ¡£
×ܵÄÀ´Ëµ£¬LinuxϵͳÖеÄreadÏÂÁî¾ßÓÐÆÕ±éµÄÓ¦Óó¡¾°£¬¿ÉÒÔÓÃÓÚ¶ÁÈ¡Óû§ÊäÈë¡¢¶ÁÈ¡ÏÂÁîÊä³ö¡¢¶ÁÈ¡ÎļþÄÚÈݵȶàÖÖÇéÐΡ£Í¨¹ý±¾ÎÄÌṩµÄÏêϸ´úÂëʾÀý£¬Ï£Íû¶ÁÕßÄܸüºÃµØÕÆÎÕºÍÔËÓÃÕâ¸öÊÊÓõÄÏÂÁî¡£
ÒÔÉϾÍÊÇLinux readÏÂÁîµÄÆÕ±éÓÃ;£ºÌ½Ë÷¶àÖÖÓ¦Óó¡¾°µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡