0

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.

HackersInside
  • 307
  • 2
  • 6
  • 17
  • 2
    variable is not getting substituted when single quotes are used . try del=`sed -i "/JAVA_HOME=${JV_PAT}/d" /home/admin/Vishal/test_bash.sh` – Maxim Sagaydachny Jan 01 '20 at 12:19
  • Related: [How to insert an arbitrary string after pattern with sed](https://stackoverflow.com/questions/19932796/how-to-insert-an-arbitrary-string-after-pattern-with-sed) – Mark Plotnick Jan 01 '20 at 20:01

1 Answers1

1

Since you are using bash apparently, some comments on the script.

Start your script with #!/bin/bash. This will enforce the use of bash when executing the script.

JAVA_LOC=`which java`
JV_PAT=`echo $JAVA_LOC |rev| cut -d'/' -f3- | rev` 

The bash-way of doing this is to use $(..) in stead of the back-ticks. Unless you have a specific reason (for example old Bourne-shell compatibility), do not use back-ticks. Oh, and quote your variables if you use them.

JAVA_LOC=$(which java)
JV_PAT=$(echo "$JAVA_LOC" |rev| cut -d'/' -f3- | rev) 

Read the manual for sed; what you are doing below does not make sense.

del=`sed -i  '/JAVA_HOME=${JV_PAT}/d' /home/admin/Vishal/test_bash.sh`
if [ $? = 0 ]
then
echo "Deleted"
else
echo "Nope"
fi

sed -i does not produce output normally. Your variable del will therefore always be empty. Also, the exit status of sed does not depend on the fact whether a replacement has been done. sed also executes successful without any replacement done. So, testing the exit status of sed will not tell you whether ot not such a replacement has been done.

If you need to test whether a pattern is in a file, I think grep as a knee-reflex. With that, the result will be:

if grep -q "/JAVA_HOME=${JV_PAT}" /home/admin/Vishal/test_bash.sh ; then
    sed -i "\\#JAVA_HOME=${JV_PAT}#d" /home/admin/Vishal/test_bash.sh
    echo "Deleted"
else
   echo "Nope"
fi

EDIT: As Léa Gris pointed out, the ${JV_PAT} may contain a /. Therefore, the sed contains a little more complex, using the # as separator instead of the /.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • thanks for the answer. I do get an error on the sed command mentioning - sed: -e expression #1, char 14: extra characters after command – HackersInside Jan 01 '20 at 13:58
  • Since JV_PAT will contain `/`, you need to use another separator character for sed commands. Example using `#` instead of `/`: `sed -i "\\#JAVA_HOME=${JV_PAT}#d" '/home/admin/Vishal/test_bash.sh'`. – Léa Gris Jan 01 '20 at 14:14
  • Thanks it worked with a slight change in the grep command: The leading '/' wasnt required. Thanks :) – HackersInside Jan 03 '20 at 07:16