2014-12-16 4 views
3

다음 스크립트는 ".properties"파일에서 한 줄씩 읽은 다음 "="구분 기호를 기준으로 토큰 화하고 두 개의 변수에 값을 저장 한 다음 표시해야합니다 그것. 하지만 토큰 화하는 방법에 대한 이해를 얻지 못하고 두 개의 다른 변수에 저장 한 다음 추가 용도로 사용합니다.파일에서 셸 스크립트의 문자열 분할하기

다음 스크립트는 파일을 줄 단위로 읽는 데 문제가 없지만 문자열을 분할하는 논리를 구현하는 데 도움이 필요합니다.

#!/bin/bash 

while IFS='=' read -r col1 col2 
do 
    echo "$col1" 
    echo "$col2" 
done <testprop.properties 

IFS가 구분 출원 입력입니다 :

이 시도

FNAME=John 
LNAME=Lock 
DOB=01111989 

스크립트를

#!/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" 
+1

그것은 당신의 최종 목표 불분명 조금 있습니다 하나 대신 이동 보내는 지금

을 수행 : 당신이 변수'$ FNAME'에 원하는거야 'John','$ LNAME'을'Lock' 등으로 설정합니까? 그렇다면'source .properties' 파일을 소싱하면됩니다. – fedorqui

답변

11

를 "등록 정보 파일".

그러나 대신 파일을 구문 분석의 (같은 fedorqui 말했다), 파일을 소싱 할 수 직접 변수를 액세스 :

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. 

마지막으로 :

source testprop.properties 
echo "$FNAME" 

$ LANG=C help source에서 최소, 더 많은 인용문을 사용하십시오! http://mywiki.wooledge.org/Quotes, http://mywiki.wooledge.org/Argumentshttp://wiki.bash-hackers.org/syntax/words을 참조하십시오.

3

IFS

read

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. 

현재 file.sql가 선택했다 (SQL 쿼리를 전송) 응답 파일 자체에 분리 할 수있는 경우

0

내가 방황 ....... 은 ....... 가 선택 선택 .......

및 스크립트 파일이

이 -a 쿼리 = $ 선언 하나 개의 쿼리가있는 경우 선언과 같은 파일에 받고 잘 작동 (고양이 "file.sql")를

for Q "$ {query [@]}"; do .....뭔가 은 이제 하나의 모든 선택 선이 시간

+0

입력 파일의 모든 선택 항목이 별도의 줄에 있어야합니다. – frodo