I want to write a script that looks for a variable and checks if it contains exact string and comment it if exists.
I have tried using sed and grep but it hasnt worked for me yet.
My script for deleting:
JAVA_LOC=`which java`
JV_PAT=`echo $JAVA_LOC |rev| cut -d'/' -f3- | rev`
del=`sed -i '/JAVA_HOME=${JV_PAT}/d' /home/admin/Vishal/test_bash.sh`
if [ $? = 0 ]
then
echo "Deleted"
else
echo "Nope"
fi
JV_PAT contains the path were java in installed excluding the /bin
JAVA_LOC = /data/jdk1.8.0_111/bin/java
JV_PAT = /data/jdk1.8.0_111
My Output:
Deleted
The script gets executed successfully, but it doesnt delete the particular line from the file test_bash.
test_bash.sh file
JAVA_HOME=/data/jdk1.8.0_111
PATH=.:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/admin/local/jdk1.8.0_111/bin:/data/hadoop28/bin:/data/hive232/bin:/data/derby1014/bin:/home/admin/local/jdk1.8.0_111/bin:/data/hadoop28/bin:/data/hive232/bin:/data/derby1014/bin:/home/admin/local/jdk1.8.0_111/bin:/data/hadoop28/bin:/data/hive232/bin:/data/derby1014/bin:/home/admin/bin:/u01/app/oracle/product/11.2.0/xe/bin:/home/admin/local/jdk1.8.0_111/bin
I want to check if JAVA_HOME contains the exact string as JV_PAT and if it does comment it.
Note: It would be nice if you could help me with the script both for commenting and deleting.