bash:将stdout捕获到一个变量,但仍然显示在控制台中(Capture stdout to a variable but still display it in the console)
我有一个bash脚本调用几个长时间运行的进程。 为了处理原因,我想将这些调用的输出捕获到变量中。 然而,因为这些是长时间运行的进程,我希望rsync调用的输出在控制台中实时显示,而不是事后。
为此,我找到了一种方法,但它依赖于将文本输出到/ dev / stderr。 我觉得输出到/ dev / stderr不是一个很好的做事方式。
VAR1=$(for i in {1..5}; do sleep 1; echo $i; done | tee /dev/stderr) VAR2=$(rsync -r -t --out-format='%n%L' --delete -s /path/source1/ /path/target1 | tee /dev/stderr) VAR3=$(rsync -r -t --out-format='%n%L' --delete -s /path/source2/ /path/target2 | tee /dev/stderr)
在上面的例子中,我正在调用rsync几次,我想在处理文件名时看到文件名,但最后我仍然希望输出一个变量,因为我稍后将会解析。
有没有一个“更清洁”的方式来实现这一点?
如果它有所作为,我正在使用Ubuntu 12.04,bash 4.2.24。
I have a bash script which calls several long-running processes. I want to capture the output of those calls into variables for processing reasons. However, because these are long running processes, I would like the output of the rsync calls to be displayed in the console in real-time and not after the fact.
To this end, I have found a way of doing it but it relies on outputting the text to /dev/stderr. I feel that outputting to /dev/stderr is not a good way of doing things.
VAR1=$(for i in {1..5}; do sleep 1; echo $i; done | tee /dev/stderr) VAR2=$(rsync -r -t --out-format='%n%L' --delete -s /path/source1/ /path/target1 | tee /dev/stderr) VAR3=$(rsync -r -t --out-format='%n%L' --delete -s /path/source2/ /path/target2 | tee /dev/stderr)
In the example above, I am calling rsync a few times and I want to see the file names as they are processed, but in the end I still want the output in a variable because I will be parsing it later.
Is there a 'cleaner' way of accomplishing this?
If it makes a difference, I am using Ubuntu 12.04, bash 4.2.24.
原文:https://stackoverflow.com/questions/12451278
最满意答案
这是错误的事件。
CellValidating
用于确定单元格的内容是否有效。CellValidated
或CellLeave
更适合您的情况。That's the wrong event.
CellValidating
is for determining whether the contents of the cell is valid or not.CellValidated
orCellLeave
would be more suited to your situation.
相关问答
更多-
在更改CurrentCell之前,CellValidating不会被调用。 所以我对这个问题的看法是改变CurrentCell,然后切换回当前的。 protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Enter) { DataGridViewCell currentCell = CurrentCell; EndE ...
-
在验证事件之后需要更改单元格,因此请尝试使用CellValidated事件: void dgv_CellValidated(object sender, DataGridViewCellEventArgs e) { string cellValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString(); if (cellValue.Contains('G') || cellValue.Contains('g')) ...
-
这是错误的事件。 CellValidating用于确定单元格的内容是否有效。 CellValidated或CellLeave更适合您的情况。 That's the wrong event. CellValidating is for determining whether the contents of the cell is valid or not. CellValidated or CellLeave would be more suited to your situation.
-
CellValidating / RowValidating事件取决于焦点 。 备注当您使用键盘(TAB,SHIFT + TAB等)更改焦点时,通过调用Selector SelectNextControl方法或将ContainerControl.ActiveControl属性设置为当前窗体,焦点事件按以下顺序发生: 输入 的GotFocus 离开 证实 验证 引发LostFocus 请参阅: https : //msdn.microsoft.com/en-us/library/system.windows. ...
-
我使用一个简单的解决方法解决了这个问题。 详细网格CellValidating ,当输入无效时,我执行以下操作: e.Cancel = true; MessageBox.Show("message to inform user"); 用户将单击“确定”关闭消息框,这足以将焦点保持在详细网格中的无效单元格上。 我希望这会有助于他人。 I have solved the problem using a simple workaround. In detail grid CellValidating, when ...
-
把事情简单化! private void ItemsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 2) { double result; if (!double.TryParse(e.FormattedValue.ToString(), out result)) { ...
-
这样的事情,假设不必检查所选行的空格:请注意,代码首先标识要删除的行,然后以相反的顺序删除它们,以避免在循环期间更改行数的任何问题。 if (dgv.CurrentCell == null) return; var spaces = new string(' ', 5); var start = dgv.CurrentCell.RowIndex; if (start == dgv.Rows.Count) return; List
rowsToDelete = new List { ... -
你可以尝试这样的另一个aproach: private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { newvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value; } private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEv ...
-
尝试这个: if (string.IsNullOrEmpty(e.FormattedValue.ToString())) { dataGridView1.CancelEdit(); } Try this: if (string.IsNullOrEmpty(e.FormattedValue.ToString())) { dataGridView1.CancelEdit(); }
-
如何手动触发Datagridview.cellValidating?(How to Trigger Datagridview.cellValidating manually?)[2021-09-03]
您必须更改当前单元格,然后返回到它。 试一试。 您可能必须更改它,因为它尚未经过测试。 BeginEdit(); DataGridViewCell currentCell = GrdDetails.CurrentRow.Cells("Prod_code"); EndEdit(); CurrentCell = null; CurrentCell = currentCell; You have to change the current cell and then go back to it. Gi ...