从标准输入流中读取并执行shell指定函数
巧妙的ohmytmux配置
看oh my tmux的配置,发现他们很巧妙的将配置和shell函数放到一个文件里
比如切换鼠标模式的相关配置和shell函数,
# : << EOF
# ...省略其他配置文件信息
# toggle mouse
bind m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
# EOF
#
# _toggle_mouse() {
# old=$(tmux show -gv mouse)
# new=""
#
# if [ "$old" = "on" ]; then
# new="off"
# else
# new="on"
# fi
#
# tmux set -g mouse $new
# }
#
# "$@"
主要功能时 将m键绑定上 cut -c3- ~/.tmux.conf | sh -s _toggle_mouse
cut命令切换注释
第一个巧妙的地方时 cut -c3- ~/.tmux.conf ,此命令运行的结果是
: << EOF
...省略其他配置文件信息
toggle mouse
nd m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
EOF _toggle_mouse() {
old=$(tmux show -gv mouse)
new="" if [ "$old" = "on" ]; then
new="off"
else
new="on"
fi tmux set -g mouse $new
} "$@"
一个完美的脚本就出现了!将每一行的前两个字符去掉,
- 使用 '<<EOF' 和 'EOF' 完美的将原来的配置放到多行注释里,
- 将原来使用 '# ' 注释掉的shell函数 打开
sh执行命令
从sh的文档中可以看到 -s 使用执行标准输入里执行命令
OPTIONS
-s Read commands from the standard input.
STDIN
The standard input shall be used only if one of the following is true: * The -s option is specified. * The -c option is not specified and no operands are specified. * The script executes one or more commands that require input from standard input (such as a read command that does not redirect its input). See the INPUT FILES section. When the shell is using standard input and it invokes a command that also uses standard input, the shell shall ensure that the standard input file pointer
points directly after the command it has read when the command begins execution. It shall not read ahead in such a manner that any characters intended to be
read by the invoked command are consumed by the shell (whether interpreted by the shell or not) or that characters that are not read by the invoked command are
not seen by the shell. When the command expecting to read standard input is started asynchronously by an interactive shell, it is unspecified whether charac‐
ters are read by the command or interpreted by the shell. If the standard input to sh is a FIFO or terminal device and is set to non-blocking reads, then sh shall enable blocking reads on standard input. This shall
remain in effect when the command completes.
执行指定函数
从sh的man文档里没有看出来可以指定执行脚本里的哪个函数呢,ohmytmux怎么让sh知道执行哪个函数的呢?
注意到cut后的脚本没,最后一行是 "$@"
在shell中$@用来获得脚本的所有入参,所以 cut -c3- ~/.tmux.conf | sh -s _toggle_mouse 执行的真正脚本时
: << EOF
...省略其他配置文件信息
toggle mouse
nd m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
EOF _toggle_mouse() {
old=$(tmux show -gv mouse)
new="" if [ "$old" = "on" ]; then
new="off"
else
new="on"
fi tmux set -g mouse $new
} _toggle_mouse
这样是不是很明了了,
脚本第一行的 ':' 是shell中的空命令占位符,它什么也不干,永远返回0
<<EOF 到 EOF定义了一个多行文本,这个多行文本没有人用, 相当于把原来的配置都注释掉了
接着 定义一个名字为_toggle_mouse的函数
最后一行 执行该行数
这就是另一个巧妙的地方, 将要执行的函数名当做参数传给shell,并通过$@将 指定的函数 放到最后一样
通过这种方式我们就可以方便的执行shell中指定的函数了
参考
oh my tmux github : https://github.com/gpakosz/.tmux
从标准输入流中读取并执行shell指定函数的更多相关文章
- 也谈matlab中读取视频的一个重要函数mmreader
也谈matlab中读取视频的一个重要函数mmreader 在matlab中输入help mmreader来查阅一下该函数,有如下信息: MMREADER Create a multimedia rea ...
- 从hivesql结果中读取数值到shell变量的方法
为了检查hive表中的数据,并统计展现,需要将查出的结果传入到shell变量,然后统一输出到文本. 最后使用了以下两个方法: 方法一 QUAN=$(hive -S -e "select co ...
- expect中使用exec执行shell命令
今天想在expect脚本中获取本机ip,执行脚本是报错,脚本如下: set localip [exec ifconfig eth0 | grep Mask | cut -d: -f2 | awk '{ ...
- python从excel中读取数据传给其他函数使用
首先安装xlrd库 pip install xlrd 方法1: 表格内容如下: 场景描述,读取该表格A列数据,然后打印出数据 代码何解析如下: import xlrd #引入xlrd库 def exc ...
- Java实践 — SSH远程执行Shell脚本(转)
原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介 SSH是Secure Shell的缩写,一 ...
- Java实践 — SSH远程执行Shell脚本
1. SSH简介 SSH是Secure Shell的缩写,一种建立在应用层和传输层基础上的安全协议.SSH在连接和传送过程中会加密所有数据,可以用来在不同系统或者服务器之间进行安全连接 ...
- Jmeter 5.1 从excel读取数据执行接口自动化
思路:数据在excel文件中进行维护,然后转换成csv格式,jme中读取数据执行: 1.将接口各数据在excel文件中进行维护,然后存为csv格式,我的数据如下: 2.jmeter脚本,配置csv文件 ...
- vim中执行shell命令小结
vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
随机推荐
- 纯css模拟电子钟
先看效果 演示地址: https://yueminhu.github.io/di...点击左边拉环切换夜间模式. 用到了伪元素生成数字的小三角`currentColor和color: inherit` ...
- 摩拜单车微信小程序开发技术总结
前言 摩拜单车小程序已于微信小程序上线第一天正式发布,刷爆微博媒体朋友圈.本文主要讲讲技术方向的总结,在段时间的开发周期内内如何一步步从学习到进阶. 思维转变 微信小程序没有HTML的常用标签,而是类 ...
- 93. 复原 IP 地址
做题思路or感想 这种字符串切割的问题都可以用回溯法来解决 递归三部曲: 递归参数 因为要切割字符串,所以要用一个startIndex来控制子串的开头位置,即是会切割出一个范围是[startIndex ...
- MFC软件国际化的几个问题及其解决方案
作者:马健 邮箱:stronghorse_mj@hotmail.com主页:https://www.cnblogs.com/stronghorse/ 以前我以为PDG相关软件只会在国内流行,所以发行简 ...
- CPUs Intel 925X/915 Chipset (925X主板芯片组)
这个是2004年的intel产品的设计(主板,主板芯片组,北桥,南桥),结构也比较清晰,主要想看南桥和北桥的设计. 一些英文解释 ECC是一种能够实现"错误检查和纠正"的技术D92 ...
- 【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
前言:随着工业化的发展,目前越来越多的开发,从互联网走向传统行业.其中,工业领域也是其中之一,包括各大厂也都在陆陆续续加入工业4.0的进程当中. 工业领域,最核心的基础设施,应该是与下位硬件设备或程序 ...
- Java学习day35(《大道至简》读后感)
对于大一之前的我来说,并不明白计算机这个专业要做的是什么,在我的眼中敲敲键盘打打字就是这个专业的全部:对于现在的我而言,这样的想法显然是十分幼稚的. 当初高考完填报志愿时选择了这门专业,也并不是因为自 ...
- Intellij IDEA 2022 正式发布,这些功能真不错
Intellij IDEA 2022 正式发布了,作为正版用户,胖哥赶紧更新了一波,好家伙!这几个功能确实很香啊.新版更新的东西真不少,不愧是一个大版本更新. 依赖分析 IDEA的依赖检查.依赖冲突解 ...
- java高级用法之:JNA类型映射应该注意的问题
目录 简介 String Buffers,Memory,数组和Pointer 可变参数 总结 简介 JNA提供JAVA类型和native类型的映射关系,但是这一种映射关系只是一个大概的映射,我们在实际 ...
- docker基础_数据卷
docker数据卷 为什么要使用数据卷 如果数据都在容器中,那么容器一旦删除,数据就会丢失!docker容器需要将产生的数据同步到本地.容器与容器之间也需要有一个数据共享的技术 将某些文件共享.这就是 ...