LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2015, 12:27 PM   #1
LetsGo
LQ Newbie
 
Registered: Apr 2015
Posts: 10

Rep: Reputation: Disabled
Check empty variable without comments


Hello,

They gave me this:
Code:
#!/bin/bash

# Write error message on stderr and die
function die() {
  echo "$@" >&2
  exit 1
}

# Load var from properties files
function load() {
  for propertie in "$@" ; do
    [[ -f "${propertie}" ]] || die "load() : \"${propertie}\" don't exist !"
    while read ; do
echo "${REPLY%=*}"
echo "${REPLY#*=}"
      [[ -n "${REPLY%%=*}" && -z "${REPLY#*=}" ]] || die "load() : Variable \"${REPLY%%=*}\" is empty!"
      eval "${REPLY%%=*}"=\'"${REPLY#*=}"\'
    done < "${propertie}"
  done
}

load "manage_srcds.conf"
echo "SRCDS_TICKRATE=$SRCDS_TICKRATE"
echo "SRCDS_SCREEN=$SRCDS_SCREEN"
echo "SRCDS_SRCDS_GAME_NAME=$SRCDS_GAME_NAME"
echo "SRCDS_MAXPLAYERS=$SRCDS_MAXPLAYERS"
but, return:
Quote:
###################################################
###################################################
load() : Variable "###################################################" is empty!

cat manage_srcds.conf


I just wish that there was no empty variable excluding those who do not have integer.

Best regards,

Last edited by LetsGo; 04-06-2015 at 07:06 AM.
 
Old 04-06-2015, 05:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
I am not sure I follow your question? i guess the first question is who are 'they' and what are you doing running something you do not understand?

Also, the link you have provided is not accessible.
 
Old 04-06-2015, 07:11 AM   #3
LetsGo
LQ Newbie
 
Registered: Apr 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
they comunity = debian-fr

The script does not work, he returns as # is empty when it is not a variable.

I just want to verify that no variable is empty in the .conf file.

I edited the link.
 
Old 04-06-2015, 08:48 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,652
Blog Entries: 10

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
Add a line to the top of your script after the #!/bin/bash line which says "set -xv". This will enable debug so you can see what happens line by line as the script runs. I suspect that the conf file happens to have one or more comment lines, which are ############################################## and as a result the if test tries to see that as a variable property, but does not and also does not ignore it being a comment.

Another thing to do in that test loop is to find any line which starts with # and assume it's a comment so skip it.
 
Old 04-06-2015, 09:23 AM   #5
LetsGo
LQ Newbie
 
Registered: Apr 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
i have add set +xv:
Quote:
# Write error message on stderr and die
function die() {
echo "$@" >&2
exit 1
}

configfile="manage_srcds.conf"
+ configfile=manage_srcds.conf
unknown=`cat $configfile | grep -Evi "^(#.*|[a-z]*='[a-z0-9 ]*')$"`
cat $configfile | grep -Evi "^(#.*|[a-z]*='[a-z0-9 ]*')$"
++ grep -Evi '^(#.*|[a-z]*='\''[a-z0-9 ]*'\'')$'
++ cat manage_srcds.conf
+ unknown='
NETWORK=""

SRCDS_USER="serveur"

SRCDS_FOLDER_NAME="serv_l4d2"

SRCDS_IP=$(sudo ifconfig | grep "inet ad" | cut -f2 -d: | awk '\''{print $1}'\'' | grep -v '\''127.*'\'' ; X_ERROR_ETH=$?)

SRCDS_RCON=""

SRCDS_PORT="27015"

CFGHOSTPORT="run01.cfg"

SRCDS_TICKRATE="30"

SRCDS_MAXPLAYERS="8"

SRCDS_MAP=""

SRCDS_SVPURE="1"

SRCDS_GAME_NAME="left4dead2"

SRCDS_APP="222860"

CLIENT_APPID="550"

SRCDS_DEBUG="-debug"

SRCDS_SCREEN="${SRCDS_FOLDER_NAME}"

