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

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

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

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

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

如果您运行此脚本

bash shelveinterrupt.sh

,它将结束于

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取消搁置--继续”)

答案1

事实证明我想做的事情可以通过 Tcl 扩展来完成,期待。然而,这需要两个脚本。

一个 shell 脚本。

#################################
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

还有一个 Expect 脚本。

#################################
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 .

这会产生以下输出

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 .也行不通。

相关内容