Shell 脚本攻略: shell数学运算 和 文件处理 以及 数组和关联数组

本文介绍BashShell中进行数学运算的方法,包括let、expr、[]、()和bc的使用,并讲解文件描述符及重定向的用法。同时,探讨了常规数组与关联数组的创建和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.Doing math calculations with the shell

(1)let :

(2)[] && (( )):

(3)expr : 

2.Playing with file descriptors and redirection

(1)用法:

(2)标准错误输出的例子:

3.数组和关联数组


1.Doing math calculations with the shell

Bash shell 使用 let,(( )) 、 [] 、expr、进行基础的数学运算。 bc 执行浮点数计算。

(1)let :

let :  用于执行基础操作,当使用let时,可以不使用 "$"

let result=no1+no2

echo $result

#加法操作
let no1++
#减法操作
let no1--
#shorthands
let no+=6  : let no=no+6
let no-=6  : let no=no-6

(2)[] && (( )):

result=$[ no1 + no2 ]

result=$[ $no1 + 5 ]

result=$(( no1 + 50 ))

(3)expr : 

result=`expr 3 + 4`
result=$(expr $no1 + 5)

注意: 上面四种操作只支持整形变量,不支持浮点数。 如果想要计算浮点数,需要使用精度计算器 bc

echo "4 * 0.56" | bc

2.24

BC 的几种附加操作:

  • 十进制精度: 例如 scale=2 时,
echo "scale=2;3/8" | bc
0.37
  • Base conversion with bc: 进制转换
#!bin/bash
Description: Number conversion

no=100
echo "obase=2;$no" | bc             #默认输入是十进制,所以这里不用强调
1100100
no=1100100
echo “obase=10;ibase=2;$no” | bc    #输入二进制,输出十进制
100
  • 平方和平方根的计算
echo "sqrt(100)" | bc #Square root
echo "10^10" |bc #Square

2.Playing with file descriptors and redirection

stdin 、stdout 、stderr

在编写脚本时,经常使用 stdin,stdout,stderr。 通过过滤内容将输出重定向到文件是我们需要执行的基本操作之一。

  • 0 stdin
  • 1 stdout
  • 2 stderr

(1)用法:

重定向 和 保存输出文本:

$ echo "This is a sample text l" > temp.txt

使用这个命令,文件之前的内容会被清除

$echo "This is sample text 2" >> temp.txt

这将把输出追加到文件中。

 

cat  观察文本内容 

$ cat temp.txt

This is sample text 1

This is sample text 2

(2)标准错误输出的例子:

$ ls +
ls: cannot access +: No such file or directory

Here + is an invalid argument and hence an error is returned

脚本通过在程序结束后立即执行 $?判断程序是否执行成功,成功返回0,不成功返回1.

#存在test文件夹的情况,输出被重定向到out.txt文件中。
ls test/ >out.txt

ls + 2> err.txt #书上说可以将stderr重定向到err.txt文件,但是没有成功。

3.数组和关联数组

Bash 支持常规数组关联数组。

常规数组: 只使用整形(int)作参数。

关联数组: 可以使用字符串(string)作为参数。(Bash 4.0及以上支持)

array_var=(1 2 3 4 5 6)
#Values will be stored in consecutive locations starting from index 0.
array_var[0]="test1"
array_var[1]="test2"

#Print:
echo ${array_var[0]}
test1

index=1
echo ${array_var[$index]}
test2

#Print all the values
echo ${array_var[*]}

#Print the length of an array
echo ${#array_var[*]}
6

打印字符串长度时也使用了#

关联数组:

#!/bin/sh
#this program is used to capture info from test log
#################################
log_name=$1
loop_num=$2
output_file="out_put_log.txt"

loop_indx=0

declare -a t1
declare -a t2
declare -a t3
declare -a t4


t1[$loop_indx]=`grep "t1_vec" $log_name | awk '{printf("%g", $8)}'`

数组声明:
declare -A ass_array

添加元素:
    ass_array=([index1]=vall [index2]=val2)
    ass_array[index1]=val1
    ass_array[index2]=val2

For example:

$ declare -A fruits_value

$ fruits_value=([apple]='100dollars' [orange]='150 dollars')

$ echo "Apple costs ${fruits_value[apple]}"

Apple costs 100 dollars

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值