SRCDS_DIR_STEAMCMD="~/steamcmd"

SRCDS_STEAMCMD_BIN="${SRCDS_DIR_STEAMCMD}/steamcmd.sh"

SRCDS_STEAM_LOGIN="anonymous"

SRCDS_STEAM_FILES="${SRCDS_FOLDER_NAME}_update.txt"

SRCDS_FORCE_DIR="~/${SRCDS_FOLDER_NAME}"

DIR_LOGS="${SRCDS_FORCE_DIR}/${SRCDS_GAME_NAME}/logs"

LOGSEXPIRE="7"

SRCDS_BIN="srcds_run"

DIR_LOGS="${SRCDS_FORCE_DIR}/${SRCDS_GAME_NAME}/logs"

UPDATE_LOG="update_`date +d%m%Y`.log"
INSTALL_LOG="install_`date +%d%m%Y`.log"

UPDATE_EMAIL=""

UPDATE_RETRY="3"

PARAM_L4D2_START="./${SRCDS_BIN} -game ${SRCDS_GAME_NAME} -console -ip ${SRCDS_IP} +exec ${CFGHOSTPORT} +map ${SRCDS_MAP} -maxplayers ${SRCDS_MAXPLAYERS} -tickrate ${SRCDS_TICKRATE} -nowatchdog -steam_dir ${SRCDS_DIR_STEAMCMD} -steamcmd_script ${SRCDS_FORCE_DIR}/${SRCDS_STEAM_FILES} -autoupdate -sv_pure ${SRCDS_SVPURE}"

PARAM_L4D2_START_DEBUG="./${SRCDS_BIN} -game ${SRCDS_GAME_NAME} -console -ip ${SRCDS_IP} +exec ${CFGHOSTPORT} -maxplayers ${SRCDS_MAXPLAYERS} -nowatchdog ${SRCDS_DEBUG}"

SRCDS_VERIFYUPDATE="1"

PARAM_UPDATE="${SRCDS_STEAMCMD_BIN} +login ${SRCDS_STEAM_LOGIN} +force_install_dir ${SRCDS_FORCE_DIR} +app_update ${SRCDS_APP} +quit"

PARAM_UPDATE_VALIDATE="${SRCDS_STEAMCMD_BIN} +login ${SRCDS_STEAM_LOGIN} +force_install_dir ${SRCDS_FORCE_DIR} +app_update ${SRCDS_APP} validate +quit"

SRCDS_BACKUP="srcds_backup"

SCRIPT_DEBUG="1"

maxloadavg="$(cat /proc/cpuinfo | grep processor | wc -l)"
loadavg1="$(cat /proc/loadavg | awk '\''{print $1}'\'')"
loadavg5="$(cat /proc/loadavg | awk '\''{print $2}'\'')"
loadavg15="$(cat /proc/loadavg | awk '\''{print $3}'\'')"'

# Load var from properties files
function load() {
for propertie in "$@" ; do
[[ -f "${propertie}" ]] || die "load() : \"${propertie}\" don't exist !"
while read ; do
echo "${REPLY%%=*}"
echo "${REPLY#*=}"
[[ -n "${REPLY%%=*}" && -z "${REPLY#*=}" ]] || die "load() : Variable \"${REPLY%%=*}\" is empty!"
eval "${REPLY%%=*}"=\'"${REPLY#*=}"\'
done < "${propertie}"
done
}

