有如下配置文件setDaemon.cfg,希望读取其中内容,并赋值给shell脚本中的变量
daemonFileName=/home/work/local/liumengting/shellScripts/daemon.sh
daemonLogFileName=/home/work/local/liumengting/daemon.log.
protectedFileName=/home/work/local/liumengting/shellScripts/clearLog.shcat $fileName | while read line
do
[statements]
donefor line in `cat $fileName`
do
[statements]
donewhile read line
do
[statements]
done < $fileName#!/bin/bash
daemonFilename="test.sh"
fileName="/home/work/local/liumengting/setDaemon.cfg"
cat $fileName | while read line
do
parameterName=`echo $line | awk -F "=" '{print $1}'`
parameterValue=`echo $line | awk -F "=" '{print $2}'`
if [ "$parameterName"x = "daemonFileName"x ]
then
daemonFilename=$parameterValue
echo $daemonFilename
break
fi
done
echo $daemonFilename
/home/work/local/liumengting/shellScripts/daemon.sh
test.sh#!/bin/bash
daemonFilename="test.sh"
fileName="/home/work/local/liumengting/setDaemon.cfg"
for line in `cat $fileName`
do
parameterName=`echo $line | awk -F "=" '{print $1}'`
parameterValue=`echo $line | awk -F "=" '{print $2}'`
if [ "$parameterName"x = "daemonFileName"x ]
then
daemonFilename=$parameterValue
echo $daemonFilename
break
fi
done
echo $daemonFilename/home/work/local/liumengting/shellScripts/daemon.sh
/home/work/local/liumengting/shellScripts/daemon.sh#!/bin/bash
daemonFilename="test.sh"
fileName="/home/work/local/liumengting/setDaemon.cfg"
while read line
do
parameterName=`echo $line | awk -F "=" '{print $1}'`
parameterValue=`echo $line | awk -F "=" '{print $2}'`
if [ "$parameterName"x = "daemonFileName"x ]
then
daemonFilename=$parameterValue
echo $daemonFilename
break
fi
done < $fileName
echo $daemonFilename/home/work/local/liumengting/shellScripts/daemon.sh
/home/work/local/liumengting/shellScripts/daemon.shThis is a test
Can you guess the output#!/bin/bash
fileName="/home/work/local/liumengting/testFor.cfg"
for line in `cat $fileName`
do
echo $line
doneThis
is
a
test
Can
you
guess
the
output#!/bin/bash
fileName="/home/work/local/liumengting/testFor.cfg"
while read line
do
echo $line
done < $fileNameThis is a test
Can you guess the output
本文详细解析了Shell脚本中读取配置文件的三种方法,并通过实例展示了每种方法的运行结果和优劣,帮助读者理解不同方法的特点和适用场景。
1904

被折叠的 条评论
为什么被折叠?



