增加历史记录行中的单个数字

时间:2013-06-04 20:59:44

标签: bash awk sed md5

所以我很难搞清楚这一点。

我要做的是显示最近输入的命令

我们以此为例:

MD5=$(cat $DICT | head -1 | tail -1 | md5sum)

此命令刚刚执行。它包含在shell脚本中。 执行后,在if..then..else ..语句中检查输出。 如果条件满足,我希望它运行上面的命令,除非我希望它每次运行时都加1。 例如:

MD5=$(cat $DICT | head -1 | tail -1 | md5sum)

if test ! $MD5=$HASH  #$HASH is a user defined MD5Hash, it is checking if $MD5 does NOT equal the user's $HASH
  then  #one liner to display the history, to display the most recent
    "MD5=$(cat $DICT | head -1 | tail -1 | md5sum)" #pipe it to remove the column count, then increment the "head -1" to "head -2"
  else echo "The hash is the same."
fi  #I also need this if..then..else statement to run, until the "else" condition is met.

任何人都可以帮忙,谢谢你。我有一个大脑放屁。 我在想使用sed或awk来增加。 grep显示最新的命令,

所以说:

$ history 3

输出:

1 MD5=$(cat $DICT | head -1 | tail -1 | md5sum)
2 test ! $MD5=$HASH 
3 history 3

-

$ history 3 | grep MD5

输出:

1 MD5=$(cat $DICT | head -1 | tail -1 | md5sum)

现在我希望它删除1,并在head的值中添加1,然后重新运行该命令。然后通过if..then..else测试发回该命令。

1 个答案:

答案 0 :(得分:1)

<强>已更新

如果我理解你的问题,这可以是一个解决方案:

# Setup test environment
DICT=infile
cat >"$DICT" <<XXX
Kraftwerk
King Crimson
Solaris
After Cyring
XXX

HASH=$(md5sum <<<"After Cyring")

# Process input file and look for match
while read line; do
  md5=$(md5sum<<<"$line")
  ((++count))
  [ "$HASH" == "$md5" ] && echo "The hash is the same. ($count)" && break
done <$DICT

输出:

The hash is the same. (4)

我改进了一点脚本。它使用clone(2)表示法而不是pipe(2)进行了另外一次md5sum<<<wordecho word|md5sum来电。

首先,它设置创建infile和HASH的测试环境。然后它读取输入文件的每一行,创建MD5校验和并检查是否与HASH匹配。如果是这样,它会向stdoutbreak写入一些消息。

恕我直言,原来的问题有点过分了。