[LINUX] Evaluate and concatenate strings in variables using eval in bash

Purpose

--Evaluate and execute a string in a variable using eval in bash --Use eval in bash to concatenate and execute strings in variables

environment

eval usage example

It is assumed that the following two txt files are stored in the same directory.

hoge.txt


hoge hoge

fuga.txt


fuga fuga

Use eval to evaluate and concatenate strings instead of simple strings. I will omit the explanation of the basic functions of eval.

example.sh


#!/bin/bash

value='hoge'

#Store the command as a string in a variable
#In the case of giving a command as a character string to a function in bash,
#Assuming a case where a command is read as a character string from a file
cmd='grep $value hoge.txt'

echo $cmd
#output-> grep $value hoge.txt
#In case of echo, the command is not executed.Variables are not expanded and are output as strings

eval $cmd
#output-> hoge hoge
#For eval, the variable is expanded and the command is executed

Execution result


$ ./example.sh
grep $value hoge.txt
hoge hoge

Evaluate variables in a string and output with echo

success.sh


#!/bin/bash

#Execute grep on the txt file in the same directory,If a search pattern is found,Function to output a message.
#Search pattern as the first argument,Pass the message to be output as the second argument
grep_text() {
	for txt_file in $(ls . | grep ".txt$"); do
		grep_result=$(grep $1 $txt_file)
		if [ $? -eq 0 ]; then
			eval echo $2
		fi
	done
}

query='hoge'
#Include variables used in the function in the output message
message='Search target found.File name found:$txt_file'
grep_text $query "${message}"

query='fuga'
message='Search target found.Found sentence:$grep_result'
grep_text $query "${message}"

Execution result


$ ./success.sh
Search target found.File name found:hoge.txt
Search target found.Found sentence:fuga fuga

If you don't use eval, variables in the string will not be evaluated, as shown below.

failed.sh


#!/bin/bash

grep_text() {
	for txt_file in $(ls . | grep ".txt$"); do
		grep_result=$(grep $1 $txt_file)
		if [ $? -eq 0 ]; then
			#When not using eval
			echo $2
		fi
	done
}

query='hoge'
message='Search target found.File name found:$txt_file'
grep_text $query "${message}"

query='fuga'
message='Search target found.Found sentence:$grep_result'
grep_text $query "${message}"

Execution result


$ ./fail.sh
Search target found.File name found:$txt_file
Search target found.Found sentence:$grep_result

Evaluate and execute commands in strings

cmd.sh


#!/bin/bash

grep_text() {
	for txt_file in $(ls | grep ".txt$"); do
		#Execute grep passed as an argument
		grep_result=$(eval $1)
		#If a search pattern is found,Output message
		if [ -n "$grep_result" ]; then
			echo "Search target found.Found sentence:$grep_result"
		fi
	done
}

cmd='grep "hoge" $txt_file'
echo $cmd
grep_text "$cmd"

cmd='grep "fuga" $txt_file'
echo $cmd
grep_text "$cmd"

Execution result


$ ./cmd.sh
grep "hoge" $txt_file
Search target found.Found sentence:hoge hoge
grep "fuga" $txt_file
Search target found.Found sentence:fuga fuga

Recommended Posts

Evaluate and concatenate strings in variables using eval in bash
Using global variables in python functions
Search for character strings in files [Comparison between Bash and PowerShell]
Notes using cChardet and python3-chardet in Python 3.3.1.
Example of using class variables and class methods
Python variables and data types learned in chemoinformatics
[python] Difference between variables and self. Variables in class
Checking methods and variables using the library see
Practice applying functions and global variables in Python
Y / n processing in bash, python and Go
Try using ChatWork API and Qiita API in Python