linux - Cut 对我来说是错误的,说它不是文件或目录。我如何使用它来 grep "on"(见下文)

标签 linux bash shell unix scripting

<分区>

早上好!我正在尝试使工作中的某些事情自动化。因此,在一个 informix 框中,运行命令“onstat -g dri”,它会给出以下输出(通常):

root@us32999s5000d0a:/root # onstat -g dri

IBM Informix Dynamic Server Version 12.10.FC7W1XPAEE -- On-Line -- Up 39 days 22:56:20 -- 8688044 Kbytes

Data Replication at 0x56b17028:
 Type           State        Paired server        Last DR CKPT (id/pg) Supports Proxy Writes
 standard       off                                       -1 / -1         NA

DRINTERVAL   30
DRTIMEOUT    30
DRAUTO       0
DRLOSTFOUND  /usr/informix_engine/etc/dr.lostfound
DRIDXAUTO    1
ENCRYPT_HDR  0
Backlog      0

我的问题是,如何才能在“状态”下 grep ONLY for on or off? 我的问题是,当我使用 cut 尝试隔离开或关时,出现错误:

./AutoDoc.sh: line 27: } | /usr/bin/cut -c1-2": No such file or directory

这是我的脚本,因此您可以准确地看到我在做什么:

#!/bin/bash
export LANG="C"

######## Written by Blake Smreker | b0s00dg | BSmreker@walmart.com ####
######## The goal of this script is to automate DR tickets ############

#Asks for DC number to be used later

printf "What is the four digit DC number?"

read DC

#Asks for 2 lowercase digit country code

printf "What is the two digit lowercase country code?"

read cc

#Defines dsinfx naming convention

infmx=$"dsinfmx.s${DC}.${cc}"

#Defines a variable to call upon the command for DR

onstat=$". /u/data/environment; onstat -g dri | grep -ie last -e type -e primary -e secondary -e ibm"

fred='/usr/bin/dzdo -u oseho /bin/ssh -qo PreferredAuthentications=publickey root@$infmx ". /u/data/environment; /usr/informix/bin/onstat -g dri | /usr/bin/grep -e on -e off | /usr/bin/grep -v informix |  /usr/bin/grep -v  data  | /usr/bin/awk '{print $2}' | /usr/bin/cut -c1-2"'

if [ "$fred" == "on" ]; then
echo "DR is on, please resolve the ticket"
else


#Logs in to the informix box for the defined DC, then runs the above command

echo "Finding the status of DR on the primary and secondary Informix box:"

echo ""


echo "--------------------------------------------------------------------------------------------------------------"

echo ""

/usr/bin/dzdo -u oseho /bin/ssh -qo PreferredAuthentications=publickey root@$infmx $onstat

echo ""

echo "--------------------------------------------------------------------------------------------------------------"
fi

最佳答案

我相信您只需要有第二个字段的状态值(第 27 行的命令导致您的代码片段出现问题),可能有很多方法,这里有一些可能对您有帮助。

第一种方法: awk 在一行中查找关键字 State,然后设置变量和下一行,它打印第二个字段是您当时命令的实际状态,然后脱离命令。

your_command | awk '/State/{flag=1;next} flag{print $2;exit}'

第二种方法: 使用 awk 但由于我们知道状态将始终位于第 3 行的第 2 个字段,因此很难对其进行编码并在此处获取它.

your_command | awk 'FNR==3{print $2}' 

第三种方法:尝试使用您的方法使用cutheadtail.

your_command | head -3 | tail -1 | cut -d" " -f9

附加说明:如果您看到这篇文章的@Basile Starynkevitch 提到的评论部分,您将看到关于 URL mywiki.wooledge.org/BashFAQ/050 的内容请引用我们如何在变量中使用命令来获取其值。如果我们仔细观察第 27 行的代码片段,我相信这是因为变量赋值没有正确完成。请尝试阅读此链接并在努力后发布您的回复。

关于linux - Cut 对我来说是错误的,说它不是文件或目录。我如何使用它来 grep "on"(见下文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51220788/

相关文章:

java - 在 Linux 上调用 JNI 代码时奇怪的崩溃;我是否正确编译了我的共享库?

linux - 如何在 Linux 服务结构中启动多个进程?

linux - unshare --pid/bin/bash - fork 无法分配内存

java - 使用 shell 在 Jenkins master 上运行 Selenium Java 代码

java - 将带空格的字符串从 shell 传递到 java

linux - 如何在我的系统上找到可执行文件的所有版本?

linux - 如何将 stderr 重定向到 stdout 和 stderr 本身

linux - 构建 RPM 包的开发版和普通版

bash - bash 中的 while 循环使用 pgrep 检查服务是否存在

python - 在Windows 10中使用Python实现文件创建任务自动化