Grep Not Working Inside of Script

Grep Not Working Inside of Script

I'm trying to write a script that takes a string and then looks for all instances of that string a a file. When I run the command outside of the script it runs perfectly but if I try to run it in the script is says that the file doesn't exist. Here is my code

    if [ "$#" -eq 0 ]; then
          echo "Please enter an argument"
    else
          $result=`grep $1 Sales.csv`
          echo $result
    fi

And yes the file is called Sales.csv. I would have thought it was something to do with the format of the file but the command works outside of the script which is why I'm confused

Edit: Should have mentioned that I'm running the linux from virtualBox

7

1 Answer

When assigning a variable you just put whatever you want as the variable minus the $. Putting the $ in front of the variable makes it the output of the variable. Since the variable isn't defined in the script yet the line should be:

result=$(grep $1 /some/path/to/Sales.csv)

Then calling the variable next like:

echo $result

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.