当前位置:网站首页>Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH
2022-08-09 10:29:00 【Mary Soda Meatloaf】
#!/bin/bash#inpathin_path(){cmd=$1 ourpath=$2 result=1 #1oldIFS=$IFS IFS=":" #2for directory in $ourpathdoif [ -x $directory/$cmd ]; then #3result=0fidoneIFS=$oldIFSreturn $result}checkForCmdInpath(){var=$1if [ "$var" != "" ];thenif [ "${var:0:1}" = "/" ];then #4if [ ! -x $var ];thenreturn 1fielif ! in_path $var "$PATH";thenreturn 2;fifi}checkForCmdInpath "$1"case $? in0) echo "$1 found in PATH" ;;1) echo "$1 not found or not executable" ;;2) echo "$1 not found in PATH" ;;esacexit 0
1:Default variables for shell scripts
$# is the number of arguments passed to the script
$0 is the name of the script itself
$1 is the first argument passed to the shell script
$2 is the second argument passed to the shell scriptparameters
[email protected] is a list of all parameters passed to the script
$* is a single string showing all parameters passed to the script, unlike positional variables, the parameters can be more than 9
$$is the current process ID number of the script running
$? is the exit status of the last command, 0 means no error, other means there is an error
sh test.sh opt1 opt2 opt3 opt4$0 $1 $2 $3 $4
2:IFS separator
https://zhuanlan.zhihu.com/p/36513249
3: Judgment symbol [ ]
Each component in'[ ]' needs to be separated by spaces.Variables are best surrounded by quotes.
Check file permissions
-r Whether the file exists, and the readable permission
Whether the -w file exists, and the writable permission
Whether the -x file exists, and the executable permission
Whether the -u file exists, and hasSUID attribute
-g Whether the file exists and has the SGID attribute
Whether the file exists and has the Sticky bit attribute
-s Whether the file exists and is a non-blank file
4: Variable segmentation syntax, starting from the offset, truncating according to the given length (if no length is provided, the rest of the string will be returned)
Script 1 running result
边栏推荐
- pytorch widedeep文档
- 笔记本电脑使用常见问题,持续更新
- tensor.eq() tensor.item() tensor.argmax()
- xmms的歌词显示及音量控制OK
- [贴装专题] 贴装流程中涉及到的位置关系计算
- 1001 害死人不偿命的(3n+1)猜想 (15 分)
- electron 应用开发优秀实践
- 条件控制语句
- 学习NET-SNMP之一 ---------编译NET-SNMP程序。
- Solve the ali cloud oss - the original 】 【 exe double-click response can't open, to provide a solution
猜你喜欢
随机推荐
2021-01-11-雪碧图做表情管理器
使用cpolar远程连接群晖NAS(创建临时链接)
OpenGL ES2.0编程三部曲(转载自MyArrow)
可能95%的人还在犯的PyTorch错误
unix环境编程 第十五章 15.3 函数popen和pclose
Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
OpengGL绘制立方体的三种方法
1005 继续(3n+1)猜想 (25 分)
Battery modeling, analysis and optimization (Matlab code implementation)
[相机配置] 海康相机丢包配置环境
强化学习 (Reinforcement Learning)
小程序员的发展计划
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
1003 我要通过! (20 分)
实现下拉加载更多
Cpolar内网穿透的面板功能介绍
今天做了手机播放器的均衡器
Win32控件--------------------WM_DRAWITEM消息测试程序
unix环境编程 第十五章 15.9 共享存储
pytorch widedeep文档