LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2018, 01:12 PM   #1
Srinivas4783
LQ Newbie
 
Registered: Aug 2018
Posts: 2

Rep: Reputation: Disabled
Reading Key Value Pair in Shell Scripting.


Hi,

I have the configuration with the below details. Where certain details were repeated like file1, file2, file3 etc.
In the shell scripting I am running a for loop for three times (No of File Count) and need to pick each value and assign to some variable in the loop. But the correct value is not getting assigned to the variable. Based on the file name retrieved I am moving to different directory.

Service_Name=XYZ
Service_Code=123
NoofFiles:3
File_Name_1=ABC
Directory_1=/tmp
File_Name_2=CDB
Directory_2=/tmp/xyx
File_Name_3=XYZ
Directory_3=/tmp/xyx/zyx

Sample Script:

source <inputFile>
for (( i=1; i <= $NoofFiles; ++i ))
do

FileName="$File_Name"_"$i"
echo "The filename is $FileName

done.

Thanks
Srini
 
Old 08-23-2018, 01:22 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,948

Rep: Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811Reputation: 5811
Quote:
Originally Posted by Srinivas4783 View Post
Hi,

I have the configuration with the below details. Where certain details were repeated like file1, file2, file3 etc.
In the shell scripting I am running a for loop for three times (No of File Count) and need to pick each value and assign to some variable in the loop. But the correct value is not getting assigned to the variable. Based on the file name retrieved I am moving to different directory.

Service_Name=XYZ
Service_Code=123
NoofFiles:3
File_Name_1=ABC
Directory_1=/tmp
File_Name_2=CDB
Directory_2=/tmp/xyx
File_Name_3=XYZ
Directory_3=/tmp/xyx/zyx

Sample Script:
Code:
source <inputFile>
 for (( i=1; i <= $NoofFiles; ++i ))
                do

                FileName="$File_Name"_"$i"
                echo "The filename is $FileName       
                  
done.
What variable isn't getting assigned? What does your input look like, and what do you want the output to look like?? See the "Question Guidelines" link in my posting signature, and use CODE tags when posting code. Your post is nearly impossible to read.
 
Old 08-23-2018, 03:24 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,193

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
You can read value pairs (or tuples) with a "while read" loop:
Code:
while IFS="=" read Directory FileName
do
  echo "The filename is $FileName and directory is $Directory"
done <<< "\
/tmp=ABC
/tmp/xyx=CDB
/tmp/xyx/zyxA=XYZ"
The IFS variable is the "Input Field Separator", whitespace by default.

Alternatively you can define for each field an array, but you have to fill each array separately.
Code:
FileName=( "ABC" "CDB" "XYZ")
Directory=( "/tmp" "/tmp/xyz" "/tmp/xyx/zyxA" )

NooFields=${#Directory[@]}

for ((i=0; i<$NooFields; i++))
do
  echo "The filename is ${FileName[i]} and the directory is ${Directory[i]}"
done
 
1 members found this post helpful.
Old 08-24-2018, 04:24 AM   #4
Srinivas4783
LQ Newbie
 
Registered: Aug 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks a lot @MadeInGermany

This is my exact requirement and has solved my issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading a file and sending mail by shell scripting..?? Plz help urgent..!! sukhdip Linux - Newbie 10 10-03-2011 02:16 AM
[SOLVED] read from keyboard while reading from file in SHELL SCRIPTING m3ll0 Programming 11 10-30-2010 08:30 AM
Reading files in shell scripting barunparichha Linux - Software 17 10-26-2010 05:18 AM
Shell scripting,;problem reading from database and exporting to html sunksullen Programming 7 05-18-2007 02:16 PM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration