Sponsored Content
Top Forums Shell Programming and Scripting BASH : Using declare in a loop Post 302268069 by crs6785 on Monday 15th of December 2008 01:44:34 AM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 12:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy