Issue
I need to define a global variable for further refrence
This is my code
# .bashrc
LOCAL_CONF_DIR='/var/www/vhosts/vhost/test.conf'
cat ${LOCAL_CONF_DIR} | while read LINE
do
if [ "ServerName" == "${LINE:0:10}" ]; then
s=( $LINE )
SERVER_NAME=$s[1];
fi
done
echo $SERVER_NAME
Doesn't work for me RHE Linux 6.0
many thanks ;)
Solution
Your if syntax
isn't correct and your loop should not be using cat
. Use this code:
SERVER_NAME=""
while read -r LINE
do
if [ "${LINE:0:10}" = "ServerName" ]; then
SERVER_NAME="${LINE:10}"
fi
done < "${LOCAL_CONF_DIR}"
echo "$SERVER_NAME"
Answered By - anubhava