首页 > 系统相关 >shell基础知识记录

shell基础知识记录

时间:2023-05-23 22:33:14浏览次数:24  
标签:head shell 记录 18 基础知识 test bob echo root

调式脚本的方式

# 调试整个脚本执行内容
sh -vx test.sh # 不加-v,只显示脚本中运行的代码,不显示注释信息

# 调试脚本语法是否有问题
sh -n test.sh 

# 调试脚本的一部分,将脚本中需要调试的部分用set -x和set +x包含起来
[root@head test]# cat test.sh 
#!/usr/bin/bash
set -x
a=123
echo ${a}
set +x

if [ $a ];then
    echo 111
fi

变量匹配的规则

# ${变量#匹配规则} 从头开始匹配,删除匹配到的最短部分
[root@head ~]# echo ${url}
www.test.com.cn
[root@head ~]# echo ${url#*t}
est.com.cn

# ${变量##匹配规则} 从头开始匹配,删除匹配到的最长部分
[root@head ~]# echo ${url##*t}
.com.cn

# ${变量%匹配规则} 从尾开始匹配,删除匹配到的最短部分
[root@head ~]# echo ${url%t*}
www.tes

# ${变量%匹配规则}从尾开始匹配,删除匹配到的最长部分
[root@head ~]# echo ${url%%t*}
www.

变量替换的规则

# ${变量/旧字符串/新字符串} 替换其中的一个
[root@head ~]# echo ${test}
aabbccdd
[root@head ~]# echo ${test/a/1}
1abbccdd

# ${变量//旧字符串/新字符串} 替换所有
[root@head ~]# echo ${test//a/1}
11bbccdd

参数相关

$* # 表示传递到shell脚本中的所有参数,如参数1 2 3, 则表示"1  2 3", 将所有参数收集为一个参数
$@ # 表示传递到shell脚本中的所有参数,如参数1 2 3,则表示"1" "2" "3", 将参数都收集起来,不改变参数的数量
$# # 表示传递到脚本中参数的个数
$0 # 表示执行脚本的文件名
$1 # 表示第一个参数,$2 $3以此类推
$$ # 表示当前脚本运行的进程ID号
$? # 仅表示上一次命令执行的状态,只有0表示正常执行,其他都表示错误执行

 read获取参数和变量设置

# 后面的hobby即为标量的名称
[root@head test]# read -p "your hobby:" hobby
your hobby:walking
[root@head test]# echo $hobby
walking

# 常量
[root@head test]# a=123
[root@head test]# readonly a
[root@head test]# a=111
-bash: a: readonly variable

# 取消设置变量
[root@head test]# b=111
[root@head test]# echo $b
111
[root@head test]# unset b
[root@head test]# echo $b

普通数组

# 申明普通数组
# 方式一:
array1=("bob" "18" "teacher")
# 方式二:
array2=([0]="jack" [2]=20 [3]="worker")
# 方式三
[root@head test]# array3[0]="alice"
[root@head test]# array3[1]=22
[root@head test]# array3[2]="docter"

# 查看数组
declare -a

# 通过索引访问数组元素
echo ${array2[1]} # 正向索引
echo ${array2[-1]} # 负向索引

关联数组

# 申明关联数组
# 方式一:
[root@head test]# declare -A info
[root@head test]# info["name"]="bob"
[root@head test]# info["age"]=18
[root@head test]# info["job"]="teather"
# 方式二: 
[root@head test]# declare -A info3
[root@head test]# info3=(["name"]="bob" ["age"]=20 ["job"]="actor")

# 查看关联数组
declare -A

访问关联数组
[root@head test]# echo ${info[*]}
teather bob 18
[root@head test]# echo ${info["name"]}
bob
[root@head test]# echo ${info["age"]}
18

数组补充

# 查看数组元素
[root@head ~]# array1=("bob" 18 "actor")
[root@head ~]# echo ${array1[*]}
[root@head ~]# echo ${array1[@]}

# 删除数组某个元素
[root@head ~]# unset array1[0]

# 删除整个数组
[root@head ~]# unset array1

# 数组元素切片
[root@head ~]# array1=("bob" 18 "actor" "drinking" "male")
[root@head ~]# echo ${array1[*]:1} # 获取从第一个元素往后的所有元素
18 actor drinking male
[root@head ~]# echo ${array1[*]:1:3} # 获取从第一个元素往后的3个元素
18 actor drinking

# 数组元素的替换
[root@head ~]# echo ${array1[*]/male/female} # 将male替换成female
bob 18 actor drinking female
[root@head ~]# echo ${array1[*]/*a*/test} # 将包含a的元素都替换成test
bob 18 test drinking test

