bbraun17
bbraun17

Reputation: 31

Grep multiple substrings from a variable without a loop

I am writing a script that will parse a web interface of a UPS (Uninterruptable Power Supply) for data, given the argument provided. I have my entire script coded correctly to accommodate for one argument.

What I would like to accomplish is entering one string of arguments separated by '+' signs, such as hostname+model+status+uptime, and then grep each one, without using a loop. Is this feasible?

The argument is assigned to $_var by use of the positional parameter $1. Like I said, I have this set up to work properly with one argument. I am only looking to modify the script for multiple arguments.

#!/bin/bash

curl=/usr/bin/curl
grep=/bin/grep
sed=/bin/sed
site=<ommitted>

if [ ${#} -lt 1 ]
then
     echo "usage: $0 <vars := name|\"name|name\">"
     exit 1
fi

# required parameters
_vars=${1}

result="$(${curl} -s ${site} | ${sed} -e 's/1,142d//; s/<[^>]*>//g' | ${grep} -w -i -E ${_vars} | ${sed} 's/.*: //; s/^\([0-9.-]\+\) .*/\1/')"

echo "${result}"

Upvotes: 2

Views: 96

Answers (1)

anubhava
anubhava

Reputation: 785561

You can convert + to | and use grep -E:

vars='hostname+model+status+uptime'
... | grep -E "${vars//+/|}"

Upvotes: 1

Related Questions