Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
DB="ORADB"
FDB="ORADB001"
LDB="ORADB009"
FDB_N=`echo $FDB | awk -F "$DB" '{print $2}'`
LDB_N=`echo $LDB | awk -F "$DB" '{print $2}'`
TD=`echo $FDB_N |wc -c`
TD_1=`expr $TD - 1`
BEGIN=$(echo $FDB_N | sed 's/^00\?//')
END=$(echo $LDB_N | sed 's/^00\?//')
TND=`expr $(expr $END - $BEGIN) + 1`
echo $TND
j=1
while [[ $j -le $TND ]]
do
for ((i=$BEGIN; i<=$END; i++)); do
# printf $DB"%0"$TD_1"d%s\n" $i
DB$j=$(printf $DB"%0"$TD_1"d%s\n" $i)
done
j=((j++))
don
I want my script to store the output as variables. This is required as this output would be inputs to complete the actual task of my script.
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?
@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?
TS1SPM001
DBN1 exported
./test1: line 31: DBN2: command not found
TS1SPM002
DBN2 exported
./test1: line 31: DBN3: command not found
TS1SPM003
DBN3 exported
./test1: line 31: DBN4: command not found
TS1SPM004
DBN4 exported
./test1: line 31: DBN5: command not found
TS1SPM005
DBN5 exported
./test1: line 31: DBN6: command not found
TS1SPM006
DBN6 exported
./test1: line 31: DBN7: command not found
TS1SPM007
DBN7 exported
./test1: line 31: DBN8: command not found
TS1SPM008
DBN8 exported
./test1: line 31: DBN9: command not found
TS1SPM009
DBN9 exported
./test1: line 31: DBN10: command not found
I thought the commands enclosed in `` will be executed first and this can be enclosed in $() too.
There is not set precedence except for the fact the shell will expand all items working from the inside out. The main advantage of $(), apart from being clearer IMO, is that they can be nested.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.