bash shell 讀取引數檔案常見方法

selectshen發表於2015-08-18
1.透過eval
[oracle@ct6605 ~]$ cat config.ini
GDBNAME=ct6605
SID="ct6605"
TEMPLATENAME="General_Purpose.dbc"
SYSPASSWORD="system"
SYSTEMPASSWORD="system"
DATAFILEDESTINATION=/u02/oradata
RECOVERYAREADESTINATION=/u09/orabackup
TOTALMEMORY="700"
[oracle@ct6605 ~]$ cat test.sh
#!/bin/bash
eval `cat  /home/oracle/config.ini`
#while read line;
#do
#eval "$line"
#done < /home/oracle/config.ini
echo $GDBNAME
echo $SID
echo $TEMPLATENAME
echo $SYSPASSWORD
echo $SYSTEMPASSWORD
echo $DATAFILEDESTINATION
echo $RECOVERYAREADESTINATION
echo $TOTALMEMORY
[oracle@ct6605 ~]$ sh test.sh
ct6605
ct6605
General_Purpose.dbc
system
system
/u02/oradata
/u09/orabackup
700
2.透過sed
[oracle@ct6605 ~]$ cat config.ini
[GENERAL]
RESPONSEFILE_VERSION = "11.2.0"
OPERATION_TYPE = "createDatabase"
[CREATEDATABASE]
GDBNAME = "ct6605"
SYSTEMPASSWORD = "system"
DATAFILEDESTINATION =/u02/oradata
[oracle@ct6605 ~]$ cat test02.sh
#!/bin/sh
GetKey(){
    section=$(echo $1 | cut -d '.' -f 1)
    key=$(echo $1 | cut -d '.' -f 2)
    sed -n "/\[$section\]/,/\[.*\]/{
     /^\[.*\]/d
     /^[ \t]*$/d
     /^$/d
     /^#.*$/d
     s/^[ \t]*$key[ \t]*=[ \t]*\(.*\)[ \t]*/\1/p
    }" config.ini
}

echo $(GetKey "GENERAL.RESPONSEFILE_VERSION")
echo $(GetKey "GENERAL.OPERATION_TYPE")
echo $(GetKey "CREATEDATABASE.GDBNAME")
echo $(GetKey "CREATEDATABASE.SYSTEMPASSWORD")
echo $(GetKey "CREATEDATABASE.DATAFILEDESTINATION")
[oracle@ct6605 ~]$ sh test02.sh
"11.2.0"
"createDatabase"
"ct6605"
"system"
/u02/oradata

參考文章:http://shenling258-hotmail-com.iteye.com/blog/1454087

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-1775246/,如需轉載,請註明出處,否則將追究法律責任。

相關文章