Question : Bash Scripting problem

I am having difficulties with a bash script I am writing. The two problems I am having are listed below. Any assistance you can provide would be appreciated. A copy of the base script is listed below.

Problem 1:
when I pass the array to the function ReadValues, I will need to add an "=" to teh end of each work in the array so when the file is read, it will look for the correct string.
Example: Primaryset would need to be set to Primaryset= before the count variable is defined.

Problem 2:
The array that was sent to the ReadValues function contains the list of variables defined when the script started. Each of these variables are name the same as the string I will be looking for in the config file, but I have not yet figures hot how to save the value of $result to the variable with the same name as the value in $key.
I could do this with a case statement, but I rather not.
Any help would be appreciated.

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
#!/bin/bash
 
filename=~/config.txt
Primaryset=""
Backupset=""
Auxset1=""
Auxset2=""
Auxset3=""
# I have about 15 more values I will be adding here
 
ReadValues (){
  for key in $1
  do
    count=$(echo $key | wc -m)
    result=$(grep $key $filename | cut -c $count-)
    echo $result # This line is only for testing
    $key=$result # Here is the problem line
  done
    echo $Primaryset # This line is only for testing
    echo $Backupset # This line is only for testing
}
 
ReadValues "Primaryset Backupset Auxset1 Auxset2 Auxset3"
 
echo ** Display results
echo $Primaryset
echo $Backupset
echo #Auxset1
echo #Auxset2
echo #Auxset3
echo ***
Open in New Window Select All

Answer : Bash Scripting problem

1. You don't have to add '='. You can add ^ before variable name, to match lines which start with your string.
(So, in line 15, change $key to ^$key). If you want to add '=', just do it :) Change "$key" (in line 15) to "$key=".

2. In line 17 add "eval" (without "") in front of $key=... (so it shoud look: eval $key=$result)

Probably you want to increase count variable by 1 (to avoid catching '=' sign).
You can do it in two ways - add some char (ie. '=') in line 14 - wc will count it, and you will have proper value. Or increase count ie. in 'cut' line (line 15): result=$(grep $key $filename | cut -c $((count+1)-)

Below, your script corrected:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
#!/bin/bash
 
filename=~/config.txt
Primaryset=""
Backupset=""
Auxset1=""
Auxset2=""
Auxset3=""
# I have about 15 more values I will be adding here
 
ReadValues() {
   for key in $1; do
      count=$(echo $key | wc -m)
      result=$(grep ^$key $filename | cut -c $((count+1))-)
      echo $result # This line is only for testing
      eval $key=$result # Here is the problem line
   done 
   echo $Primaryset # This line is only for testing
   echo $Backupset # This line is only for testing
}
 
ReadValues "Primaryset Backupset Auxset1 Auxset2 Auxset3"
 
echo "** Display results"
echo $Primaryset
echo $Backupset
#echo #Auxset1
#echo #Auxset2
#echo #Auxset3
echo "***"
Open in New Window Select All
Random Solutions  
 
programming4us programming4us