Shell条件变量练习

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" | awk '/https/ {print "https"}'    
https

[root@shell ~]# echo $url | awk -F: '{print $1}'
https

[root@shell ~]# echo ${url%:*}     #字符串变量切片
https

[root@shell ~]# echo $url | cut -d : -f1
https


2)截取网站访问账号信息

[root@shell ~]# echo $url | awk -F/ '{print $4}'
weixin_45029822

[root@shell ~]# echo $url | awk -F/ '{print $(NF-3)}'
weixin_45029822

[root@shell ~]# echo $url | cut -d / -f4
weixin_45029822

[root@shell ~]# echo $url | grep -o "weixin_45029822"
weixin_45029822

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 ~]# sed -n '/bash$/p' /etc/passwd   #shell为bash的用户有三个
root:x:0:0:root:/root:/bin/bash
kxy:x:1000:1000:kxy:/home/kxy:/bin/bash
fox:x:1001:1001::/home/fox:/bin/bash

方法一:使用grep加wc统计bash的行数

[root@shell scripts2]# vim user2.sh +

#!/bin/bash

user=`grep "bash" /etc/passwd | wc -l`
if [ $user -eq 0 ]                         #通过bash的行数来判断
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

方法二:使用awk对文本进行处理

[root@shell scripts2]# awk '/bash/ {print "bash"}' /etc/passwd | wc -l
3

[root@shell scripts2]# vim user2.sh +

#!/bin/bash

user=$(awk '/bash/ {print "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]# ./user2.sh 
The number of users whose default shell is bash is 3

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值