# 数组元素的循环
[root@head ~]# for item in ${array1[*]}
> do
> echo $item
> done
bob
18
actor
drinking
male

[root@head ~]# array2=(["name"]="bob" ["age"]=18 ["job"]="actor" ["hobby"]="drinking" ["gender"]="male")
[root@head ~]# for item in ${array2[*]}
> do
> echo $item
> done
drinking
actor
male
bob
18

[root@head ~]# for i in ${!array2[*]} # 获取到数组的所有索引
> do
> echo $i ${array2[$i]}
> done
hobby drinking
job actor
gender male
name bob
age 18

获取变量的长度

[root@head test]# aa="this is a test"
[root@head test]# echo ${#aa}
14
[root@head test]# echo $aa | wc -L
14
[root@head test]# echo $aa | awk '{print length}'
14
[root@head test]# expr length "$aa"
14

字符的切片

[root@head test]# echo $aa
this is a test
[root@head test]# echo ${aa:2} # 从第二个字符,往后的所有字符
is is a test
[root@head test]# echo ${aa:2:4} # 从第二个字符,往后的4个字符
is i
[root@head test]# echo ${aa::4} # 前面的4个字符
this

算数运算符

bc:支持浮点型运算

[root@head test]# echo `echo 1 + 1 | bc`
2
[root@head test]# echo `echo 6 % 5 | bc`
1
[root@head test]# echo `echo 6 \* 5 | bc`
30
[root@head test]# echo `echo 6 - 5 | bc`
1
[root@head test]# echo `echo "scale=2;5.0/3.0"|bc` # 设置保留两位小数
1.66

expr:不支持浮点型运算,且数字间要留有空格

[root@head test]# echo `expr 1 + 1`
2
[root@head test]# echo `expr 3 - 2`
1
[root@head test]# echo `expr 3 \* 3`
9
[root@head test]# echo `expr 6 / 5`
1
[root@head test]# echo `expr 6.0 / 5.0`
expr: non-integer argument

$(()): 不支持浮点型运算

[root@head test]# echo $(( 1 + 1 ))
2
[root@head test]# echo $((1+1))
2
[root@head test]# echo $((3-2))
1
[root@head test]# echo $((3*3))
9
[root@head test]# echo $((9/3))
3
[root@head test]# echo $((9.0/3))
-bash: 9.0/3: syntax error: invalid arithmetic operator (error token is ".0/3")

$[]同$(())和expr:不支持浮点型运算

let不支持浮点型运算,且只能赋值,不能直接输出

[root@head test]# let 1+1
[root@head test]# let res=1+1
[root@head test]# echo $res
2
[root@head test]# let res=3-2
[root@head test]# echo $res
1
[root@head test]# let res=3*3
[root@head test]# echo $res
9
[root@head test]# let res=6/3
[root@head test]# echo $res
2
[root@head test]# let res=6.0/3
-bash: let: res=6.0/3: syntax error: invalid arithmetic operator (error token is ".0/3")

测试运算符

test和[]用法是一样的

[root@head test]# test ! -d /home
[root@head test]# echo $?
1
[root@head test]# test -d /home
[root@head test]# echo $?
0
[root@head test]# [ -d /home ] # 注意两边有空格,[[]]与[]类似,但是[[]]支持正则
[root@head test]# echo $?
0

字符串测试

# != 测试两个字符串不相等,注意符号两边也有空格
[root@head test]# [ "test" != "test" ]
[root@head test]# echo $?
1
# == 测试两个字符串相等 
[root@head test]# [ "test" == "test" ]
[root@head test]# echo $?
0
# -z 字符串长度为0则为真
[root@head test]# [ -z "" ]
[root@head test]# echo $?
0
[root@head test]# [ -z "test" ]
[root@head test]# echo $?
1
# -n 字符串长度不为0则为真
[root@head test]# [ -n "" ]
[root@head test]# echo $?
1
[root@head test]# [ -n "test" ]
[root@head test]# echo $?
0

数值比较

# -eq 等于
[root@head test]# [ 3 -eq 4 ] 
[root@head test]# echo $?
1
# -ne 不等于
[root@head test]# [ 3 -ne 4 ]
# -gt 大于
[root@head test]# [ 4 -gt 3 ]
# -lt 小于
[root@head test]# [ 3 -lt 4 ]
# -ge 大于等于
[root@head test]# [ 3 -ge 3 ]
# -le 小于等于 
[root@head test]# [ 3 -le 3 ]

(())使用的数值比较符号

