gpt4 book ai didi

unix - 无法在 korn 脚本的每一行末尾插入日期和主机名

转载 作者:行者123 更新时间:2023-12-04 04:58:48 25 4
gpt4 key购买 nike

我需要在使用 ksh 时将日期和主机名插入到行尾。这些行是从 vmstat os 输出的,如果一行以数字开头,我需要将 Date 和 Hostname 添加到行尾:

我有这个测试脚本:

#!/usr/bin/ksh

while read line
do
printf "$line"
if [[ "$line" =~ "^([0-9])" ]]; then
date '+ %m-%d-%Y %H:%M:%S'
hostname
else
echo
fi
done

当我这样做时
vmstat 3 | test



syntax error at line 6 : `=~' unexpected

最佳答案

您似乎使用了 ksh 的实现哪个不知道=~运营商,例如ksh88 .

您可以求助于例如grep做匹配,例如
test.sh :

#!/bin/ksh

while read line; do
printf "$line"
if echo "${line}" | grep -q "^[0-9]"; then
printf "%s %s" "$(
date '+ %m-%d-%Y %H:%M:%S'
)" "$(hostname)"
fi
echo
done

示例运行:
$ printf "foo\n3 bar\nquux\n" | ./test.sh
foo
3 bar 05-06-2013 18:53:59 myhostname
quux

关于unix - 无法在 korn 脚本的每一行末尾插入日期和主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com