Thursday, May 16, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 5904  / 3 Years ago, mon, may 17, 2021, 12:18:54

I am working on a script to check if volume group vg0 exist. The problem is that it return Found even when I delete the volume group. So very unsure what is wrong.



#!/bin/bash

if vgdisplay | grep 'vg0' | awk '{print $3}'; then
echo "Found"
else
echo "Not found"
fi

More From » 12.04

 Answers
1

The if condition in this case is evaluated based on the exit code of awk, which is likely to be zero unless one of the previous commands in the pipeline failed to execute.



You could use the -q option for grep instead:



if vgdisplay | grep -q 'vg0'; then
echo "Found"
else
echo "Not found"
fi


The same could also be written as:



vgdisplay | grep -q 'vg0' && echo "Found" || echo "Not found"

[#26937] Tuesday, May 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
itutejagua

Total Points: 89
Total Questions: 124
Total Answers: 113

Location: British Indian Ocean Territory
Member since Sun, Feb 13, 2022
2 Years ago
itutejagua questions
;