.sh file not checking the if condition correctly on ubuntu through fixlet

Hello All,

I’m trying to run the below action script on an Ubuntu machine to find and delete the folder and unset the environment variable by creating a script using “createfile until EOF1” condition in the action script. Everything is working fine as mentioned in fixlet, and I am getting exit code 0, but whatever is written on the script is not working as expected, like not capturing the environment variable and not deleting the folder or unsetting the environment variable. through fixlet…

Just to update, the same script if I run manually on a Ubuntu machine is working perfectly and removing all mentioned folders and unsetting variables with both sudo bash and source commands from the terminal.

I’m requesting to check and help me if I’m missing anything.

Action script:

createfile until Eof1
#! /bin/bash

#Find and delete the SPARK home directory

folder_name=$(printenv "SPARK_HOME")   ** this is not capturing the variable of SPARK_HOME but same command is working fine when running manually ***
if [ -d "$folder_name" ]; then
sudo rm -rf "$folder_name"
echo " Folder deleted succussfully"
else
echo "Folder does not exit"
fi

#Remove entry from .bashrc file

if [ -f ~/.bashrc ]; then
if grep -q "export SPARK_HOME" ~/.bashrc; then
sed -i 'export SPARK_HOME/d' ~/.bashrc
echo "Spark entry removed"
else
echo "spark entry does not exist"
fi

#unset entry from PATH and variable
unset SPARK_HOME
export PATH=$(echo $PATH | sed -e 's/:\/opt\/spark\/bin//g')
echo "Spark entry removed"

source ~/.bashrc

Eof1

delete __Download/script.sh
move __createfile /var/opt/BESClient/__Download/script.sh
wait chmod +x /var/opt/BESClient/__Download/script.sh
wait /var/opt/BESClient/__Download/script.sh

===========================================================
Note:
I have tried execute this script with below commands none of it worked.

  1. wait /bin/bash -c /var/opt/BESClient/__Download/script.sh
  2. wait /bin/bash -c “source /var/opt/BESClient/__Download/script.sh”
  3. wait /bin/sh -c /var/opt/BESClient/__Download/script.sh

Also, I have tried creating a sh script directly on the fixlet, but still, it’s not working.

You misspelled ‘folder_name’ on the first line.

This also is running in the ‘root’ environment so it would only unset SPARK_HOME for the root user. You might need to loop through the other users’ home directories and cleanup their .bashrc files as well.

3 Likes

Thank you for the update,
I just updated the folder_name on the script.
My concern is that the value is being removed from the .bashrc file, but it is not being unset because “folder_name=$(printenv “SPARK_HOME”)” This line is not capturing the value of “SPARK_HOME,” but the same command is working fine when running manually.

It’s odd, I have tried folder_name="$SPARK_HOME" as well, but still, it’s not capturing the value when only running from bigfix.

Also, can you help me with executing “source /var/opt/BESClient/__Download/script.sh” through the BigFix action script?

Thanks in Advance…

Thanks, able to run above script after modified the script to get the currently logged-in users details.

Thanks you for your help.