#!/bin/bash echo -e "what's this\c" printf "\n" echo "#-------------#" printf "%-10s %-8s %-4s\n" name gender weight #-10表示左对齐,10表示如果字符串不够10个字符长度,那么其他的会以空格代替。如果超过10个长度则会全部打印出来 printf "%-10s %-8s %-4.2f\n" Ada female 50.0343 #-4.2f表示浮点数打印出来一共不超过4位,还有2位小数 printf "%-10s %-8s %-4.2f\n" Albert male 51.453 printf "%-10s %-8s %-4.2f\n" Amy female 53.5423 printf "%-10s %-8s %-4.2f\n" David male 52.871 echo "#------------#" printf "%s\n" abcdef #printf比echo的打印功能稍微强大一些,\n是换行 printf "%s\n" abc def #format-string被重用,abc和def都可以被打印出来 printf %s 00ooOO #即使对于format-string不加双引号,也会正常输出 printf "please input the username:" read username #读入从键盘输入的字符串 printf "the username is %s\n" $username a=4 b=8 while [ $a -lt $b ] #while循环的格式,后面可以带中括号条件 do a=`expr $a + 1` echo "\$a=$a" done echo "\$a=$a" i=100 sum=0 while (($i>=0)) #while循环的格式,后面可以带(())的格式 do sum=`expr $sum + $i` i=`expr $i - 1` done echo "the sum is $sum" #while true i=100 sum=0 until (($i<0)) #until循环和while循环是相反的。until是一旦检测到条件不成立,就退出循环 do sum=`expr $sum + $i` i=`expr $i - 1` done echo "the sum is $sum" echo "please input the number 1-4" echo "the number you input:" read num case $num in #case语法规则,case 变量 in ... esac 1) echo "you chose 1";; 2) echo "you chose 2";; 3) echo "you chose 3";; 4) echo "you chose 4";; *) echo "you chose other numbers";; esac while true do echo "please input 1-5:" read num case $num in 1|2|3|4|5) echo "you input $num";; *) echo "you input other numbers" break;; esac done