当前位置: 首页 > news >正文

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 | 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

http://www.lryc.cn/news/247170.html

相关文章:

  • 【PHP】MySQL简介与MySQLi函数(含PHP与MySQL交互)
  • vscode在Windows上安装插件提示错误xhr failed
  • SHAP(一):具有 Shapley 值的可解释 AI 简介
  • C++数据结构:图
  • 「C++」红黑树的插入(手撕红黑树系列)
  • 2023年生肖在不同时间段的运势预测
  • ERRO报错
  • shiyan
  • 深度学习黎明时期的LeNet:揭开卷积神经网络的序幕
  • 跨越威胁的传说:揭秘Web安全的七大恶魔
  • 【SpringCloud系列】@FeignClient微服务轻舞者
  • 【数据库设计和SQL基础语法】--SQL语言概述--SQL的基本结构和语法规则(一)
  • 使用oxylabs代理国外ip请求openai接口报错记录
  • 搜索引擎语法
  • @ResponseBody详解
  • 一些关于开关电源经典回答
  • Linux-文件夹文件赋权、文件指定修改用户和用户组
  • 【Java】7. 类型转换和类型判断
  • c语言练习12周(15~16)
  • 2023-简单点-机器学习中矩阵向量求导
  • 帮管客CRM SQL注入漏洞复现
  • 如何编写自己的python包,并在本地进行使用
  • xv6 磁盘中断流程和启动时调度流程
  • Spring Security 6.x 系列(6)—— 显式设置和修改登录态信息
  • Linux的软件安装
  • 443. 压缩字符串
  • Python面经【6】
  • 2020年6月9日 Go生态洞察:VS Code Go扩展加入Go项目
  • C语言错误处理之“非局部跳转<setjmp.h>头文件”
  • 【SpringCloud】微服务架构设计模式