LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Would like to "echo" a line, "read" reply, then execute the reply: How in bash? (https://www.linuxquestions.org/questions/linux-newbie-8/would-like-to-echo-a-line-read-reply-then-execute-the-reply-how-in-bash-4175532847/)

UnixAgain 02-02-2015 03:37 PM

Would like to "echo" a line, "read" reply, then execute the reply: How in bash?
 
Apologies. I've been out of Unix for 14 years, and the gears have rusted. Also, Unix has changed - enough similarities to memory that I'm pretty comfy though.

Glad to see lots of info available via google. However changes are going to take getting used to. Have set up a testbed of 3 nearly identical machines, hoping to make an (at home) private network where I can automate installs and set-ups using NIS and NFS (Similar to work I once did).

Have found useful scripts interspersed with comments, would like to copy and paste them to a bash script that:

Reads the line.
Echos the line.
Reads the reply.
Executes what is typed.

Essentially, by making myself type (or copy & paste) the commands I think I'll become familiar.

Figured I'd use a spreadsheet, select the file, and paste into column B of the spreadsheet. In column A I'd paste:

echo "

and in column C I'd paste:

"

Then I'd cut and paste into a text document, insert the lines:

read A
$A

behind every command necessary to the actual task.

Figured I'd have to escape the ' and ".

Ran a test (test.bash):

#!/bin/bash
for i in `seq 1 10`;
do
echo "Enter a command:"
read A
echo "Trying $A"
$A
done

works fine when I type

ls -alg

but not so when I type

ls -alg | grep dwrx

so I'm wondering if someone has a suggestion?

Thanks for any ideas.

Keith Hedger 02-02-2015 06:07 PM

try using
Code:

...
echo "Trying $A"
eval $A
done
...


veerain 02-02-2015 11:39 PM

There is a typo. It should be eval instead of evaal.

chrism01 02-03-2015 04:00 AM

Try adding a new 2nd line
Code:

#!/bin/bash
set -xv


Keith Hedger 02-03-2015 04:46 AM

Quote:

Originally Posted by veerain (Post 5310984)
There is a typo. It should be eval instead of evaal.

Thanks I was typing on a tablet!

Habitual 02-03-2015 07:59 AM

Interactive and non-interactive shells and scripts

UnixAgain 02-03-2015 08:27 AM

Thanks! eval (oh yeah! I remember that now!) does the trick. Also thanks for the set -xv, which will be useful when the scripts have been modified to fit my situation. Appreciate it!


All times are GMT -5. The time now is 04:03 AM.