爲了讓用戶更加便捷的瞭解到文件的內容,從而對用戶提供幫助,該腳本的目的就是能夠在顯示文件自己內容的基礎上,再多概括總結幾行。 shell
#!/bin/sh # showfile.sh -- 展現一個文件的內容, 也包括有用的額外信息 width=72 for input do lines="$(wc -l < $input | sed 's/ //g')" chars="$(wc -c < $input | sed 's/ //g')" owner="$(ls -ld $input | awk '{print $3}')" echo "----------------------------------------------------------" echo "File $input($lines lines, $chars characters, owned by $owner):" echo "----------------------------------------------------------" while read line do if [ ${#line} -gt "$width" ]; then echo "$line" | fmt.sh | sed -e '1s/^/ /' -e '2,$s/^/+ /' else echo "$line" fi done < $input echo "----------------------------------------------------------" done | more exit 0這個腳本的功能,你們應該看到了,多出了一些形象化的說明,統計了行數和字符數。在顯示每行的內容時,最多顯示72個字符,假若多於72個,那麼就截斷爲多行。這個腳本中最難的地方就在截斷顯示這兒:
echo "$line" | fmt.sh | sed -e '1s/^/ /' -e '2,$s/^/+ /'它調用了第14個腳本fmt.sh,用來格式化輸出用,最後一個管道的做用是:
$ showfile ragged.txt ----------------------------------------------------------------- File ragged.txt (7 lines, 639 characters, owned by taylor): ----------------------------------------------------------------- So she sat on, with closed eyes, and half believed herself in Wonderland, though she knew she had but to open them again, and all would change to dull reality--the grass would be only rustling + in the wind, and the pool rippling to the waving of the reeds--the rattling teacups would change to tinkling sheep-bells, and the Queen's shrill cries to the voice of the shepherd boy--and the sneeze of the baby, the shriek of the Gryphon, and all the other queer + noises, would change (she knew) to the confused clamour of the busy + farm-yard--while the lowing of the cattle in the distance would + take the place of the Mock Turtle's heavy sobs. -----------------------------------------------------------------