Skip to content
Advertisement

Find if a shell variable contains specific string and comment it

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.

Advertisement

Answer

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 /.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement