Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ECHO to execute commands?

I've come up with a cool script that will produce the output that I need, but it only displays on the screen, so I have to copy, then paste in the commands to get them to execute. Here's the abbreviated version of my script:

#!/bin/bash runc=/etc/csf/csf.pl -d for IP in `tail -400 iptext.txt` do cc=`geoiplookup $IP` echo -e $runc $IP    $cc | grep Algeria echo -e $runc $IP    $cc | grep Argentina echo -e $runc $IP    $cc | grep Armenia echo -e $runc $IP    $cc | grep Azerbaijan echo -e $runc $IP    $cc | grep Bolivia echo -e $runc $IP    $cc | grep Brazil done 

Okay, so it loops through the list of IP addresses in iptext.txt, then does a geoIP lookup on each, if (in this example) there are two geoIP matches in the list, let's say for Armenia and Brazil, I will see output like this to the shell window:

/etc/csf/csf.pl -d 46.162.242.17 GeoIP Country Edition: AM, Armenia /etc/csf/csf.pl -d 200.147.38.50 GeoIP Country Edition: BR, Brazil 

This is great, but I want more than just output, I actually want the /etc/csf/csf.pl -d command to run, and block (just and only) the IP in the lines that match the country name, in the list.

I've tried various things with my limited knowledge of shell scripting, but so far, nothing seems to work. So is there some option for ECHO I am missing that would actually run the command rather than just printing out the line?

like image 295
jols Avatar asked Jul 16 '13 10:07

jols


People also ask

How does echo work command line?

echo command in linux is used to display line of text/string that are passed as an argument . This is a built in command that is mostly used in shell scripts and batch files to output status text to the screen or a file. In above example, text after \c is not printed and omitted trailing new line.

How do you echo the output of a command?

The echo command writes text to standard output (stdout). The syntax of using the echo command is pretty straightforward: echo [OPTIONS] STRING... Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.

What does echo do in Linux script?

The echo command is a built-in Linux feature that prints out arguments as the standard output. echo is commonly used to display text strings or command results as messages.

Is echo a bash command?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


2 Answers

Just put your command into parenthesis like this:

echo $(ls) 

You can also have text before the command

echo "The date is $(date)" 

For Example

echo "Enter Text Here $(Command Here)" 
like image 159
Neeraj Verma Avatar answered Oct 10 '22 03:10

Neeraj Verma


A simple way that won't need modification of your script would be to pipe the command's output to another bash instance. Like this:

yourscript | bash - 

The - tells bash that it should read commands from stdin.


However, if you are not searching for a quick solution, it is possible to build and execute the command dynamically as well. Like this:

cmd="ls" if [ "foo" != "bar" ] ; then     cmd="$cmd -a" then  # ... and so on  # now execute it: $cmd 
like image 25
hek2mgl Avatar answered Oct 10 '22 02:10

hek2mgl