本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#32 Displaying a File with Additional Information

百度收藏 QQ搜藏

Many of the most common Unix commands have evolved within slow-throughput, expensive output environments and therefore offer minimal output and interactivity. An example is cat: When used to view a short file, it really doesn't have much helpful output. It would be nice to have more information about the file. This script, a more sophisticated variation of Script #31, accomplishes this.

The Code
#!/bin/sh
# showfile - Shows the contents of a file, including additional useful info.

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 | sed -e '1s/^/ /' -e '2,$s/^/+ /'
      else
        echo " $line"
      fi
    done < $input

  echo "-----------------------------------------------------------------"
done | more

exit 0

How It Works
To simultaneously read the input line by line and add head and foot information, this script uses a handy shell trick: Near the end of the script it redirects the input to the while loop with the snippet done < $input. Perhaps the most complex element in this script, however, is the invocation of sed for lines longer than the specified length:

echo "$line" | fmt | sed -e '1s/^/ /' -e '2,$s/^/+ /'

Lines greater than the maximum allowable length are wrapped with fmt (or its shell script replacement, Script #14). To visually denote which lines are wrapped continuations and which are retained intact from the original file, the first line of wrapped output has the usual two-space indent, but subsequent wrapped lines are prefixed with a plus sign and a single space instead. Finally, the more program displays the results.

Running the Script
As with the previous script, you can run showfile simply by specifying one or more filenames when the program is invoked.

The Results
$ 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.
-----------------------------------------------------------------


上一篇:#31 Displaying a File with Line Numbers 下一篇:#33 Wrapping Only Long Lines
power by soyo123 2007-2008