演示了如何在Linux的Shell中使用数组.

  1. #!/bin/bash
  2. # Bash shell script to reverse text file contain i.e. concatenate files and
  3. # print on the standard output in reverse. This script also demonstrate how
  4. # to use arrays under bash shell script.
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # -------------------------------------------------------------------------
  9. # This script is part of nixCraft shell script collection (NSSC)
  10. # Visit http://bash.cyberciti.biz/ for more information.
  11. # -------------------------------------------------------------------------
  12. FILE="$1"
  13. if [ $# -eq 0 ]; then
  14.   echo "$(basename $0) - file-name"
  15.   exit 1
  16. fi
  17. textArray[0]="" # hold text
  18. c=0 # counter
  19. # read whole file in loop
  20. while read line
  21. do
  22.   textArray[$c]=$line # store line
  23.   c=$(expr $c + 1) # increase counter by 1
  24. done < $FILE
  25. # get length of array
  26. len=$(expr $c - 1 )
  27. # use for loop to reverse the array
  28. for (( i=$len; i>=0; i-- ));
  29. do
  30.   echo ${textArray[$i]}
  31. done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值