Skip to next line if the variable is string

Hi

I have the follwoing requirement
I have a file as follows:

# cat priy
yyy.poweroff_cmd = /sbin/poweroff
hhh.powersave-nap = 1

When this file is provided as input, I first used "awk" command and saved variables present after "="

replace=$line
replace1=`echo $line | awk -F "=" '{print $1}'`

Now i see the output of replace1 at first iteration as "/sbin/poweroff"
My problem is if i see the output of replace1 as a string I should skip this line and proceed to second iteration where I can fetch the second line
Please help..:confused:

No need to bother awk unless you are using a very old shell. With e.g. a recent bash , you could try

replace="yyy.poweroff_cmd = 1"
[[ "${replace##* }" =~ [^0-9] ]] && echo string || echo number
number
replace="yyy.poweroff_cmd = /sbin/poweroff"
[[ "${replace##* }" =~ [^0-9] ]] && echo string || echo number
string

If you do the extra mile and make the config file's entries like hhh.powersave-nap=1 , you could source them and immediately have the variables defined for later processing.

1 Like

One way is to assume the number is not zero - using awk (use this code for the two lines of shell you show)

replace1=$(awk ' int ($(NF) ) == 0 {continue }  # int returns zero when $NF is a string 
                         {print $(NF) }' priy )

This logic may not work for what you are doing.

1 Like

It worked .. Thanks :slight_smile: