使用csh脚本读取属性文件

时间:2016-10-04 06:17:00

标签: shell csh

如何使用csh脚本读取属性文件?

当我用Google搜索时,我看到的只是bash脚本。

目前我正在使用的是

#!/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

我的属性文件是

server=192.168.1.20
port=8081

它工作正常。但是如果任何属性被注释,它仍然会读取值。

server=192.168.1.20
#port=8081

端口I的最终值在第二种情况下得到8081而不是9000。

1 个答案:

答案 0 :(得分:1)

使用awk如下;

awk '$0 !~ /^#/'表示如果行不以#

开头,则设置端口
#!/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
相关问题