load ${configfile}
+ load manage_srcds.conf
+ for propertie in '"$@"'
+ [[ -f manage_srcds.conf ]]
+ read
+ echo '###################################################'
###################################################
+ echo '###################################################'
###################################################
+ [[ -n ################################################### ]]
+ [[ -z ################################################### ]]
+ die 'load() : Variable "###################################################" is empty!'
+ echo 'load() : Variable "###################################################" is empty!'
load() : Variable "###################################################" is empty!
+ exit 1
Quote:
I suspect that the conf file happens to have one or more comment lines, which are ############################################## and as a result the if test tries to see that as a variable property, but does not and also does not ignore it being a comment.

Another thing to do in that test loop is to find any line which starts with # and assume it's a comment so skip it.
yes sure, but how please?

Last edited by LetsGo; 04-07-2015 at 01:20 PM.
 
Old 04-06-2015, 09:38 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,652
Blog Entries: 10

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
This should be the correct test, which is to evaluate one character from the string propertie, starting at position zero:
Code:
if [[ ${propertie:0:1} == "#" ]]; then
    continue
fi
 
Old 04-06-2015, 10:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
May I ask what is the purpose of the script? Assuming you just want all the variables available to your script, why not just source the file?
 
Old 04-06-2015, 10:19 AM   #8
LetsGo
LQ Newbie
 
Registered: Apr 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
@rtmistler:
Quote:
function load() {
for propertie in "$@" ; do
[[ -f "${propertie}" ]] || die "load() : \"${propertie}\" don't exist !"

while read ; do
echo "${REPLY%%=*}"
echo "${REPLY#*=}"
if [[ ${propertie:0:1} == "#" ]]; then continue; fi
[[ -n "${REPLY%%=*}" && -z "${REPLY#*=}" ]] || die "load() : Variable \"${REPLY%%=*}\" is empty!"
#[[ && -n "${REPLY%%=*}" && -z "${REPLY#*=}" ]] || die "load() : Variable \"${REPLY%%=*}\" is empty!"
eval "${REPLY%%=*}"=\'"${REPLY#*=}"\'
done < "${propertie}"
done
}

load ${configfile}
+ load manage_srcds.conf
+ for propertie in '"$@"'
+ [[ -f manage_srcds.conf ]]
+ read
+ echo '###################################################'
###################################################
+ echo '###################################################'
###################################################
+ [[ m == \# ]]
+ [[ -n ################################################### ]]
+ [[ -z ################################################### ]]
+ die 'load() : Variable "###################################################" is empty!'
+ echo 'load() : Variable "###################################################" is empty!'
load() : Variable "###################################################" is empty!
+ exit 1

@grail manage game srcds for install a server. I execute . ${configfile} but i'll need to check
 
Old 04-07-2015, 06:17 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,652
Blog Entries: 10

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
Quote:
Originally Posted by LetsGo View Post
@rtmistler:
I hadn't really looked at your reading of the file, here's a suggestion to do that differently. $0 is a passing argument, or you can just use the direct name of your conf file. I prefer to leave that flexible.
Code:
#!/bin/sh

while read line
do
    if [[ ${line:0:1} == '#' ]]; then
        echo "$line: Is a comment"
    else
        echo "$line: Is NOT a comment"
    fi
done < $0
Start by running that script against your conf file to validate that it reads the lines and properly detects comments versus not, then expand it to include your tests.

I tend to agree with grail in that you can just source the file and you'll get your variables, plus it will skip the comments. Once you source the file, you can test for presence or absence of required variables, and also as part of the checking process, you can choose defaults when appropriate and even log the fact that those option choices were made.
 
Old 04-07-2015, 10:40 AM   #10
LetsGo
LQ Newbie
 
Registered: Apr 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
yes, work fine but how add this function to my script?

Quote:
Once you source the file, you can test for presence or absence of required variables
It's big....
 
Old 04-07-2015, 11:59 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,652
Blog Entries: 10

Rep: Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683Reputation: 1683
Quote:
Originally Posted by LetsGo View Post
yes, work fine but how add this function to my script?
Cut and paste.

Check the link pointing to my Bash Blog in my signature for some suggestions, there are examples of function use. Check also the links for Bash programming on the same line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting Help--Check empty string condition (not null, but empty!) sungchoiok Linux - Newbie 4 01-01-2012 03:46 PM
[SOLVED] HOSTNAME variable is being an empty string alexa-lol Programming 1 05-20-2011 07:15 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Excel files have empty comments in OO.o Calc DJOtaku Linux - Software 2 06-25-2010 04:08 AM
getopts empty variable felix001 Programming 2 05-12-2010 02:37 AM


All times are GMT -5. The time now is 01:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration