題目描述

輸入文字檔案內容,並輸出其前20個字元。



原題網址

輸入格式

輸入一個文字檔案。

輸出格式

將輸出文字檔案的前20個字元。

範例輸入

New York is a state in the Northeastern and Mid-Atlantic regions of the United States. 
New York is the 27th-most extensive, the third-most populous populated of the 50 United States. 
New York is bordered by New Jersey and Pennsylvania to the south.
About one third of all the battles of the Revolutionary War took place in New York.
Henry Hudson's 1609 voyage marked the beginning of European involvement with the area.

範例輸出

New York is a state

解題概念

利用while和「read」指令,來一行一行讀取從標準輸入傳進來的檔案內容。每讀取到一行資料,就儲存到變數C之中,再串接到TEST變數之後,以此來讀取完整的檔案內容。接著將TEST變數作為「head」指令的輸入來輸出結果。「head」指令可以讀取文字資料。並利用「-c」選項指定來輸出資料前幾個字元的內容。

參考答案

#!/bin/bash

read C
TEXT=${C}
while read C; do
    TEXT="${TEXT}\n${C}"
done
printf "${TEXT}" | head -c 20