bash - Split String in shell script while reading from file -
i have following script should read line line ".properties" file , tokenize on base of "=" delimiter , store values 2 variables , display it. not getting understanding of how tokenize , store in 2 different variables , use further purposes.
following script works fine in reading file line line need in implementing logic of splitting string.
"properties file"
fname=john lname=lock dob=01111989
script
#!/bin/bash echo "filereading starts" while read line value=$line echo $value #tokenize logic property=sample property_value=sample echo $property echo $property_value done <testprop.properties echo "finish"
try :
#!/bin/bash while ifs='=' read -r col1 col2 echo "$col1" echo "$col2" done <testprop.properties
ifs
input filed separator.
but instead of parsing file (like fedorqui said), can source file , accessing variables directly:
source testprop.properties echo "$fname"
from $ lang=c source
:
source: source filename [arguments] execute commands file in current shell. read , execute commands filename in current shell. entries in $path used find directory containing filename. if arguments supplied, become positional parameters when filename executed. exit status: returns status of last command executed in filename; fails if filename cannot read.
last not least, use more quotes ! see http://mywiki.wooledge.org/quotes, http://mywiki.wooledge.org/arguments , http://wiki.bash-hackers.org/syntax/words.
Comments
Post a Comment