BASH : Using declare in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH : Using declare in a loop
# 1  
Old 12-14-2008
BASH : Using declare in a loop

I have a config file that needs to be formatted before it's sourced to a script. I've been formatting it with sed and dropping it in /tmp then sourcing it from there.

I'd like to just format the config file with sed then declare the variables without having to create a temporary file in /tmp. So I tried a loop like this ...

Code:
for line in $config_formatted; do
  var_name=$(echo "$line" | awk -F= '{print $1}')
  var_value=$(echo "$line" | awk -F= '{print $2}' | sed 's/\(^"\|"$\)//g')
  declare $var_name=$var_value
done

This works fine except when $var_value contains a value that appends itself with another variable that should have been declared already. For example, when it formats my config file and runs through the loop I have it echo a few variables it declared at the end. I end up with ...

Code:
$ ./configRead

audit_host : http://server
non_ie_page : ${audit_host}/oa/admin_pc_add_2.php

As you can see from my original loop, I used sed to format $var_value to remove the quotations...which seems a little odd to have to do in the first place. So perhaps I don't fully understand how declare is working? Is this simply a limitation that I cannot get around?

Thanks in advance for any advice.
# 2  
Old 12-15-2008
Quote:
Originally Posted by crs6785
I have a config file that needs to be formatted before it's sourced to a script. I've been formatting it with sed and dropping it in /tmp then sourcing it from there.

What is the format of the config file? Why do you have to change it?
Quote:
I'd like to just format the config file with sed then declare the variables without having to create a temporary file in /tmp. So I tried a loop like this ...

Code:
for line in $config_formatted; do
  var_name=$(echo "$line" | awk -F= '{print $1}')
  var_value=$(echo "$line" | awk -F= '{print $2}' | sed 's/\(^"\|"$\)//g')
  declare $var_name=$var_value
done


Why are you using declare? It's not necessary.
Quote:
This works fine except when $var_value contains a value that appends itself with another variable that should have been declared already. For example, when it formats my config file and runs through the loop I have it echo a few variables it declared at the end. I end up with ...

Code:
$ ./configRead

audit_host : http://server
non_ie_page : ${audit_host}/oa/admin_pc_add_2.php

As you can see from my original loop, I used sed to format $var_value to remove the quotations...which seems a little odd to have to do in the first place.

It is odd. Why do it?
Quote:
So perhaps I don't fully understand how declare is working?

What do you think it does?
Quote:
Is this simply a limitation that I cannot get around?

I don't know of any limitation to declare. It does what it is supposed to (but I've never had a reason to use it).
# 3  
Old 12-15-2008
Quote:
Originally Posted by cfajohnson
What is the format of the config file? Why do you have to change it?
The config file was mostly intended to be used with a vb script. I didn't really want to start maintaining two separate config files, as I actually do use the vb script as well.

The config file holds variables in the form of ...

variable = "value"

Quote:
Originally Posted by cfajohnson
Why are you using declare? It's not necessary.
I'm using declare because if I don't bash will try to use $var_name as a command. Using declare I can create variables by using variables. This way I can just add variables to the config file if needed and I don't need to go back to the script and manually declare each one separately on it's own line.

Quote:
Originally Posted by cfajohnson
It is odd. Why do it?
Because they would throw my script off as the script takes some action based on the contents of the variables. I would have to account for the variable values having quotations around them which they shouldn't have in the first place.

Quote:
Originally Posted by cfajohnson
What do you think it does?
Well, running 'help declare' at the shell gives me ...

Quote:
declare: declare [-afFirtx] [-p] [name[=value] ...]
Declare variables and/or give them attributes. If no NAMEs are
given, then display the values of variables instead. The -p option
will display the attributes and values of each NAME.
I assumed (yeah,yeah...bad idea to make assumptions Smilie) that it would be like declaring a variable normally only with this the declaration is explicit and you can define its type (dunno exactly what practical use that would give?).

However, after I wrote this I wrote a test that simply does...

Code:
#!/bin/bash

audit_host="http://server"
var_name="non_ie_page"
var_value="${audit_host}/oa/admin_pc_add_2.php"
declare $var_name=$var_value

echo "audit_host : $audit_host"
echo "non_ie_page : $non_ie_page"

and it gives the following output ...

Quote:
So I'm guessing the issue ultimately lies in the way I'm getting the variable values, which seems to allude to a pipeline "issue" (not really an issue, just the way bash handles it) that I read in this thread.

However, I just got around my problem by doing the following ...

Code:
    for line in $config_formatted; do
      var_name=$(echo "$line" | awk -F= '{print $1}')
      var_value=$(echo "$line" | awk -F= '{print $2}' | sed 's/\(^"\|"$\)//g')
      echo "$line" | grep -q '^.*_page=\$' && var_value="${audit_host}${var_value/\$\{audit_host\}/}"
      declare $var_name=$var_value
    done

So all in all, it works now. It's a bit of a kluge, but oh well.

Thanks for your reply!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Declare and grep a variable via ssh/remote/loop

If I am running a bash command, and some awk getting the ethernet adapter on the local machine. It works fine. But if I will run it from the remote, it is EMPTY on echo and throwing error in grep. Thank you This work perfectly fine $ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

2. Shell Programming and Scripting

Help on for loop in bash

Hi, In the code "for loop" has been used to search for files (command line arguments) in directories and then produce the result to the standard output. However, I want when no files are named on the command line, it should read a list of files from standard input and it should use the command... (7 Replies)
Discussion started by: Ra26k
7 Replies

3. Shell Programming and Scripting

Bash For Loop Help

This is a short program I wrote to search through a directory and move files containing the keyword to a folder. #!/bin/bash echo 'What is the directory?' read DIR echo -e '\n' echo 'What is the keyword?' read KEY echo -e '\n' cd $DIR rmdir 'relevant_files' mkdir 'relevant_files'... (5 Replies)
Discussion started by: zenyoul
5 Replies

4. Shell Programming and Scripting

If loop in bash

Hello, I have a script that runs a series of commands. Halfway through the script, I want it to check whether everything is going alright: if it is, to proceed with the script, if it isn't to repeat the last step until it gets it right. My code so far looks like this, simplified a bit: ... (3 Replies)
Discussion started by: Leo_Boon
3 Replies

5. UNIX for Dummies Questions & Answers

How do I declare boost?

Hello all, I am trying to "make" a database system, VDB (Veritas Data Base), and when I run "make" I receive the following error: VDBException.h:19: error: expected `)' before '*' token VDBException.h:20: error: expected `)' before '*' token VDBException.h:43: error: expected `)' before '*'... (4 Replies)
Discussion started by: Tyler_92
4 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

9. Programming

File declare !

Dear all I declare like this in my program : int main(int argc ,char **argv) { FILE *ft; char* ini_file; char fbuf; char sendbuf; char strbuf; } When I compile this is the error : cc: "send.c", line 28: error 1588: "ft" undefined. (1 Reply)
Discussion started by: iwbasts
1 Replies

10. Shell Programming and Scripting

declare, assign variables using array, counter, loop

using bash Tru64... converting DCL to shell... any tips to make this work would be greatly appreciated. Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call... (3 Replies)
Discussion started by: egkumpe
3 Replies
Login or Register to Ask a Question