Bash script issue
Hi Guru
Following is part of a script which is being written to automate one of our work, but i couldn't be successful on getting the result i expected. Can someone help? Code:
#!/bin/bash Code:
DB1=ORADB001 |
First off - are you trying to set variables from this bash script? Or are you trying to output it to a file, which can then be used to set the variables?
|
Hi!
I honestly have to admit that I don't understand the logic in your script. Not saying it is bad, mind you, I just don't get it! However... this: Code:
j=((j++)) Code:
i=$(($i+1)) Best regards, HMW |
Quote:
Code:
let i++ And of course this has been discussed: http://www.linuxquestions.org/questi...sh-4175437116/ |
Quote:
|
Variables are local to the script. Unless you "export" them.
So... Code:
export DB$j=$(printf $DB"0"$TD_1"d%s\n" $i) |
Thank you all for helping me on this.
@szboardstretcher: I am trying to set the variables and the value of these variables will be used as final result of this script @HMW: Yes, i was wrong. I corrected in the script. @shadow 7: Not aware that $() and `` does the same thing. I thought the commands enclosed in `` will be executed first and this can be enclosed in $() too. Please correct me if i am wrong. As far as the variables are concerned, i would like to keep it local to the script. But i am working on specific part of the script. This portion of the script should give output as DB1=DB001, and DB2=DB002 (obviously, as per the inputs given in the script) But i have a doubt on setting the variable name itself. "DBN$j" is this the right bash variable name? Modified script: Code:
#!/bin/bash Code:
TS1SPM001 |
Quote:
So you can do $(cmd1 $(cmd2)), but `cmd1 `cmd2`` will fail As for your code, I would perhaps looking at doing most of it with just bash as the calls to outside commands like awk and sed are not really needed. So I have commented out your original pieces and put in what I would have done: Code:
#!/usr/bin/env bash Hope that helps :) |
A variable array is probably better suited than a variable variable name.
http://www.tldp.org/LDP/abs/html/arrays.html But that means that your Code:
DB1=ORADB001 Code:
DB[1]=ORADB001 Code:
DBN$j=$(printf $DB"0"$TD_1"d%s\n" $i) Code:
"DBN$j"=$(printf $DB"%0"$TD_1"d%s\n" $i) Code:
DBN[$j]=$(printf $DB"%0"$TD_1"d%s\n" $i) |
Quote:
Edit: I see that grail has used that approach in his solution above. |
Thanks again Grail, Shadow 7 and HMW.
@Grail: Your script is much powerful yet simple. I have to learn a lot to write a script as yours. I will mark this thread as resolved. |
All times are GMT -5. The time now is 12:23 AM. |