[root@head test]# ((3<2))
[root@head test]# ((3>=2))
[root@head test]# ((3<=2))
[root@head test]# ((3==3))
[root@head test]# ((3>2)) && ((5>4))
[root@head test]# echo $?
0
[root@head test]# ((3>2)) || ((5<4))
[root@head test]# echo $?
0
[root@head test]# ((3>2 && 5>4))
[root@head test]# echo $?
0

浮点数的比较

bc的结果中1代表正确,0代表错误

[root@head test]# echo "1.2>1.0" | bc
1
[root@head test]# echo "1.2<1.0" | bc
0
[root@head test]# echo "1==1" | bc
1
[root@head test]# echo "1!=1" | bc
0

其他补充

# $()中执行命令,且可以嵌套
[root@head test]# $(echo hostname)
[root@head test]# $(echo ls $(pwd))

# ()在子shell中运算
[root@head test]# (aaa=123)
[root@head test]# echo $aaa

 

标签:head,shell,记录,18,基础知识,test,bob,echo,root
From: https://www.cnblogs.com/fdsimin/p/17286278.html

相关文章

  • shell函数和三剑客
    函数基本语法#写法一:function函数名(){函数要执行的命令}#写法二,省略():function函数名{函数要执行的命令}#写法三,省略function:函数名(){函数要执行的命令}#调用函数#没有参数的函数函数名#有参数的函数函数名参数一参数二函数的......
  • 安全测试的一些记录__基于fiddler工具
    工作中做了部分安全测试,这里记录执行安全测试时,遇到和学习到的一些内容,其中在安全测试时,也涉及到了fiddler工具的使用:1、公司的安全规范中,规定平台涉及到文件上传时,后台接口需要对文件大小、文件格式、文件名长度做接口限制。当然这种限制是肯定的,因为前端虽然会做这些限制,来防......
  • Shell
    shellShell是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。Shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。Shell脚本是一种为shell编写的脚本程序。Shell编程跟JavaScript、php编......
  • 记录--使用率比较低的10个Web API
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助avaScript中有些API可能使用率比较低,下面我们逐一介绍它们的用法和使用场景。至于标题,主要是想让你进来看看,兄弟们别打我!BlobAPIBlobAPI用于处理二进制数据,可以方便地将数据转换为Blob对象或从Blob对象读取数......
  • Linux shell command make & Makefile All In One
    Linuxshellcommandmake&MakefileAllInOne脚本自动化构建工具make&makefilehttps://linux.xgqfrms.xyz/linux_basic/1010index.htmmake第二十二章、1.3https://linux.xgqfrms.xyz/linux_basic/0520source_code_and_tarball.htm#intro_makemakefile第二十二章、......
  • shell特殊符号梳理
    1$相关关键词shell中与@和n等经常被使用,但是有时候仍然对部分符号记忆不是很深刻,特地整理成表格方便记忆。-描述备注$0当前脚本文件名$n传递给脚本或函数的参数$#传递给脚本或函数的所有参数个数$*传递给脚本或函数的所有参数当它们被双引号("“)包含时,”$*"会将所有的参数作为......
  • 算法刷题记录:NC22227 约瑟夫环
    题目链接https://ac.nowcoder.com/acm/problem/22227解题思路模拟环。这道题顺序数就行,顺序是逆时针,逆时针的箭头是往左拐的,变成直线后趋于正半轴所以是+。不过,这道模拟环并没有说从idx号开始,往左/右数几个人,所以不需要考虑+或-。因为不会越界,所以也不用额外%n。AC代码......
  • (二)shell脚本基础
    shell条件测试read内置命令-p#设置提示信息-t#等待用户输入超时,timeout​[root@shellopt]#read-t15-p"pleaseentername,age:"you_nameyou_agepleaseentername,age:gm18[root@shellopt]#echo$you_name$you_agegm18字符串条件测试test命令test评......
  • 记录学习 ShardingSphere 遇到的坑
    ​ 首先来一下官网文档地址:概览::ShardingSphere一、数据库分为2个库,每个库有2张表​编辑 二、配置官方有很多配置方式(ShardingSphere-JDBC::ShardingSphere),这边使用的是YAML配置(YAML配置::ShardingSphere)规则配置:数据分片遇到的坑以下是官方给出的参数配置解......
  • SQL删除重复的记录(只保留一条)
    首先新建表:--创建示例表CREATETABLEt(idINTIDENTITY(1,1)PRIMARYKEY,aVARCHAR(10),bVARCHAR(10))--插入数据INSERTINTOtSELECT'aa','bb'UNIONALLSELECT'a1','bgb'UNIONALLSELECT'aa','bb'UNION......