入门 shell 从脚本开始 - lazy_find
编写脚本实现在指定文件路径下查找文件夹或文件名。
#!/bin/sh
# lazy find # GNU All-Permissive License
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty. ## help function function helpu {
echo " "
echo "Fuzzy search for filename."
echo "$0 [--match-case|--path] filename"
echo " "
exit
} ## set variables MATCH="-iname"
SEARCH="." ## parse options while [ True ]; do
if [ "$1" = "--help" -o "$1" = "-h" ]; then
helpu
elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then
MATCH="-name"
shift 1
elif [ "$1" = "--path" -o "$1" = "-p" ]; then
SEARCH="${2}"
shift 2
else
break
fi
done ## sanitize input filenames
## create array, retain spaces ARG=( "${@}" )
set -e ## catch obvious input error if [ "X$ARG" = "X" ]; then
helpu
fi ## perform search for query in ${ARG[*]}; do
/usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*"
done
- Shebang 行
- 变量
- 循环
- 列表
- 参数
- 函数
- 判断
- set
- 由字母,数字,下划线组成。
- 变量名开头不能是数字,可以是字母或下划线。
- 变量名中不允许出现空格和标点符号。
MATCH="-iname"
MATCH=-iname NAME="lian hua"
echo $MATCH
echo ${MATCH}
[lianhuasheng@demo home]$ echo $lianhua [lianhuasheng@demo home]$
set -e command1
command2 set +e
while condition; do
commands
done
## 1. normal for script
for variable in list; do
commands
done ## demo for script
#!/bin/bash for i in lian hua sheng; do
echo $i
done ## 2. c style for script
for (( expression1; expression2; expression3 )); do
commands
done
[lianhuasheng@demo home]$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
[lianhuasheng@demo home]$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
- $0: 脚本文件名。
- $1 - $9: 脚本的第 1 个参数到第 9 个参数,超过 9 个参数使用 ${10}, ${11}... 形式表示。
- $#: 传入脚本的参数数量。
- $@: 传入脚本中的全部参数,它是一个列表。
[lianhuasheng@demo home]$ cat parameter.sh
#!/bin/bash
# script.sh echo "all parameters:" $@
echo "number of parameters:" $#
echo '$0 = ' $0
echo '$1 = ' $1
echo '$2 = ' $2
echo '$3 = ' $3 [lianhuasheng@demo home]$ ./parameter.sh lian hua
all parameters: lian hua
number of parameters: 2
$0 = ./parameter.sh
$1 = lian
$2 = hua
$3 = # $3 没有读取参数,并没有报错
for value in "$@"; do
echo ${value}
done
[lianhuasheng@demo home]$ arg=(lian hua sheng)
[lianhuasheng@demo home]$ echo ${arg[@]}
lian hua sheng
[lianhuasheng@demo home]$ arg[0]=da
[lianhuasheng@demo home]$ arg[1]=shuai
[lianhuasheng@demo home]$ arg[2]=ge
[lianhuasheng@demo home]$ echo ${arg[@]}
da shuai ge
[lianhuasheng@demo home]$ read -a arg
hei hei hei
[lianhuasheng@demo home]$ echo ${arg[@]}
hei hei hei
[lianhuasheng@demo home]$ echo ${arg[@]}
da shuai ge
[lianhuasheng@demo home]$ echo ${arg[1]}
shuai
[lianhuasheng@demo home]$ echo ${arg[@]}
da shuai ge
[lianhuasheng@demo home]$ echo ${arg[*]}
da shuai ge
[lianhuasheng@demo home]$ arg=(lian "hua sheng" da "shuai-ge")
[lianhuasheng@demo home]$ for value in ${arg[*]}; do echo "parameter: " ${value}; done
parameter: lian
parameter: hua
parameter: sheng
parameter: da
parameter: shuai-ge
[lianhuasheng@demo home]$ for value in "${arg[*]}"; do echo "parameter: " ${value}; done
parameter: lian hua sheng da shuai-ge
[lianhuasheng@demo home]$ for value in ${arg[@]}; do echo "parameter: " ${value}; done
parameter: lian
parameter: hua
parameter: sheng
parameter: da
parameter: shuai-ge
[lianhuasheng@demo home]$ for value in "${arg[@]}"; do echo "parameter: " ${value}; done
parameter: lian
parameter: hua sheng
parameter: da
parameter: shuai-ge
[lianhuasheng@demo home]$ echo $arg
lian
[lianhuasheng@demo home]$ echo ${arg}
lian
[lianhuasheng@demo home]$ echo $arg[0]
lian[0]
[lianhuasheng@demo home]$ echo ${arg}[0]
lian[0]
if conditions; then
commands
elif conditions; then
commands
else
commands
fi
- 文件判断
- 字符串判断
- 整数判断
- 正则判断
- test 判断逻辑运算
- 算术判断
- 普通命令的逻辑运算
- [ string ]:string 不为空,则判断为真。
- [ string1 = string2 ]:string1 和 string2 字符串相同则判断为真,同 [ string1 == string2 ]。
- [ string1 != string2 ]:string1 和 string2 不同则判断为真。
- [ -n string ]:string 长度大于 0 则判断为真。
- [ -z string ]:string 长度等于 0 则判断为真。
# 1 style of test
test expression # 2 style of test
[ expression ] # 3 style of test
[[ expression ]]
# 1 style of function
function fn {
commands
} # 2 style of function
fn() {
commands
}
#!/bin/bash
function input_param {
echo "I am input function, the input parameters are: "
echo "$@"
}
function input_defined_param {
echo "I am input defined function, the defined parameters are: "
echo "The first parameter is: " "${1}"
echo "The second parameter is: " "${2}"
echo "The totally parameters are: " "${@}"
}
input_param ${@}
input_defined_param lian hua
[lianhuasheng@demo home]$ ./function_return.sh
127
[lianhuasheng@demo home]$ cat function_return.sh
#!/bin/bash function_return() {
return 127
} function_return
echo $?
[lianhuasheng@demo home]$ cd lianhua
-bash: cd: lianhua: No such file or directory
[lianhuasheng@demo home]$ echo $?
1 [lianhuasheng@demo home]$ cd query/
[lianhuasheng@demo query]$ echo $?
0
[lianhuasheng@demo home]$ cat function_exit.sh
#!/bin/bash function_exit() {
exit 127
} function_exit
echo "kissMe"
[lianhuasheng@demo home]$ ./function_exit.sh # echo "kissMe" 没有执行到
[lianhuasheng@demo home]$ echo $?
127
[lianhuasheng@demo home]$ vi function_exit.sh
[lianhuasheng@demo home]$ ./function_exit.sh
[lianhuasheng@demo home]$ echo $?
0
[root@demo bash]# cat function_variable.sh
#!/bin/bash name="lian hua" function rename {
echo "My original name is: " "${name}"
name="${1}"
echo "Now, My name is: " "${name}"
} function naming {
initName="lao wang"
} naming
echo ${initName} # 函数外可以使用函数内定义的变量 rename "shuai ge"
echo "once again, my name is: " "${name}" # 函数内可以修改函数外定义的全局变量 [root@demo bash]# ./function_variable.sh
lao wang
My original name is: lian hua
Now, My name is: shuai ge
once again, my name is: shuai ge [root@demo bash]# cat function_variable.sh
#!/bin/bash
function naming {
firstName="wang"
local lastName="lao"
} naming
echo ${firstName} set -eux
echo ${lastName}
set +eux [root@demo bash]# ./function_variable.sh
wang
./function_variable.sh: line 12: lastName: unbound variable # lastName 是局部变量,无法引用
入门 shell 从脚本开始 - lazy_find的更多相关文章
- shell script 脚本编程
介绍 Shell脚本编程30分钟入门 Shell 教程 Linux 的 Shell 种类众多,常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again ...
- shell及脚本4——shell script
一.格式 1.1 开头 必须以 "# !/bin/bash" 开头,告诉系统这是一个bash shell脚本.注意#与!中间有空格. 二.语法 2.1 数值运算 可以用decla ...
- 【Telnet】使用Telnet协议连接到远程Shell执行脚本
介绍 本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果: 相关文章: <[Jsch]使用SSH协议连接到远程Shell执行脚本>http://www.cnbl ...
- shell自动计算脚本
shell自动计算脚本 #!/bin/bash echo $(($)) [root@bogon ~]# sh b.sh 123+123246 let用户声明这个操作是要计算,后者的效率更高 (expr ...
- Shell菜单脚本
今天在这儿给大家分享一个我简单编写的Shell菜单脚本,傻瓜式的人机交互,人人都可以操作linux. #!/bin/sh #Shell菜单演示 function menu () { cat <& ...
- shell常见脚本30例
shell常见脚本30例 author:headsen chen 2017-10-19 10:12:12 本文原素材出自网上,特此申明.有些地方加入我自己的改动 常见的30例shell脚本 1.用 ...
- shell常用脚本
shell常用脚本 author:headsen chen 2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim name.grep.sh 2,cat ...
- 一篇关于Maven项目的jar包Shell启动脚本
使用Maven作为项目jar包依赖的管理,常常会遇到命令行启动,笔者也是哥菜鸟,在做微服务,以及服务器端开发的过程中,常常会遇到项目的启动需要使用main方法,笔者潜心的研究了很多博客,发现大多写的都 ...
- Linux shell编写脚本部署pxe网络装机
Linux shell编写脚本部署pxe网络装机 人工安装配置,Linux PXE无人值守网络装机 https://www.cnblogs.com/yuzly/p/10582254.html 脚本实 ...
- 使用shell解析脚本依赖关系,并自动补数
将脚本依赖关系放到表中 使用shell解析脚本依赖关系,递归的计算各个脚本. #!/bin/bash # dm 补数 basepath=$(cd ``; pwd) cd $basepath sourc ...
随机推荐
- Oracle-startup和shutdown
startup不同参数作用 startup nomount 非安装启动,以这种方式启动可执行: 1.重建控制文件. 2.重建数据库读取init.ora文件. 3.启动实例,即启动SGA和后台进程,需要 ...
- 华企盾DSC苹果Mac针对Word编辑文件不加密
解决方法:查看了客户端的控制台日志显示的进程名和加密后缀都正常,可能文件内部有rename的操作,加密所有类型解决
- Guava Cache 异步刷新技巧,你值得拥有!
Guava Cache是一款非常优秀的本地缓存框架,提供简洁易用的 API 供开发者使用. 这篇文章,我们聊聊如何使用 Guava Cache 异步刷新技巧带飞系统性能 . 1 基本用法 首先,在 J ...
- NoClassDefFoundError: javax/el/ELManager
Caused by: java.lang.NoClassDefFoundError: javax/el/ELManager at org.hibernate.validator.messageinte ...
- P5179 Fraction 题解
题目描述 给你四个正整数 \(a,\,b,\,c,\,d\) ,求一个最简分数 \(\frac{p}{q}\) 满足 \(\frac{a}{b} < \frac{p}{q} < \frac ...
- spring-cloud-alibaba项目打包
在父依赖中加入 <build> <plugins> <plugin> <groupId>org.springframework.boot</gro ...
- Cesium案例解析(八)——CesiumWidget简化窗体
目录 1. 正文 2. 参考 1. 正文 Cesium Widget这个案例展示了一个Cesium的简化窗体.在之前的案例中使用的都是Cesium.Viewer这个窗体组件,包含了非常丰富的组件内容. ...
- C++篇:第九章_字符串_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 九.字符串 可以用[ ]进行下标访问 使用string类需将头文件包含在程序中, ...
- GaussDB(for openGauss)让数据“存得下、算得快、算得准”
摘要:本文从总体架构.数据分布方式.计算下推.数据强一致等方面进行介绍GaussDB(for openGauss). 1.前言 随着云计算规模越来越大,企业业务数据量呈指数级增长,传统数据库在海量数据 ...
- 带你掌握二进制SCA检测工具的短板及应对措施
摘要:本文针对二进制SCA检测技术短板所面临的一些特殊场景.检测影响及应对措施进行详细分析和说明,希望对使用二进制SCA检测工具的测试和研发人员有所帮助. 本文分享自华为云社区<二进制SCA检测 ...