随笔分类 -  Bash

摘要:function中的变量默认是global,必须显式加关键字 local 才能定义为局部变量 #!/bin/bash function f1() { b=88888 } function f2() { echo $b } f1 f2 #!/bin/bash function f1() { local 阅读全文
posted @ 2020-12-10 11:05 ascertain 阅读(303) 评论(0) 推荐(0) 编辑
摘要:要与Linux交互,脚本获取键盘输入的结果是必不可少的,read可以读取键盘输入的字符。 read [-rs] [-a ARRAY] [-d delim] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [var_name1 var_name2 .. 阅读全文
posted @ 2020-12-09 18:18 ascertain 阅读(637) 评论(0) 推荐(0) 编辑
摘要:: 是shell 内建命令,等同于 true type -at : 返回值 为 NULL , exit code 为 0 作为分隔符PATH /etc/passwd 清空文本内容 :>file 原理为先运行 :,返回值为NULL,redirect至 file等同于 true>file >file 占 阅读全文
posted @ 2020-12-09 11:47 ascertain 阅读(97) 评论(0) 推荐(0) 编辑
摘要:printf %s作为字符串打印时, %4.2s "."前面表示宽度,后面表示显示几个字符 阅读全文
posted @ 2020-12-06 12:25 ascertain 阅读(83) 评论(0) 推荐(0) 编辑
摘要:Usage函数 function Usage() { cat <<-delimiter Usage: command [-x] [-v] [-z] [files] A short explanation of the operation goese here It might be a few li 阅读全文
posted @ 2020-12-02 15:24 ascertain 阅读(99) 评论(0) 推荐(0) 编辑
摘要:$ echo $- himBH 1、himBH这个结果又是什么意思? 2、看不懂,求第69行详解。多谢++ for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" els 阅读全文
posted @ 2020-12-01 14:59 ascertain 阅读(717) 评论(0) 推荐(0) 编辑
摘要:1、bash的POSIX标准 在一般的linux系统当中(如redhat), 使用sh调用执行脚本相当于打开了bash的POSIX标准模式 (等效于bash的 --posix 参数) 一般的,sh是bash的“子集” (不是子集的部分,具体区别见下的“Things sh has that bash 阅读全文
posted @ 2020-12-01 14:46 ascertain 阅读(435) 评论(0) 推荐(0) 编辑
摘要:#!/bin/sh echo "Num of arguments: $#" echo "Current PID: $$" echo "BASHPID: $BASHPID" sleep 10 & echo "The last deforegroud PID: $!" echo "The current 阅读全文
posted @ 2020-11-02 11:38 ascertain 阅读(73) 评论(0) 推荐(0) 编辑
摘要:#!/bin/bash echo $BASHPID for _ in `seq 100`;do echo start sleep 5 echo $BASH_SUBSHELL echo stop done & sleep 20 7267是脚本进程,其又开启了两个进程7268 7269,7268是for 阅读全文
posted @ 2020-10-29 12:06 ascertain 阅读(75) 评论(0) 推荐(0) 编辑
摘要:shell中if while等后必须接命令,利用命令的exit code作为判断,而其他高级语言是利用return值作为判断对于非命令变量,可以利用test or [ ] or [[ ]] 进行判断如果确实需要用命令或函数的返回值作为判断,可以如下 function p(){ echo $$ exi 阅读全文
posted @ 2020-10-25 12:54 ascertain 阅读(186) 评论(0) 推荐(0) 编辑
摘要:shell中true & false是两个命令,其返回值为NULL,true 的 exit code为0,false 的exit code 为 1 #!/bin/env sh if [[ `$1` ]];then echo true else echo false fi 把false作为命令执行,其 阅读全文
posted @ 2020-10-23 11:51 ascertain 阅读(5487) 评论(0) 推荐(0) 编辑
摘要:删除从左至右删除第一个匹配的字符串 ${var/str} 删除全部匹配的str ${var//str} 替换从左至右替换第一个匹配的str1 ${var/str1/str2} 替换全部str1为str2 ${var//str1/str2} 阅读全文
posted @ 2020-10-13 17:36 ascertain 阅读(103) 评论(0) 推荐(0) 编辑
摘要:#!/bin/env sh while [ true ];do count=$(ps -ef|grep $1|grep -v grep|grep -v $1) if [[ -z $count ]];then echo failed sh $1 else echo success fi sleep 2 阅读全文
posted @ 2020-10-10 20:48 ascertain 阅读(299) 评论(0) 推荐(0) 编辑
摘要:return 返回的是状态码, return不返回函数返回值 可以在前面用 echo 返回函数返回值,return 返回指定函数退出状态码 echo 返回的是函数返回值,如果没有使用return,则函数退出状态码是函数最后一条命令的退出状态码 return后面的语句将不再执行 #!/bin/bash 阅读全文
posted @ 2020-10-09 20:58 ascertain 阅读(1204) 评论(0) 推荐(0) 编辑
摘要:#!/bin/env sh account_list=('chenwk' 'chenw' 'sales' 'data' 'personnel' 'operation' 'tech' 'dinghh') mail_list=('chenwk@qq.com.com' 'chenw@qq.com.com' 阅读全文
posted @ 2020-09-28 16:27 ascertain 阅读(0) 评论(0) 推荐(0) 编辑
摘要:用getopts命令获取到脚本选项后,getopts会将获取到的选项所对应的参数(选项对应的参数,并不是选项本身,有的命令选项后面是需要跟一个参数的,例如tcpdump 的-w选项,后面需要指定一个文件来保存抓包数据)自动保存到OPTARG这个变量中。 getopts命令格式:getopts OPT 阅读全文
posted @ 2020-09-28 14:24 ascertain 阅读(169) 评论(0) 推荐(0) 编辑
摘要:bash有三种输入重定向 < : 用于文件 << : here-documents <<< : here-strings here-documents: 语法格式: command <<[-]MARKER Here Document MARKER 这个操作符指示bash从标准输入读取输入内容,知道读 阅读全文
posted @ 2020-09-25 18:44 ascertain 阅读(506) 评论(0) 推荐(0) 编辑
摘要:$# 传递到脚本的参数个数 $$ 脚本运行的当前进程PID $! 最后一个运行的后台进程PID $- 当前Shell选项 #!/bin/sh echo "Num of arguments: $#" echo "Current PID: $$" echo "The last deforegroud P 阅读全文
posted @ 2020-09-24 19:34 ascertain 阅读(126) 评论(0) 推荐(0) 编辑
摘要:引用数组中所有元素时${arr[*]}和${arr[@]}是有细微区别的 Example: #!/bin/sh function showarr(){ arr=$1 for b in ${arr[*]};do echo $b done return 0 } regions=('aa pp' 'bb' 阅读全文
posted @ 2020-09-24 14:43 ascertain 阅读(3715) 评论(0) 推荐(0) 编辑
摘要:C语言中我们经常使用DEBUG宏来控制是否输出调试信息,在shell脚本中,同样可以使用相同机制 if [[ "$DEBUG" == true ]];then echo 'DEBUGGING' fi 这样的代码块称之为 "调试钩子" 或 "调试块",在调试钩子内部可以输出调试信息,使用调试钩子的好处 阅读全文
posted @ 2020-09-22 18:27 ascertain 阅读(297) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示