当前位置:网站首页>shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
2022-08-09 10:26:00 【玛丽苏打肉饼】
#!/bin/bash
#inpath
in_path()
{
cmd=$1 ourpath=$2 result=1 #1
oldIFS=$IFS IFS=":" #2
for directory in $ourpath
do
if [ -x $directory/$cmd ]; then #3
result=0
fi
done
IFS=$oldIFS
return $result
}
checkForCmdInpath()
{
var=$1
if [ "$var" != "" ];then
if [ "${var:0:1}" = "/" ];then #4
if [ ! -x $var ];then
return 1
fi
elif ! in_path $var "$PATH";then
return 2;
fi
fi
}
checkForCmdInpath "$1"
case $? in
0) echo "$1 found in PATH" ;;
1) echo "$1 not found or not executable" ;;
2) echo "$1 not found in PATH" ;;
esac
exit 01:shell脚本的默认变量
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
[email protected] 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
sh test.sh opt1 opt2 opt3 opt4
$0 $1 $2 $3 $42:IFS分隔符
https://zhuanlan.zhihu.com/p/36513249
3:判断符号[ ]
’[ ]‘里面每个组件都需要用空格分隔。变量最好用引号包围。
检查文件权限
-r 文件是否存在,且可读权限
-w 文件是否存在,且可写权限
-x 文件是否存在,且可执行权限
-u 文件是否存在,且具有SUID属性
-g 文件是否存在,且具有SGID属性
-k 文件是否存在,且具有 Sticky bit 属性
-s 文件是否存在,且为 非空白文件
4:变量切分语法,从偏移处开始,按照给定长度截取(如果没有提供长度,则返回余下的全部字符串)

脚本1运行结果

边栏推荐
- Practical skills: a key for image information in the Harbor, quick query image
- Throwing a question? The execution speed of the Count operation in the Mysql environment is very slow. You need to manually add an index to the primary key---MySql optimization 001
- BERT预训练模型(Bidirectional Encoder Representations from Transformers)-原理详解
- Dream Notes 0809
- 关于页面初始化
- 公里周日
- [Halcon&定位] 解决Roi区域外的模板匹配成功
- Qt 国际化翻译
- LeetCode(剑指 Offer)- 25. 合并两个排序的链表
- 实验室装修及改造工程程序简介
猜你喜欢

机器学习-逻辑回归(logistics regression)

3D打印了这个杜邦线理线神器,从此桌面再也不乱了

Battery modeling, analysis and optimization (Matlab code implementation)

阿里神作!吃透这份资料入厂率高达99%
![[贴装专题] 贴装流程中涉及到的位置关系计算](/img/72/a60a51c86e641749f38fab66f1236a.png)
[贴装专题] 贴装流程中涉及到的位置关系计算

蓄电池建模、分析与优化(Matlab代码实现)

ESIM(Enhanced Sequential Inference Model)- 模型详解

深度学习--神经网络(基础讲解)

Quick sort eight sorts (3) 】 【 (dynamic figure deduction Hoare, digging holes, front and rear pointer method)

electron 应用开发优秀实践
随机推荐
强化学习 (Reinforcement Learning)
动态内存管理
KeyBERT和labse提取字符串中的关键词
蓄电池建模、分析与优化(Matlab代码实现)
单元测试1之单元测试的引用
[贴装专题] 贴装流程中涉及到的位置关系计算
Throwing a question? The execution speed of the Count operation in the Mysql environment is very slow. You need to manually add an index to the primary key---MySql optimization 001
WUSTOJ:n个素数构成等差数列
[相机配置] 海康相机丢包配置环境
在犹豫中度过了老多天,今天的工作时记录
循环嵌套以及列表的基本操作
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
抛出一个问题? Mysql环境下进行Count操作执行的时候速度很慢_需手动给主键添加索引---MySql优化001
1001 害死人不偿命的(3n+1)猜想 (15 分)
3D printed this DuPont cable management artifact, and the desktop is no longer messy
公里周日
一天半的结果——xmms on E2
函数二
xmms的均衡器试验成功 音效相当不错 比rockbox可能还要好
xmms已经发布到v1.3了,好久没写博客了