So, I'm aware of that this topic has been discussed in several ways, but the responses didn't work for me.
I'm using sed to remove a line from a script, but each time I execute the bash script, I receive an error message "No such file or directory". I'm able run the command from the remote host terminal. See the sed command below.
#!/bin/bash
declare server_list="/location/of/iplist.txt"
declare file="/location/of/file/to/modify"
declare regex='"/\export JAVA_HOME=\/jvm\/home\/directory\/java/d"'
declare file_content="$(sudo grep -Fxq export
JAVA_HOME=/java/home/directory/java "${file}" )
declare script="sudo sed -i -e ${regex} ${file}"
date
cat ${server_list} | while read server
do
# connect to each server and execute the "sed -i" command
ssh -I /location/of/pub/key ${server} -o StrictHostKeyChecking=no
# read file content into condition statement
${file_content}
if [ $? -eq 0 ];
then
# confirm string exist and remove the line
echo "JAVA_HOME located on ${server}"
"${script}"
else
echo "No JAVA_HOME located on ${server}"
fi
done