如何使用 shell 中另一个文件的值?
How Do I use Values from another file in shell?
现在我正在尝试纠正一个脚本,该脚本将根据脚本中的一组设置值检查 .properties 文件中的值
但出于某种原因,当我调用这些值时,它们不会正确匹配,这就是我目前所拥有的,我相信也许 .properties 值存储正确
#!/bin/bash
SuccessfulDiffRun="true"
timestamp() { date +"%a %d %b %Y"; }
TodaysDate=$(timestamp)
echo ""
echo $TodaysDate
echo ""
#Properties Call
file="savedState.properties"
#Echo out the file
while read LINE; do echo "$LINE"; done < savedState.properties
#Check Values Hotpatch.
echo ""
echo "Running Checks HotPatch..."
if [ "$TodaysDate" = "$WD_MANAGEGOLD_DATETIMESTAMP" ]; then
echo Dates Are A Match
if [ "$SuccessfulDiffRun" = "$WD_MANAGEGOLD_SUCCESS" ]; then
echo Diff Run Successful
echo Hotpatch Run Was Successful
else
echo Diff Run Not Successful
exit 0
fi
else
echo Dates Not A Match
exit 0
fi
#Check Values RC.
echo ""
echo "Running Checks RC..."
if [ "$TodaysDate" = "$WD_MANAGERC_DATETIMESTAMP" ]; then
echo Dates Are A Match
if [ "$SuccessfulDiffRun" = "$WD_MANAGERC_SUCCESS" ]; then
echo Diff Run Successful
echo RC Run Was Successful
else
echo Diff Run Not Successful
exit 0
fi
else
echo Dates Not A Match
exit 0
fi
以及 .properties 文件中的值
WD_MANAGEGOLD_DATETIMESTAMP=Wed 21 May 2015
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP=Wed 21 May 2015
WD_MANAGERC_SUCCESS=false
任何帮助都会很棒
创建一个在属性文件中查找的函数get_value。
function get_value {
grep "^=" savedState.properties | cut -d= -f2-
}
...
WD_MANAGERC_DATETIMESTAMP="$(get_value WD_MANAGERC_DATETIMESTAMP)"
你有不同的选择来让它工作。
首先,它的 "Wed 21 May 2015" 与任何内容都不匹配,因为它是 "Thu 21 May 2015"... 但这是一个细节。
如果您解析的文件包含 bash 变量定义,您需要替换 echo
...
while read LINE; do echo "$LINE"; done < savedState.properties
...
with eval(并最终删除 "
)
...
while read LINE; do eval $LINE; done < savedState.properties
...
您也可以用简单的 source savedState.properties
替换整行
最后用"
保护属性文件中的字符串定义
WD_MANAGEGOLD_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGEGOLD_SUCCESS="false"
WD_MANAGERC_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGERC_SUCCESS="false"
最好的,
五、
如果您只是 read
.properties 文件,则不会创建变量。每行存储在 LINE
中,如果要将字符串作为代码执行,请使用 eval
:
while read LINE; do eval $LINE; done < savedState.properties
但是一定要把里面有空格的字符串用引号引起来,如
WD_MANAGEGOLD_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGERC_SUCCESS=false
另一个技巧是将文件作为脚本获取:
source savedState.properties
或
. savedState.properties
相同。
此外,timestamp 并不意味着像'Wed 21 May 2015' 那样代表日期的字符串。时间戳表示从 01/01/1970 开始的秒数(或毫秒数)。是数字,不是字符串。
如果您在当前脚本中执行 source
.properties
文件(或任何其最终名称),正确的做法是首先检查它是否存在且可读,然后source
:
#!/bin/bash
...
[ -r ".properties" ] && . ".properties" || {
printf "error: source of file '.properties' failed, exiting"
exit 1
}
现在我正在尝试纠正一个脚本,该脚本将根据脚本中的一组设置值检查 .properties 文件中的值
但出于某种原因,当我调用这些值时,它们不会正确匹配,这就是我目前所拥有的,我相信也许 .properties 值存储正确
#!/bin/bash
SuccessfulDiffRun="true"
timestamp() { date +"%a %d %b %Y"; }
TodaysDate=$(timestamp)
echo ""
echo $TodaysDate
echo ""
#Properties Call
file="savedState.properties"
#Echo out the file
while read LINE; do echo "$LINE"; done < savedState.properties
#Check Values Hotpatch.
echo ""
echo "Running Checks HotPatch..."
if [ "$TodaysDate" = "$WD_MANAGEGOLD_DATETIMESTAMP" ]; then
echo Dates Are A Match
if [ "$SuccessfulDiffRun" = "$WD_MANAGEGOLD_SUCCESS" ]; then
echo Diff Run Successful
echo Hotpatch Run Was Successful
else
echo Diff Run Not Successful
exit 0
fi
else
echo Dates Not A Match
exit 0
fi
#Check Values RC.
echo ""
echo "Running Checks RC..."
if [ "$TodaysDate" = "$WD_MANAGERC_DATETIMESTAMP" ]; then
echo Dates Are A Match
if [ "$SuccessfulDiffRun" = "$WD_MANAGERC_SUCCESS" ]; then
echo Diff Run Successful
echo RC Run Was Successful
else
echo Diff Run Not Successful
exit 0
fi
else
echo Dates Not A Match
exit 0
fi
以及 .properties 文件中的值
WD_MANAGEGOLD_DATETIMESTAMP=Wed 21 May 2015
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP=Wed 21 May 2015
WD_MANAGERC_SUCCESS=false
任何帮助都会很棒
创建一个在属性文件中查找的函数get_value。
function get_value {
grep "^=" savedState.properties | cut -d= -f2-
}
...
WD_MANAGERC_DATETIMESTAMP="$(get_value WD_MANAGERC_DATETIMESTAMP)"
你有不同的选择来让它工作。 首先,它的 "Wed 21 May 2015" 与任何内容都不匹配,因为它是 "Thu 21 May 2015"... 但这是一个细节。
如果您解析的文件包含 bash 变量定义,您需要替换 echo
...
while read LINE; do echo "$LINE"; done < savedState.properties
...
with eval(并最终删除 "
)
...
while read LINE; do eval $LINE; done < savedState.properties
...
您也可以用简单的 source savedState.properties
最后用"
WD_MANAGEGOLD_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGEGOLD_SUCCESS="false"
WD_MANAGERC_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGERC_SUCCESS="false"
最好的, 五、
如果您只是 read
.properties 文件,则不会创建变量。每行存储在 LINE
中,如果要将字符串作为代码执行,请使用 eval
:
while read LINE; do eval $LINE; done < savedState.properties
但是一定要把里面有空格的字符串用引号引起来,如
WD_MANAGEGOLD_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGERC_SUCCESS=false
另一个技巧是将文件作为脚本获取:
source savedState.properties
或
. savedState.properties
相同。
此外,timestamp 并不意味着像'Wed 21 May 2015' 那样代表日期的字符串。时间戳表示从 01/01/1970 开始的秒数(或毫秒数)。是数字,不是字符串。
如果您在当前脚本中执行 source
.properties
文件(或任何其最终名称),正确的做法是首先检查它是否存在且可读,然后source
:
#!/bin/bash
...
[ -r ".properties" ] && . ".properties" || {
printf "error: source of file '.properties' failed, exiting"
exit 1
}