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.


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.