简体   繁体   English

使用变量操作 bash 脚本输出

[英]Manipulating bash script output using variables

I am new to scripting using Linux.我是使用 Linux 编写脚本的新手。 When running a script I want to be selective about what output the script show from a command, instead of displaying all of a commands output, I would like for it to display some of its words or sentences.运行脚本时,我想选择脚本从命令中显示的输出,而不是显示所有命令输出,我希望它显示一些单词或句子。 How do I do that.我怎么做。 Help will be appreciated.帮助将不胜感激。 See below.见下文。

#!/bin/bash
#

megacliout=$(sudo megacli -AdpAutoRbld -Dsply -a0)

echo $megacliout | cut -d '0' -f 2

The output I get is:我得到的输出是:

: AutoRebuild is Enabled. Exit Code: 

But I do not want 'Exit Code:' to be displayed in the output.但我不希望在输出中显示“退出代码:”。 So how do I remove that and keep everything else?那么我如何删除它并保留其他所有内容?

This question is likely better answered at SuperUser.这个问题可能在 SuperUser 上得到更好的回答。

That said, your cut command is unlikely to work reliably.也就是说,您的cut命令不太可能可靠地工作。 You are using it to extract text between two zeroes (which might or might not be present in the output).您正在使用它来提取两个零之间的文本(输出中可能存在也可能不存在)。

As an alternative, you can try grep , for example like so: grep -o 'AutoRebuild is [^.]*' This will output only phrases that start with "AutoRebuild is", continuing until the next dot.作为替代方案,您可以尝试grep ,例如像这样: grep -o 'AutoRebuild is [^.]*'这将仅输出以“AutoRebuild is”开头的短语,一直持续到下一个点。

您可以使用sed删除“退出代码”及其后的所有内容:

echo "$megacliout" | cut -d0 -f2 | sed 's/ Exit Code.*$//'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM