Thursday, 26 February 2015

Shell script to identify invalid variable paths in environment

All environment variables configured in Linux or Unix OS, should be correct means - path present in environment variable should be correct. Sometime over the time, due to file system changes or directory structure changes or software upgrade or installation in system, paths become invalid. It is very time consuming to check each variable paths (env command) and validate it, especially PATH variable may contain many different paths. 

Here is the script to identify each environment variable and its wrong path configured in system. 
No impact, no delete or no changes, script does. It only reads environment variables and identify wrong paths in environment variables. Store below text in some shell script, provide execute permission (chmod 775 abc.sh) and execute it.

*******************************************************************

for Variable in `env | grep "/"`
#for Variable in `env | grep PATH`
do
V_name=`echo $Variable | awk -F"=" '{ print $1 }'`
V_Value=`echo $Variable | awk -F"=" '{ print $2 }'`
            for Path_value in `echo $V_Value | awk 'BEGIN { RS = ":" }; {print $0}'`
             do
                                                if [[ ! -f $Path_value && ! -d $Path_value ]];
                                                then
                                                                                echo $V_name " :- " $Path_value
                                                 fi
               done
  done​


*********************************************************************

Regards,
Ketan Pitroda.