Article 59GHC Need help with bash script

Need help with bash script

by
Tem2
from LinuxQuestions.org on (#59GHC)
First let me explain that I have very little expertise with bash scripting. I only use it for very simple applications.

My script is used to generate a grep command.

I use the echo command as an interim debug tool. I figure that if I can get the echo command to show the command I want to execute, all I have to do is remove the echo and the quotes and the command inside the echo should do what I want.

Here is my script (called grepper3.sh). Again, I am not an expert at this:
Code:#!/bin/bash
echo "what should I grep?"
read this

echo "grep -Ri \"$this\" > \"$this\""Here is what happens when I execute:
Code:master@master-Latitude-E6440:~$ ./grepper3.sh
what should I grep?
all that
grep -Ri "all that" > "all that"The grep command being echoed by the code is exactly what I want. But when I remove the echo and the surrounding double quotes:
Code: was: echo "grep -Ri "$this" > "$this""
changed to: grep -Ri "$this" > "$this"I get this:
Code:master@master-Latitude-E6440:~$ ./grepper3.sh
what should I grep?
all that
./grepper3.sh: line 5: "$this": ambiguous redirectIf I copy and paste what the echo statement produces, the grep command works and redirects the output to a file with the same name as my search argument, which is what I want to do.

I'm guessing that there is a simple fix, but I can't figure it out.latest?d=yIl2AUoC8zA latest?i=zdwrh_gOjuU:FUh-db8uVwI:F7zBnMy latest?i=zdwrh_gOjuU:FUh-db8uVwI:V_sGLiP latest?d=qj6IDK7rITs latest?i=zdwrh_gOjuU:FUh-db8uVwI:gIN9vFwzdwrh_gOjuU
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments