I How can I get the output from grep into a variable? In the following program I've tried the SSID line two ways but I am not getting what I want.

SSID=$(grep) ${FILE} -o empty-ssid
Gives me this

garrett@bedroom ~/Desktop/folder/project $ ./testScript.sh ../OLD-172.28.50.30-system.cfg 
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
./testScript.sh: line 5: ../OLD-172.28.50.30-system.cfg: Permission denied
../OLD-172.28.50.30-system.cfg

SSID=$(grep ${FILE} -o empty-ssid)
Gives me this

garrett@bedroom ~/Desktop/folder/project $ ./testScript.sh ../OLD-172.28.50.30-system.cfg 
grep: empty-ssid: No such file or directory
../OLD-172.28.50.30-system.cfg

Program

FILE=${1}
SSID=$(grep) ${FILE} -o empty-ssid

echo ${FILE}
echo ${SSID}
#END#

Recommended Answers

All 4 Replies

This is where you use the back-quote method, as in

SSID=`grep ${FILE} -o empty-ssid`

You do need the file "empty-ssid" to exist in the local directory however or you will still get the "No such file or directory" error.

empty-ssid is not a file, it's a line of text inside a file. I thought using the backtick was teh same as using $() ?

Not necessarily. In any case, your use of it is incorrect. Ok. You are looking for empty-ssid inside the named file then? If so, your syntax for grep (order of options) is incorrect, and it should be like this:

SSID=`grep -o empty-ssid ${FILE}`

garrett@bedroom ~/Desktop/folder/project $ ./testScript.sh OLD-172.28.50.30-system.cfg

Above is what I'm getting now. It's returning $FILE but not $SSID.

Here's what the line looks like now ->

SSID=$(grep -o empty-ssid ${FILE})

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.