当前位置:网站首页>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
边栏推荐
猜你喜欢
Tom Morgan | Twenty-One Rules of Life
分类预测 | MATLAB实现CNN-GRU(卷积门控循环单元)多特征分类预测
Qt 国际化翻译
【报错记录】解决华擎J3455-ITX不插显示器无法开机的问题
使用.NET简单实现一个Redis的高性能克隆版(四、五)
使用cpolar远程连接群晖NAS(创建临时链接)
3D打印了这个杜邦线理线神器,从此桌面再也不乱了
[贴装专题] 视觉贴装平台与贴装流程介绍
【 original 】 VMware Workstation implementation Openwrt soft routing, the ESXI, content is very detailed!
Demand side power load forecasting (Matlab code implementation)
随机推荐
元组 字典 集合
xmms播放器加了播放列表的管理功能
[贴装专题] 贴装流程中涉及到的位置关系计算
GeoScene Pro 2.1下载地址与安装基本要求
Attentional Feature Fusion
unix环境编程 第十四章 14.8 存储映射I/O
How tall is the B+ tree of the MySQL index?
Transformer+Embedding+Self-Attention原理详解
使用cpolar远程连接群晖NAS(创建临时链接)
OpenGL 2.0编程例子
学长告诉我,大厂MySQL都是通过SSH连接的
Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
[项目配置] 配置Qt函数库和ui界面库的封装并调用的项目
第二周作业
conditional control statement
Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
虚拟列表key复用问题
小程序员的发展计划
Technology Sharing | How to simulate real usage scenarios?mock technology to help you