I am running an executable file from inside a bash script. I need to store the output of this execution in a variable. The executable file is running and giving correct result when I just run the executable, but there is error when I try to store the output to a variable.
The executable file is a tool called pdfToolBox, and it runs like given below:
./<path_to pdfToolBox> --extracttext <path_to pdfFile>
As shown above, the executable takes a command and a file to check. The complete available documentation of this tool is found here https://help.callassoftware.com/m/pdftoolbox/l/657637-installation-and-activation-of-pdftoolbox-server-cli . I am using the CLI interface in my project.
Now, I am creating a script that accepts the executable path, and the pdf file path and executes the command mentioned above. So I can call it from the terminal like:
./check_file.sh <path_to_pdfToolBox> <path_to_pdfFile>
Here is the code I have written:
#!/bin/bash
# we will use this to find if there is any text in the file being checked
# return value from here will be used to load the proper profile for file check
find_text() {
local containedText=""
$containedText="./$pdfToolBox --extracttext $fileToCheck"
echo "$containedText"
# process containedText to check file
}
# argument 1 is the path of pdfToolBox executable
# argument 2 is the path of pdf file to be checked
pdfToolBox=$1
fileToCheck=$2
find_text
I expected the output to be saved in the variable $containedText, but I receive an error like this:
./findText2.sh: line 7: =./pdfToolbox --extracttext data/Test-Briefpapier-A3-sg.pdf: No such file or directory
I have tried using both relative paths and actual paths but none seem to work and return the same error. However, if I change line 7 to
./$pdfToolBox --extracttext $fileToCheck
instead of
$containedText="./$pdfToolBox --extracttext $fileToCheck"
it executes without any error. I cannot point out the problem here. Please help.