首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Ctrl-C传递给shell脚本中的提示符

将Ctrl-C传递给shell脚本中的提示符
EN

Unix & Linux用户
提问于 2021-04-28 11:30:49
回答 1查看 204关注 0票数 1

我正在尝试编写一个shell脚本来重现我在使用Mercurial的搁置扩展时遇到的情况。

这需要中断Mercurial的命令行用户界面(UI)提供的提示。请参见以下脚本

代码语言:javascript
复制
rm -rf test-shelveinterrupt
hg init test-shelveinterrupt
cd test-shelveinterrupt

hg branch foo
echo "First line of foo" >> foo
hg add foo
hg ci -m "First line of foo" foo
echo "Second line of foo" >> foo
hg shelve

hg up null

hg branch bar
echo "First line of bar" >> bar
hg add bar
hg ci -m "First line of bar" bar

hg unshelve

pkill -INT shelveinterrupt

如果您将此脚本作为

代码语言:javascript
复制
bash shelveinterrupt.sh

,它会以

代码语言:javascript
复制
file 'foo' was deleted in local [working-copy] but was modified in other [shelve].
You can use (c)hanged version, leave (d)eleted, or leave (u)nresolved.
What do you want to do? 

它是脚本末尾的hg unshelve命令的响应。

我想打断一下这个提示。最后一个pkill的目的是中断shell进程,但当然不会在脚本等待输入时调用它。有没有一种不创建第二个脚本来调用第一个脚本的方法来调用这个脚本?

请注意,在中断脚本之后,您将得到

未解决的冲突(请参阅“hg解决”,然后“hg取消搁置-继续”)

EN

回答 1

Unix & Linux用户

发布于 2021-05-07 13:01:15

原来我想要做的事情可以用Tcl扩展来完成,expect。但是,这需要两个脚本。

一个外壳脚本。

代码语言:javascript
复制
#################################
shelveinterrupt.sh
#################################
#!/bin/bash

rm -rf test-shelveinterrupt
hg init test-shelveinterrupt
cd test-shelveinterrupt
hg branch foo
echo "First line of foo" >> foo
hg add foo
hg ci -m "First line of foo" foo

echo "Second line of foo" >> foo
hg shelve

hg up null

hg branch bar
echo "First line of bar" >> bar
hg add bar
hg ci -m "First line of bar" bar

hg log -vG

hg branch

hg unshelve

还有一个期待的剧本。

代码语言:javascript
复制
#################################
shelveinterrupt.exp
#################################
#!/usr/bin/expect -f

spawn ./shelveinterrupt.sh
expect "What do you want to do?"
send -- "^C"
expect eof

# Check `hg status
cd test-shelveinterrupt
set hgst [exec hg status]
puts $hgst
exec hg update .

这将产生以下输出

代码语言:javascript
复制
faheem@orwell:~/test-mercurial$ ./shelveinterrupt.exp
spawn ./shelveinterrupt.sh
marked working directory as branch foo
(branches are permanent and global, did you want a bookmark?)
shelved as foo
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
0 files updated, 0 files merged, 1 files removed, 0 files unresolved
marked working directory as branch bar
unshelving change 'foo'
rebasing shelved changes
file 'foo' was deleted in local [working-copy] but was modified in other [shelve].
You can use (c)hanged version, leave (d)eleted, or leave (u)nresolved.
What do you want to do? interrupted!
# The repository is in an unfinished *update* state.

# Unresolved merge conflicts:
#
#     foo
#
# To mark files as resolved:  hg resolve --mark FILE

# To continue:    hg update .

abort: outstanding merge conflicts
(use 'hg resolve' to resolve)
    while executing
"exec hg update ."
    (file "./shelveinterrupt.exp" line 11)

所以hg update .也不起作用。

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/647129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档