当前位置:网站首页>shell语法个人运用中问题小结
shell语法个人运用中问题小结
2022-07-22 03:41:00 【狛枝zi】
1,shell脚本执行报错:
-bash: ./test.sh: bin/bash: bad interpreter: No such file or directory
报错原因:脚本头部写错了!
2,定义变量:
不可以中间空格,错误写法:
name1 = 'yxc'
name2 = "yxc"
name3 = yxc
正确写法:
name1='yxc' # 单引号定义字符串
name2="yxc" # 双引号定义字符串
name3=yxc # 也可以不加引号,同样表示字符串
3,只读变量不能被unset删除
4,declare -x name x为设置为环境变量 +号为取消
5,向脚本传递参数时,参数个数超过一位需要用大括号括起来
6,注意参数相关变量也是参数,也是需要输出不能脱离存在的
错误写法:
$(ls)
正确写法:
`ls`
7,在读取整个数组时,未定义的部分就不会显示。
比如定义了array[0]array[1]array[2]array[1000],利用echo ${array[@]}输出时,只会显示我们定义的数组那4个元素的值
8,对上例求长度时,结果是4,而不是1000
9,正确写法:
str="Hello World!"
echo `expr length "$str"` # ``不是单引号,表示执行该命令,输出12
对于expr命令,因为将结果输出到stdout(标准输出中),所以要输出记得使用$()或``
错误写法:
str="Hello World!"
echo `expr length $str`
报错:
expr: syntax error: unexpected argument 'World!'
原因:空格后都没有截取,所以变量外最好加上"",可以防止变量中有空格报错
10,expr substr STRING POSITION LENGTH
需要注意下标从1开始
11,弄清取模是什么意思。。。。3%4和4%3的区别要注意
12,整数表达式中需要转义的:
\* \( \)
13,逻辑关系表达式中的都要转义,也可以将特殊字符用引号引起来
14,
[email protected]:~$ test -e test1.sh && echo "exist" || echo "Not exist"
Not exist
[email protected]:~$ echo $?
0
之所以exit code是0不是1,是因为test的是后面整个
边栏推荐
- Codeforces Round #719 (Div. 3)
- NB-IOT的基礎知識
- Understand the three mountains of JS
- DOM style operation
- JVM内存模型:类加载器的分类和获取
- SSTI簡單總結和CISCN 2019華東南]Double Secret
- Preloading and lazy loading of DOM
- Trigger Basics (medium)
- SSTI simple summary and ciscn 2019 southeast China]double secret
- The big problem of HA automatic failover (active) namenode
猜你喜欢
DOM style operation
NFT 多样化的发行方式
SAP 电商云 Spartacus UI 客户系统的跨域请求
Scala variables and data types (2)
Online RPC timeout troubleshooting and subsequent GC tuning ideas
Glide 源码解析
Basic knowledge of nb-iot
科普 | 如何创建一个DAO?
M using similink simulation module to realize dynamic simulation of multipath channel
"Demand optimization" uses PostMessage to solve the cross domain problem of localstorage
随机推荐
Preparation of albumin sorafenib /gd2o3/cus composite albumin nanoparticles /alb-icg-dox albumin nanoparticles coated with ICG & dox
你了解Lumen和Nanite吗?在ue5场景制作中如何使用呢?
Helm理解和使用
Segment tree learning record
QT notes - customized qlistwidget
科普 | 如何创建一个DAO?
How does SCM work?
Collagen protease loaded albumin composite nanoparticles / bovine serum albumin coated ceria nano artificial enzyme
机器学习基础篇(5)图像轮廓
分布式事务,原理简单,写起来全是坑
[elaborate] ES6 import, export and other operations
什么是单片机?单片机的组成部分有哪些?
操作教程:大华摄像头通过GB28181协议注册EasyCVR平台的详细配置
包裹DNA和光敏剂Ce6的白蛋白纳米粒/棉酚白蛋白纳米粒/拉帕替尼白蛋白纳米粒
Les bases de l'IOT - NB
M using similink simulation module to realize dynamic simulation of multipath channel
线段树学习记录
18. What is the persistence mechanism of redis? Respective advantages and disadvantages?
Codeforces Round #719 (Div. 3)
P6327 interval plus interval sin sum