Bash字符串操作.docVIP

  • 6
  • 0
  • 约7.99千字
  • 约 7页
  • 2016-12-29 发布于重庆
  • 举报
Bash中的Arithmetic Expansion(算术扩展) Arithmetic Expansion(算术扩展)为在脚本中进行算术运算(整型)提供了强有力的工具,其使用 backticks[反引号], double parentheses[(())], 或 let命令将字符串直接转换为相关的算术表达式。 基于backticks[反引号](经常与expr命令一起使用)的算术计算方法 1?z=`expr $z + 3` # The expr command performs the expansion. 基于双括号(())或let命令的算术计算方法 基于backticks[反引号]的算术计算方法已经逐渐被双括号(()) $(()) 和 使用更方便的let 命令替代。 1?z=$(($z+3)) 2?z=$((z+3)) # Also correct. 3? # Within double parentheses, 4? #+ parameter dereferencing 5? #+ is optional. 6? 7?# $((EXPRESSION)) is arithmetic expansion. # Not to be confused with 8? #+ command substitution. 9? 10? 11? 12?# You may also use operations within double parentheses without assignment. 13? 14? n=0 15? echo n = $n # n = 0 16? 17? (( n += 1 )) # Increment. 18?# (( $n += 1 )) is incorrect! 19? echo n = $n # n = 1 20? 21? 22?let z=z+3 23?let z += 3 # Quotes permit the use of spaces in variable assignment. 24? # The let operator actually performs arithmetic evaluation, 25? #+ rather than expansion. 1?#!/bin/bash 2? 3?var0=0 4?LIMIT=10 5? 6?while [ $var0 -lt $LIMIT ] 7?do 8? echo -n $var0 # -n suppresses newline. 9? # ^ Space, to separate printed out numbers. 10? 11? var0=`expr $var0 + 1` # var0=$(($var0+1)) also works. 12? # var0=$((var0 + 1)) also works. 13? # let var0 += 1 also works. 14?done # Various other methods also work. 15? 16?echo 17? 18?exit 0 Bash中的字符串处理 Bash支持一系列令人吃惊的数字和字符串处理。不幸的是这些工具缺乏统一的焦点/目的, 它们有些是参数替换(parameter substitution)的子集,其他的属于UNIX命令

文档评论(0)

1亿VIP精品文档

相关文档