1.算数运算命令有哪几种?
(1) "(( ))"用于整数运算的常用运算符,效率很高
[root@shell scripts]# echo $((2+4*5**2/8)) #(( ))2+4×52÷8=14
14
(2) "$[ ] "用于整数运算
[root@shell scripts]# echo $[2+4*5**2/8] #[ ]也可以运算
14
(3) "let"用于整数运算,类似于 (( ))
[root@shell scripts]# let r=2+4*5**2/8 #let也可以作整数运算
[root@shell scripts]# echo $r
14
(4) "expr"可用于整数运算,但还有很多其他的额外功能
[root@shell scripts]# expr 2 + 4 \* 5 / 8 #expr是外部命令运算运算符两边需要空格,且乘法*需要转义,**运算符在expr命令中并不支持。
4
[root@shell scripts]# expr 10 % 3 #取余,两边必须要有空格
1
(5) "bc"Linux下的一个计算器程序(适合整数及小数运算)
[root@shell scripts]# echo 2+3*6 | bc #linux用于数学计算的高级计算器bc
20
(6) "declare"义变量值和属性,-i 参数可以用于定义整形变量,做运算
[root@shell scripts]# declare -i r2=2+3*6 #做运算
[root@shell scripts]# echo $r2
20
(7) "awk" 既可以用于整数运算,也可以用于小数运算
[root@shell scripts]# awk 'BEGIN {print 2+3*6/7}' #可以进行小数预算
4.57143
BEGIN 是一个在 awk 中可用的特殊关键字,用于在处理输入之前执行一些初始操作。BEGIN 块通常用于设置变量、打印标题或执行其他一次性任务
2.定义变量url=https://blog.csdn.net/weixin_45029822/article/details/103568815
1)截取网站访问的协议
[root@shell ~]# url=https://blog.csdn.net/weixin_45029822/article/details/103568815
[root@shell ~]# echo $url
https://blog.csdn.net/weixin_45029822/article/details/103568815[root@shell ~]# echo $url | grep -o "https" #-o只显示匹配到的结果
https[root@shell ~]# echo $url | grep "\<https" -o #正则
https[root@shell ~]# echo "$url" | awk '/https/ {print "https"}'
https[root@shell ~]# echo ${url%:*} #字符串变量切片
https[root@shell ~]# echo $url | cut -d : -f1
https
2)截取网站访问账号信息
[root@shell ~]# echo ${url##*/}
103568815[root@shell ~]# echo $url | cut -d / -f7
103568815[root@shell ~]# echo $url | grep -o "103568815"
103568815
3.写一个脚本,完成以下要求:
给定一个用户:
1、如果其UID为0,就显示此为管理员;
2、否则,就显示其为普通用户;
[root@shell scripts2]# vim user1.sh +
#!/bin/bash
read -p "please input a user:" user
uid=`id $user -u`
if [ $uid -eq 0 ]
then
echo The user with a UID of $uid is an administrator
else
echo This user has a UID of $uid and is a regular user
fi[root@shell scripts2]# chmod +x user1.sh
[root@shell scripts2]# ./user1.sh
please input a user:root
The user with a UID of 0 is an administrator
[root@shell scripts2]# ./user1.sh
please input a user:fox
This user has a UID of 1001 and is a regular user
4.写一个脚本
判断当前系统上是否有用户的默认shell为bash;
如果有,就显示有多少个这类用户;否则,就显示没有这类用户;
[root@shell scripts2]# vim user2.sh +
#!/bin/bash
user=`grep "bash" /etc/passwd | wc -l`
if [ $user -eq 0 ]
then
echo There are no users with a default shell of bash
else
echo The number of users whose default shell is bash is $user
fi[root@shell scripts2]# chmod +x user2.sh
[root@shell scripts2]# ./user2.sh
The number of users whose default shell is bash is 3