我有一个场景,我会得到以下格式的输出:
public class WinExitSignal : IExitSignal
{
public event EventHandler Exit;
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
/// <summary>
/// Need this as a member variable to avoid it being garbage collected.
/// </summary>
private HandlerRoutine m_hr;
public WinExitSignal()
{
m_hr = new HandlerRoutine(ConsoleCtrlCheck);
SetConsoleCtrlHandler(m_hr, true);
}
/// <summary>
/// Handle the ctrl types
/// </summary>
/// <param name="ctrlType"></param>
/// <returns></returns>
private bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
case CtrlTypes.CTRL_BREAK_EVENT:
case CtrlTypes.CTRL_CLOSE_EVENT:
case CtrlTypes.CTRL_LOGOFF_EVENT:
case CtrlTypes.CTRL_SHUTDOWN_EVENT:
if (Exit != null)
{
Exit(this, EventArgs.Empty);
}
break;
default:
break;
}
return true;
}
}
我能够将整个结果存储到bash变量中。
例如,我创建了一个bash变量1,2,3,4,5,6||7,8,9,10,11,12||13,14,15,16,17,18||19,20,21,22,23,24
和其他6个名为myvar
的变量。 a,b,c,d,e,f
包含上述结果集。
我需要的是当我在第一个循环中循环myvar时,它应分配如下:
myvar
准备一个html正文并发送给它 并在第二个循环中
a=1,b=2,c=3,d=4,e=5,f=6
再次准备一个html正文并通过
发送我对循环部分感到震惊
然后做一些html电子邮件正文,我只能返回
时才能这样做1,2,3,4,5,6-
如果提供的信息足以让您回答这个问题,请告诉我。
答案 0 :(得分:1)
您可以通过设置IFS='|'
来拆分输入行,但遗憾的是您使用||
作为分隔符。 This answer显示了使用和IFS=''
执行第二个数组参数扩展的技巧。在您的情况下,这相当于:
#!/bin/bash
# '(' execute the following commands in a subshell, see `man bash` (search for
# "Compound Commands").
(
# disable filename expansion
set -f
input="1,2,3,4,5,6||7,8,9,10,11,12||13,14,15,16,17,18||19,20,21,22,23,24"
# split by '|'
IFS='|'
lines=(${input})
# remove empty elements
IFS=""
lines=(${lines[@]})
for ((i=0; i < ${#lines[@]}; i++)); do
echo "Processing ${i}: '${lines[${i}]}'"
IFS=','
elements=(${lines[${i}]})
for ((j=0; j < ${#elements[@]}; j++)); do
echo " found element ${elements[${j}]}"
done
# here you can either use ${elements[${0}]}, ..., ${elements[${5}]} directly
# or you can assign them to your variables:
# set a="${elements[${0}]
# set b="${elements[${1}]
# ...
done
)
<强>输出强>
% ./foo.sh
Processing 0: '1,2,3,4,5,6'
found element 1
found element 2
found element 3
found element 4
found element 5
found element 6
Processing 1: '7,8,9,10,11,12'
found element 7
found element 8
found element 9
found element 10
found element 11
found element 12
Processing 2: '13,14,15,16,17,18'
found element 13
found element 14
found element 15
found element 16
found element 17
found element 18
Processing 3: '19,20,21,22,23,24'
found element 19
found element 20
found element 21
found element 22
found element 23
found element 24
<强>参考强>
答案 1 :(得分:0)
这只是您可以扩展的示例
下面你可以使用$ 1,$ 2,$ 3,$ 4等进行进一步处理(比如分配变量a,b c ..)或直接使用$ variables等。
x="1,2,3,4,5,6||7,8,9,10,11,12||13,14,15,16,17,18||19,20,21,22,23,24"
echo $x | cut -d'|' -f1- --output-delimiter=$'\n' | sed -e '/^[ ]*$/d' > /tmp/data.txt
for line in `cat /tmp/data.txt`; do awk -F',' '{print $1, $2, $3, $4, $5}' <<< $line; done
请注意cat /tmp/data.txt