网上找工作可靠吗有骗子吗?

扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
下载作业帮安装包
扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
shell 脚本里面从一个文本里面读出一个数字,如何转换成整数?我需要用这个数字进行加减乘除
扫二维码下载作业帮
拍照搜题,秒出答案,一键查看所有搜题记录
方法有很多:var=4.57法一:echo ${var%.*} #这个是直接去除小数点及后面所有内容,只用于bash法二:echo $var | awk -F.'{print $1}' #以小数点为分隔符取第一个字段法三:echo $var | awk '{print int($0)}' #awk中可直接使用C函数取整法四:echo "scale=0;$var/1"|bc -l #用bc计算法五:printf "%.0f\n" $var #这个与上面不同,是四舍五入的
为您推荐:
其他类似问题
扫描下载二维码Shell脚本编程中常用的数学运算实例
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Shell脚本编程中常用的数学运算实例,包含最基本的加减乘除,还有质数、偶数的判断等,需要的朋友可以参考下
这部分主要讨论数学相关的shell脚本编程。
新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
代码如下:#!/bin/bash
echo “Enter the First Number: ”
echo “Enter the Second Number: ”
x=$(expr "$a" + "$b")
echo $a + $b = $x
输出结果:
[root@tecmint ~]# vi Additions.sh
[root@tecmint ~]# chmod 755 Additions.sh
[root@tecmint ~]# ./Additions.sh
“Enter the First Number: ”
“Enter the Second Number: ”
12 + 13 = 25
#!/bin/bash
echo “Enter the First Number: ”
echo “Enter the Second Number: ”
x=$(($a - $b))
echo $a - $b = $x
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。
输出结果:
[root@tecmint ~]# vi Substraction.sh
[root@tecmint ~]# chmod 755 Substraction.sh
[root@tecmint ~]# ./Substraction.sh
“Enter the First Number: ”
“Enter the Second Number: ”
13 - 20 = -7
#!/bin/bash
echo “Enter the First Number: ”
echo “Enter the Second Number: ”
echo "$a * $b = $(expr $a \* $b)"
输出结果:
[root@tecmint ~]# vi Multiplication.sh
[root@tecmint ~]# chmod 755 Multiplication.sh
[root@tecmint ~]# ./Multiplication.sh
“Enter the First Number: ”
“Enter the Second Number: ”
11 * 11 = 12
#!/bin/bash
echo “Enter the First Number: ”
echo “Enter the Second Number: ”
echo "$a / $b = $(expr $a / $b)"
输出结果:
[root@tecmint ~]# vi Division.sh
[root@tecmint ~]# chmod 755 Division.sh
[root@tecmint ~]# ./Division.sh
“Enter the First Number: ”
“Enter the Second Number: ”
12 / 3 = 4
下面的这个脚本可以打印一组数字。
#!/bin/bash
echo “Enter The Number upto which you want to Print Table: ”
while [ $i -ne 10 ]
i=$(expr $i + 1)
table=$(expr $i \* $n)
echo $table
输出结果:
[root@tecmint ~]# vi Table.sh
[root@tecmint ~]# chmod 755 Table.sh
[root@tecmint ~]# ./Table.sh
“Enter The Number upto which you want to Print Table: ”
你可以从这里下载这个例子的代码
判断奇偶数
#!/bin/bash
echo "Enter The Number"
num=$(expr $n % 2)
if [ $num -eq 0 ]
echo "is a Even Number"
echo "is a Odd Number"
输出结果:
[root@tecmint ~]# vi EvenOdd.sh
[root@tecmint ~]# chmod 755 EvenOdd.sh
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
is a Even Number
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
is a Odd Number
Factorial数
#!/bin/bash
echo "Enter The Number"
while [ $a -ne 0 ]
fact=$(expr $fact \* $a)
a=$(expr $a - 1)
echo $fact
输出结果:
代码如下:[root@tecmint ~]# vi Factorial.sh
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
Enter The Number
你可以从这里下载这个例子的代码
判断Armstrong数
Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
#!/bin/bash
echo "Enter A Number"
while [ $n -ne 0 ]
r=$(expr $n % 10)
arm=$(expr $arm + $r \* $r \* $r)
n=$(expr $n / 10)
if [ $arm -eq $temp ]
echo "Armstrong"
echo "Not Armstrong"
输出结果:
[root@tecmint ~]# vi Armstrong.sh
[root@tecmint ~]# chmod 755 Armstrong.sh
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
Not Armstrong
#!/bin/bash
echo “Enter Any Number”
while [ $i -le $n ]
i=$(expr $i + 1)
r=$(expr $n % $i)
if [ $r -eq 0 ]
c=$(expr $c + 1)
if [ $c -eq 2 ]
echo “Prime”
echo “Not Prime”
输出结果:
[root@tecmint ~]# vi Prime.sh
[root@tecmint ~]# chmod 755 Prime.sh
[root@tecmint ~]# ./Prime.sh
“Enter Any Number”
“Not Prime”
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具bai 的BLOG
用户名:bai
文章数:19
访问量:1313
注册日期:
阅读量:5863
阅读量:12276
阅读量:403532
阅读量:1092717
51CTO推荐博文
#!/bin/bash
if&[&$#&-eq&0&];&then
&echo&"输入值不能为空"&&&&&&&&&&//提示不能输入空值&&注:$#代表环境变量个数
num=$*&&&&&&&&&&&&&&&&&&&&&&&//定义一个全局变量,方便后边的调用
while&[&$#&-gt&0&]
&&sum=$[&$sum+$1&]
echo&&"数字&$num&相加的和是&$sum"
while&[&$#&-gt&0&]
&cha=$[&$cha-$1&]
echo&&"数字&$num&相减的差是&$cha"
}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
while&[&$#&-gt&0&]
&cheng=$[&$cheng*$1&]
echo&&"数字&$num&相乘的是&$cheng"
}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
while&[&$#&-gt&0&]
&chu=$[&$chu/$1&]
echo&&"数字&$num&相除的商是&$chu"
read&-p&"请输入&+&|&-&|&*&|&/&:"&type
case&"$type"&in
"+")&sum&$*&;;
"-")&cha&$*&;;
"*")cheng&$*&;;
"/")&chu&$*&;;
*)&echo&"&请输入{&+|-|*|/}"
了这篇文章
类别:┆阅读(0)┆评论(0)shell的数值计算,小数计算
时间: 21:21:57
&&&& 阅读:124
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&shell脚本中,可以进行数值计算,
如加减乘除,通过expr、let、(())等完成,文章介绍:http://blog.csdn.net/longshenlmj/article/details/
但是上面的运算只支持整数。
小数计算怎么办?
shell中小数计算
通过bc,awk和nawk工具等完成
使用bc的代码如:
click=`awk -F"\t" ‘$1==2 {print $2}‘ ctr_result.txt`
show=`awk -F"\t" ‘$1==1 {print $2}‘ ctr_result.txt`
ctr=`echo "scale=4;$click/$show"| bc`
其中,echo "scale=4;$click/$show"| bc完成小数计算,
scale参数指定小数点后的保留位数,这里保留4位。
ctr_result.txt内容为
通过awk获取两个参数
当然,也可以直接用awk完成小数计算:
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:&&&&&&&&&
&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!

我要回帖

更多关于 网上怎样找工作 的文章

 

随机推荐