我有一个属性文件test.properties,其内容如下:
x.T1 = 125
y.T2 = 256
z.T3 = 351
Run Code Online (Sandbox Code Playgroud)
如何将y.T2( 256) 的值分配给 shell 脚本中的某个变量并回显该值?
小智 7
检查这个,将有帮助:
expVal=`cat test.properties | grep "y.T2" | cut -d'=' -f2`
Run Code Online (Sandbox Code Playgroud)
您想read在脚本中循环使用。虽然您可以创建source文件,但如果符号周围有空格,则该文件不起作用=。这是处理读取文件的方法:
#!/bin/sh
# test for required input filename
if [ ! -r "$1" ]; then
printf "error: insufficient input or file not readable. Usage: %s property_file\n" "$0"
exit 1
fi
# read each line into 3 variables 'name, es, value`
# (the es is just a junk variable to read the equal sign)
# test if '$name=y.T2' if so use '$value'
while read -r name es value; do
if [ "$name" == "y.T2" ]; then
myvalue="$value"
fi
done < "$1"
printf "\n myvalue = %s\n\n" "$myvalue"
Run Code Online (Sandbox Code Playgroud)
输出
$ sh read_prop.sh test.properties
myvalue = 256
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11480 次 |
| 最近记录: |