iUser iUser - 1 month ago 20
Bash Question

Read properties file using csh script

How to read properties file using csh script?

When I googled, all I see are for bash script.

Currently what I'm using is

#!/bin/csh

set config_file=$1

echo "Configuration at : $config_file"

set server=`grep -i 'server' $config_file | cut -f2 -d'='`
set port=`grep -i 'port' $config_file | cut -f2 -d'='`

if ( "$port" == "" ) then
set port=9000
endif


And my properties file is

server=192.168.1.20
port=8081


It's working fine. But if any property is commented, it still reads the value.

server=192.168.1.20
#port=8081


Final value for port I'm getting as 8081 instead of 9000 in 2nd case.

Answer

with awk as below;

awk '$0 !~ /^#/' means that port set if line not start with #

#!/bin/csh
set config_file=$1
echo "Configuration at : $config_file"
set server=`grep -i 'server' $config_file  | cut -f2 -d'='`
set port = `grep -i 'port' $config_file | awk '$0 !~ /^#/' | cut -f2 -d'='`
if ( "$port" == "" ) then
        set port=9000
endif
Comments