user360321
user360321

Reputation: 177

Executing a shell script from a file

My OS platform is this : SunOS machinehull01 5.10 Generic_148888-05 sun4v sparc SUNW,Sun-Fire-T200

I have written a shell script to run from a file

File name: test.sh

#!/bin/sh
VARNAME=$grep '-l' TestWord /home/hull/xml/text/*.txt
echo "Found $VARNAME"

When I run the above command in the console I'm getting the correct output without errors, But when I run sh test.sh or ./test.sh I'm getting below error

test.sh: -l: not found
Found

Can someone please help me on this?

Upvotes: 0

Views: 90

Answers (2)

user360321
user360321

Reputation: 177

Got it.

#!/bin/sh
VARNAME=`grep -l TestWord /home/hull/xml/text/*.txt`
echo "Found $VARNAME"

I had to put those (`)there.

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157967

You are searching for so called "command substitution" :

VARNAME=$(grep -l TestWord /home/hull/xml/text/*.txt)
echo "Found $VARNAME"

It will execute the command between $( and the closing parenthesis ) in a subshell and return the output of the command into VARNAME.

Upvotes: 3

Related Questions