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- 
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
./testScript.sh: line 5: ../OLD- Permission denied

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

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


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

echo ${FILE}
echo ${SSID}

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-

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.