Bash 从文件读取时拆分shell脚本中的字符串

Bash 从文件读取时拆分shell脚本中的字符串,bash,shell,Bash,Shell,我有一个下面的脚本,它应该逐行读取“.properties”文件,然后根据“=”分隔符标记它,并将值存储到两个变量中,然后显示它。但我不理解如何标记它,然后将其存储在两个不同的变量中,然后将其用于进一步的目的 下面的脚本可以很好地逐行读取文件,但我需要帮助实现拆分字符串的逻辑 “属性文件” 脚本 #!/bin/bash echo "FileReading Starts" while read line do value=$line echo $value #Tokeni

我有一个下面的脚本,它应该逐行读取“.properties”文件,然后根据“=”分隔符标记它,并将值存储到两个变量中,然后显示它。但我不理解如何标记它,然后将其存储在两个不同的变量中,然后将其用于进一步的目的

下面的脚本可以很好地逐行读取文件,但我需要帮助实现拆分字符串的逻辑

“属性文件”

脚本

#!/bin/bash
echo "FileReading Starts"
while read line
do 
    value=$line
    echo $value
    #Tokenize Logic
    property=sample
    property_value=sample
    echo $property
    echo $property_value
done <testprop.properties

echo "Finish"
#/bin/bash
回显“文件读取开始”
读行时
做
值=$line
echo美元价值
#标记化逻辑
属性=样本
属性值=样本
echo$属性
echo$property\u值
完成尝试以下操作:

#!/bin/bash

while IFS='=' read -r col1 col2
do 
    echo "$col1"
    echo "$col2"
done <testprop.properties
$LANG=C帮助源代码

source: source filename [arguments]
Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.


最后但并非最不重要的一点是,使用更多引号!请参阅和。

可以使用
IFS
将字段分隔符值设置为
读取

范例

while IFS="=" read line val
do 
   echo $line : $val; 
done < inputFile 
内部变量

FNAME=John
LNAME=Lock
DOB=01111989
$IFS
    internal field separator
    This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

我想知道是否可以在应答文件本身中分离(发送sql查询)

当前文件.sql有 选择。。。。。。。 选择。。。。。。。 选择

脚本像声明一样进入文件,若文件只有一个查询,那个么工作正常

declare-a查询=$(cat“file.sql”)

对于“${query[@]}”中的Q;做 ..... 某物 完成


现在,它正在一次性发送所有选择行,而不是一次发送一行

您的最终目标有点不清楚:您是否希望将变量
$FNAME
设置为
John
,然后
$LNAME
设置为
锁定
,等等?如果是这样的话,只需对文件进行寻源即可:
source.properties
。输入文件的每一项选择都在单独的行中
FNAME : John
LNAME : Lock
DOB : 01111989
$IFS
    internal field separator
    This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.