how to execute lines coming from a grep result?

719016

I've got a text file with certain installation instructions, and I know I can grep for a unique occurrence in the file. For example, the text file has something like:

MYDIR=`find /home/user -name somedir`
export PERL5LIB=$PERL5LIB:$MYDIR

In bash, how can I execute the lines after a grep in the file? Something like:

execute result from "grep somedir INSTALLFILE"
execute result from "grep 'export PERL5LIB' INSTALLFILE"
glenn jackman

Assumptions:

  • you have control over this file and are not in danger of malicious code
  • you want to set these variables your current shell

You could redirect your commands to a temp file and execute that:

tmp=$(mktemp)
{
    grep somedir INSTALLFILE
    grep 'export PERL5LIB' INSTALLFILE
} > "$tmp"
. "$tmp"

Or you could evaluate the results

eval "$(grep somedir INSTALLFILE)"
eval "$(grep 'export PERL5LIB' INSTALLFILE)"

Updating an old answer. What I would do today is use a process substitution:

source <(
    grep somedir INSTALLFILE
    grep 'export PERL5LIB' INSTALLFILE
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to echo multiple lines into files coming from a grep result

From Dev

Execute command from grep result

From Dev

How to evaluate eagerly a sequence coming from grep in Raku?

From Dev

How to evaluate eagerly a sequence coming from grep in Raku?

From Dev

How can I write lines coming from two lists?

From Dev

Counting the number of lines (from result of grep) with a specific symbol in one column?

From Dev

How to display lines 2-4 after each grep result?

From Dev

How to execute a command with each GREP result while TAILING a file

From Dev

How to grep the following lines from a file?

From Dev

How to grep two lines from lshw?

From Dev

How to grep information from multiple lines?

From Dev

How to grep the result of a grep?

From Dev

How to substract 1 from grep result

From Dev

Conditionally execute code coming in from parameters

From Dev

Conditionally execute code coming in from parameters

From Dev

How to grep particular lines

From Dev

How to grep multi lines

From Dev

Unexpected result coming from JSON parser

From Dev

How to run grep in parallel on single lines from a list

From Dev

grep -f A B - How to reverse and get lines NOT containing words from A

From Dev

How to use awk or grep to select data from different lines?

From Dev

How do I grep a string from lines that contain a certain number?

From Dev

how do i execute a command after grep from a find in linux

From Dev

How do I replace part of result coming from Database with predefined text

From Dev

Grep Lines from File with spaces

From Dev

Grep files matching result of grep from a file

From Dev

how to escape empty lines from end of command result

From Dev

How to map result sets from an execute SQL task using ManagedDTS

From Dev

grepping a substring from a grep result

Related Related

  1. 1

    How to echo multiple lines into files coming from a grep result

  2. 2

    Execute command from grep result

  3. 3

    How to evaluate eagerly a sequence coming from grep in Raku?

  4. 4

    How to evaluate eagerly a sequence coming from grep in Raku?

  5. 5

    How can I write lines coming from two lists?

  6. 6

    Counting the number of lines (from result of grep) with a specific symbol in one column?

  7. 7

    How to display lines 2-4 after each grep result?

  8. 8

    How to execute a command with each GREP result while TAILING a file

  9. 9

    How to grep the following lines from a file?

  10. 10

    How to grep two lines from lshw?

  11. 11

    How to grep information from multiple lines?

  12. 12

    How to grep the result of a grep?

  13. 13

    How to substract 1 from grep result

  14. 14

    Conditionally execute code coming in from parameters

  15. 15

    Conditionally execute code coming in from parameters

  16. 16

    How to grep particular lines

  17. 17

    How to grep multi lines

  18. 18

    Unexpected result coming from JSON parser

  19. 19

    How to run grep in parallel on single lines from a list

  20. 20

    grep -f A B - How to reverse and get lines NOT containing words from A

  21. 21

    How to use awk or grep to select data from different lines?

  22. 22

    How do I grep a string from lines that contain a certain number?

  23. 23

    how do i execute a command after grep from a find in linux

  24. 24

    How do I replace part of result coming from Database with predefined text

  25. 25

    Grep Lines from File with spaces

  26. 26

    Grep files matching result of grep from a file

  27. 27

    how to escape empty lines from end of command result

  28. 28

    How to map result sets from an execute SQL task using ManagedDTS

  29. 29

    grepping a substring from a grep result

HotTag

Archive