- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有 3 个进程在 Linux/Shell 终端中排队,使用 && 分隔各个进程的命令,如下所示:
<command1> && <command3> && <command4>
现在正在执行 process1 的命令,我想编辑队列:
例如1:我想在 process1 和 process3 之间有另一个 process2。这样更新后的命令队列就变成了:
<command1> && <command2> && <command3> && <command4>
例如2:或者可能会删除一个过程,例如队列中的 process3。这样更新后的命令队列就变成了:
<command1> && <command4>
有没有办法即时编辑命令队列?即 process1 何时已被执行?
最佳答案
你说的是command list在 bash
(不是队列)中,一旦 shell 解析它,就不能修改它。
从问题中不清楚你是否真的需要队列,或者简单的退出状态测试将为你服务,例如:
#!/bin/bash
if cmd1; then
cmd2
else
if cmd3; then
cmd4
fi
fi
(这将首先运行 cmd1
,然后,根据它的成功,运行 cmd2
如果成功,或者尝试运行 cmd3
,然后使用 cmd4
。最后一个 if block 可以简化为 cmd3 && cmd4
。)
但是,如果您真的需要命令队列,则必须自己实现一个。
第一个想法是将队列存储在一个 shell 数组变量中,如下所示:
#!/bin/bash
declare -a queue
clear() { queue=(); }
is_empty() (( ! ${#queue[@]} ))
# insert an item at the beginning
insert() {
queue=("$1" "${queue[@]}")
}
# append an item at the end
push() {
queue=("${queue[@]}" "$1")
}
# remove an item from the beginning
pop() {
queue=("${queue[@]:1}")
}
# return the first item, without popping it
peek() {
echo "${queue[0]}"
}
然后用它来运行这样的命令:
# run commands from the `queue`
run() {
while ! is_empty; do
local cmd=$(peek)
pop
eval "$cmd" || return
done
return 0
}
# run: echo before && sleep 1 && echo after
clear
push 'echo before'
push 'sleep 1'
push 'echo after'
run
但是这种方法的主要问题是您不能真正异步地修改这个队列。您可以在运行之前修改它,或者在 run
循环 中运行时修改它,但这可能不是您所要求的。
您可能会想,为什么我们不能使用 run &
在后台执行命令。好吧,我们可以,但是后台子进程(子 shell )将收到它自己的 queue
变量副本,并且您在生成后所做的修改不会反射(reflect)在 run
子流程。
另一种方法是通过每行存储一个命令来在文件中实现队列。这实际上可行,但我们还需要确保某些互斥机制就位(例如 flock
)。
虽然这种方法有效,但我不喜欢每次需要在位置 0 插入命令时重写整个文件的想法。不过,您可以通过在 /上创建文件来将文件保存在内存中dev/shm
(ramdisk),但这在 MacOS 上不起作用。
前一种方法的逻辑扩展是使用一个实际的共享内存队列,就像 Redis 提供的队列一样。 .
此基于 Redis 的队列的bash
包装器将是:
#!/bin/bash
redis=(redis-cli --raw)
clear() {
"${redis[@]}" DEL queue &>/dev/null
}
is_empty() (( $("${redis[@]}" LLEN queue) == 0 ))
# insert the item at the beginning
insert() {
"${redis[@]}" LPUSH queue "$1" &>/dev/null
}
# append the item at the end
push() {
"${redis[@]}" RPUSH queue "$1" &>/dev/null
}
# remove the item from the beginning
pop() {
"${redis[@]}" LPOP queue
}
peek() {
"${redis[@]}" LRANGE queue 0 0
}
show() {
"${redis[@]}" LRANGE queue 0 -1
}
您可以运行您的第一个示例(在运行前一个命令时插入),如下所示:
# run commands from the redis queue
run() {
while ! is_empty; do
eval "$(pop)" || return
done
return 0
}
# start with: echo before && sleep 1 && echo after
clear
push 'echo before'
push 'sleep 3'
push 'echo after'
run &
# but then, 1sec after running, modify the queue, insert another command
sleep 1
insert 'echo inserted'
wait
示例输出:
$ ./redis-queue-demo.sh
before
inserted
after
因此,使用 Redis 方法,您的第一个示例(插入命令)将如下所示:
clear
push 'echo command1; sleep 2'
push 'echo command3'
push 'echo command4'
run &
sleep 1
insert 'echo command2'
wait
输出:
command1
command2
command3
command4
第二个例子(删除命令):
clear
push 'echo command1; sleep 2'
push 'echo command3'
push 'echo command4'
run &
sleep 1
pop >/dev/null
wait
输出:
command1
command4
关于linux - 如何在 Linux/bash 中使用 && 编辑排队的进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586533/
我添加了编辑按钮 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButto
我试图在运行时“干净地”更改 UIBarButtonItem 文本,以便可以切换编辑/完成模式。然而,每次我在运行时更改 title 属性时,动画看起来都很笨拙。我正在寻找模拟联系人应用程序中“编辑/
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想更改 INI 文件中的一些值。不幸的是,我在 2 个不同的部分有键,它们共享相同的名称但需要不同的值。我的代码使用 Get-IniContent函数来自 PsIni . 示例 INI 文件: [P
是否有通知或委托(delegate)方法可用于检测表格 View 何时进入编辑状态? 我想要做的是检测表正在编辑,然后显示一个额外的行,上面写着“添加新项目”或类似的东西。 我尝试在加载 View C
例如,我试图从 svg 读取样式块,我可以获取类型但不能获取字符串。 $svgTemplate = new SimpleXMLElement($_POST['SvgTemplateImport']);
我可以使用 self.navigationItem.leftBarButtonItem = self.editButtonItem; 通过按下导航面板上的编辑按钮让 UITableViewContro
我正在使用markitup!作为 Markdown 编辑器( example )。 目前,我需要按预览按钮(绿色勾号)来显示预览面板。 我希望自动显示预览 - 我怎样才能实现这一点? 最佳答案 我没有
我的处境非常糟糕。我丢失了源代码,客户需要在应用程序中进行一些更改。想想一个编辑程序集的例子:Test.dll,然后添加代码行,最后重新编译它 所以我的问题是: -可以这样做吗? -如果可能的话,什么
我使用了一些 JavaScript 来通过按钮更改段落元素的内容。它工作正常,但我还想让按钮控制标题和附图。给我指明正确的方向吗? 这是我用来更改段落的代码 .... 谢谢! 最佳答案 尝试将 Ja
是否有任何 Emacs lisp 插件可以让我轻松地在 yaml 文件中编辑或输入数据。 例如: --- sample yaml file ---Name : Addr :City :State:Zi
新手Java问题,我确定已经解决了,但是在任何地方都找不到解决方案:( 我想使用这里包含的java程序http://sourceforge.net/projects/ant-tibco/files/
在我的网页中,我使用了 gridview。在这个 GridView 中,它显示了一组用户信息。我刚刚从智能标签菜单中添加了一个按钮。我的要求是当我点击每个用户对应的按钮时,它会重定向到另一个页面并显示
我想在没有任何框架的情况下直接在 JS 中编辑一个 SVG 文件。 基本上我有一个 SVG 主文件,其中应该包含一些子 SVG。 我已经在 Ajax 中检索了这些子项的内容,但我想将它们插入到 SVG
我有我的 ViewModel,我有我的 Controller 可以从 ViewModel 正确显示,但是我不确定如何使 ViewModel 可编辑,以便将编辑后的数据发送回模型。我只想编辑 Order
我不确定我的做法是否正确。 IplImage *dog_1 = cvCreateImage(cvGetSize(oriImg), oriImg->depth, oriImg->nChannels);
我有一个创建二维码的网络服务器。在此过程中,我得到一个 BarcodeQRCode 对象,我可以从中获取图像 (.getImage())。 我不确定如何将这张图片发回给客户。我不想将它保存在文件中,而
已编辑:我的第一个问题解决了,但又出现了另一个问题,只提供了一个用户 ID。这是修改后的代码的屏幕截图。 回到表格,用户将按下编辑按钮,这样他就可以编辑问题并给出适当的操作.. 我的上表代码是这样的:
据我了解,我无法通过重新启动服务器来清除 MySQL 查询缓存。 每次运行 sql 时,我都试图获得与第一个代码块类似的结果 1-这是在重新启动 Apache 和 MySQL 之前(第一次使用这些查询
我正在创建一个页面来搜索项目,然后能够编辑/更新它。当它只返回一个结果时我能够做到这一点,但当它给我多个结果时我只能编辑最后一项。下面是我的代码: ....... $dj =$_POST[djnum]
我是一名优秀的程序员,十分优秀!