Ubuntu linux命令练习1 shell seq rand 格式化

linux命令练习1

一些基础命令,以下为方便演示均用shell脚本演示

  1. od
#!/bin/bash -v

echo -n "$IFS" | od -c

echo -n "$IFS" | od -b


#左侧输出为变量地址(八进制)
# -c 选择可打印字符或反斜杠转义
# -b 选择八进制字节

在这里插入图片描述

  1. type
#!/bin/bash -v

type type

type let 

type expr 

type awk 

#let 是 Shell 内置命令,其他几个是外部命令,都在 /usr/bin 目录 下

#而 expr 和 bc 因为刚用过,已经加载在内存的 hash 表中

#说明:如果要查看不同命令的帮助,对于 let 和 type 等 Shell 内置命令,可以通过 Shell 的一个内置命令 help 来查看相关帮助,而一些外部命令可以通过 Shell 的一个外部命令 man 来查看帮助,用法诸如 help let , man expr 等。

  1. while
#!/bin/bash 
# calc.sh 
i=0; 
while [ $i -lt 10000 ] 
do 
	((i++)) 
done 
echo $i

#$ time calc.sh
#10000 
#real 0m1.319s user
#0m1.056s
#sys 0m0.036s

#time 命令可以用来统计命令执行时间,这部分时间包括总的运行时间,用户空间执 行时间,内核空间执行时间,它通过 ptrace 系统调用实现。

  1. ++
#!/bin/bash
((i++)) #最快
let i++; #内置命令,效率也很高
i=$(expr $i + 1)
i=$(echo $i+1|bc) 
i=$(echo "$i 1" | awk '{printf $1+$2;}') # expr , bc , awk 的计算效率就比较低

  1. 文件读取
#!/bin/bash
#sumA_B.sh

#[ $# -lt 1 ] && echo "please input the income file" && exit -1
#[ ! -f $1 ] && echo "$1 is not a file" && exit -1

income=1

awk '{
	printf("%d\n",$2+$1);
}'

#-lt(less than)  : 测试一个数是否小于另一个数;小于,为真;否则,为假;

#-f : 判断参数是否为文件

#-gt(greter than) : 测试一个数是否大于另一个数;大于,为真;否则,为假;

#-lt(less than)  : 测试一个数是否小于另一个数;小于,为真;否则,为假;

#-ge(greter equal): 大于或等于
#-le(less equal) :小于或等于

  1. 获取一系列数
#!/bin/bash -v

for i in {1..5};
do echo -n "$i ";
done

  1. seq
#!/bin/bash -v
#获取一系列数.sh

#其实通过一个循环就可以产生一系列数,但是有相关工具为什么不用呢! seq 就是这么一个 小工具,它可以产生一系列数,你可以指定数的递增间隔,也可以指定相邻两个数之间的分 割符

seq 5

seq 1 5

seq 1 2 5

seq -s: 1 2 5

seq 1 2 15

seq -w 1 2 14

seq -s: -w 1 2 14

seq -f "0x%g" 1 5


#!/bin/bash -v

for i in `seq -f"http://thns.tsinghua.edu.cn/thnsebooks/ebook73/%02g.pdf" 1 21`;
do wget -c $i;
done

for i in `seq -w 1 21`;do wget -c "http://thns.tsinghua.edu.cn/thnsebooks/ebook73/$i ";
done



  1. 随机rand
#!/bin/bash -v
#获取一个随机数.sh

echo $RANDOM

echo "" | awk '{srand();printf("%f",rand());}'

#说明: srand() 在无参数时,采用当前时间作为 rand() 随机数产生器的一个 seed

#!/bin/bash -v
#随机产生一个从 0 到 255 之间的数字.sh

expr $RANDOM / 128

echo "" | awk '{srand();printf("%d\n",rand()*255);}'

#可以通过 RANDOM 变量的缩放和 awk 中 rand() 的放大来实现

#!/bin/bash -v
# getip.sh -- get an usable ipaddress automatically

# set your own network, default gateway, and the time out of "ping" command
net="192.168.1"
default_gateway="192.168.50.2"
over_time=2

# check the current ipaddress
ping -c 1 $default_gateway -W $over_time
[ $? -eq 0 ] && echo "the current ipaddress is okey!" && exit -1;

while :; do
	# clear the current configuration
	ifconfig eth0 down
	# configure the ip address of the eth0
	ifconfig eth0 \
		$net.$(($RANDOM /130 +2)) \
		up
	# configure the default gateway
	route add default gw $default_gateway
	# check the new configuration
	ping -c 1 $default_gateway -W $over_time
	# if work, finish
	[ $? -eq 0 ] && break
done

# 	$?:表示上一个命令执行的退出状态

  1. 格式化
#!/bin/bash -v

echo "scale=5; 1/6" | bc

echo "1 6" | awk '{printf("%0.5f\n",$1/$2)}'

  1. 余弦值转角度
#!/bin/bash -v

export cos=0.800123;

echo "scale=100; a(sqrt(1-$cos^2)/$cos)*180/(a(1)*4)" | bc -l

#在用 bc 进行运算时,如果不用 scale 指定精度,而在 bc 后加上 -l 选项,也可 以进行浮点运算,只不过这时的默认精度是 20 位

echo 0.800123 | awk '{ printf("%s\n", atan2(sqrt(1-$1^2),$1)*180/3.1415926535);}'



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值