当前位置:网站首页>Shell变量$#,$@,$0,$1,$2,${},##,%%使用解释以及好用的shell格式化工具
Shell变量$#,$@,$0,$1,$2,${},##,%%使用解释以及好用的shell格式化工具
2022-04-21 09:47:00 【从零开始的数据猿】
使用解释
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。
如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。 如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
假设定义了一个变量为:
file=/dir1/dir2/dir3/my.file.txt
可以用${ }分别替换得到不同的值:
${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt
${file#*.}:删掉第一个 . 及其左边的字符串:file.txt
${file##*.}:删掉最后一个 . 及其左边的字符串:txt
${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:删掉第一个 / 及其右边的字符串:(空值)
${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
记忆的方法为:
# 是 去掉左边(键盘上#在 $ 的左边)
%是去掉右边(键盘上% 在$ 的右边)
单一符号是最小匹配;两个符号是最大匹配
${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2
也可以对变量值里的字符串作替换:
${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt
if条件
if <test_expression>; then
<command-to-execute>
elif <test_expression>; then
<command-to-execute>
else
<command-to-execute>
fi
常见判断:
-d :判断制定的是否为目录
-z:判断制定的变量是否存在值
-f:判断制定的是否为文件
-L:判断制定的是否为符号链接
-r:判断制定的是否可读
-s:判断存在的对象长度是否为0
-w:判断制定的是否可写
-x:判断存在的对象是否可以执行
!:测试条件的否定符号
这些文件操作很多时候给脚本编程带来方便,尤其是用在if条件语句中
Shell格式化
参考:https://blog.csdn.net/wangzhaotongalex/article/details/73321766
https://blog.csdn.net/wangzhaotongalex/article/details/73321766
版权声明
本文为[从零开始的数据猿]所创,转载请带上原文链接,感谢
https://nmydt.blog.csdn.net/article/details/124295224
边栏推荐
猜你喜欢

My life of Honker Security Commando

Fashion cloud learning -js implementation disables right click and F12

2022制冷与空调设备运行操作考试题模拟考试题库及答案

Download the first analysis report on China's database industry!

2022危险化学品经营单位主要负责人上岗证题库及模拟考试

vr全景适合那些行业
事务的隔离级别与MVCC

某小厂面试题:什么是虚假唤醒?

【手拉手 带你准备电赛】April Tag标记跟踪(3D定位)详解

Multithreaded copy set (New 4)
随机推荐
2022年制冷与空调设备运行操作考试试题模拟考试平台操作
Is synchronized really heavy?
2022 refrigeration and air conditioning equipment operation test question simulation test question bank and answers
SQL question set [(2)]
You are using pip version 20.2.3; however, version 22.0.4 is available. You should consider
利用已有的标签文件生成训练集和验证集(YOLO)
m3u8视频下载器-idm突破无法下载受保护的数据, 下不了限制
2022危险化学品经营单位主要负责人上岗证题库及模拟考试
[pycharm plugins] download online to install the translation plug-in
刷题记录(leetcode)
ZABBIX 5.4 server installation
[Netty ]自己实现一个redis客户端难吗?
[summary] 1296 - summarize 12 common mobile terminal H5 and hybrid development problems
2022 question bank and simulation examination of work license of main principals of hazardous chemical business units
vr全景适合那些行业
Add pjax to the website without refresh and uninterrupted page change music
【无标题】
Responsive layout to realize the static page of ghost blog home page
One trick is to solve the servlet of servlet [dispatcher servlet] Init() threw an exception
操作系统 - 线程安全 - 学习