专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

Bash shell脚本:利用shell数组颠倒文本并输出

百度收藏 QQ搜藏

原文:http://bash.cyberciti.biz/file-management/reverse-text-file/

这个脚本同样可以作为在Bash shell脚本如何使用数组的实例

以下是该shell脚本的源代码:

#!/bin/bash
# Bash shell script to reverse text file contain i.e. concatenate files and
# print on the standard output in reverse. This script also demonstrate how
# to use arrays under bash shell script.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
FILE="$1"
if [ $# -eq 0 ]; then
  echo "$(basename $0) - file-name"
  exit 1
fi
 
textArray[0]="" # hold text
c=0 # counter
# read whole file in loop
while read line
do
  textArray[c]=$line # store line
  c=$(expr $c + 1) # increase counter by 1
done < $FILE
# get length of array
len=$(expr $c - 1 )
 
# use for loop to reverse the array
for (( i=$len; i>=0; i-- ));
do
  echo ${textArray[$i]}
done
上一篇:shell脚本 实现计算英文文章里诸如'A/An/The'个数 下一篇:shell脚本 查找文件是否有读/写/执行权限

power by soyo123 2007-2008