LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-10-2018, 08:52 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
import variable from file into current bash session


Hi,

Is there a way I could run a line which assigns a variable in a script, so that I could use it within the current bash session? In other words, I would like to use the source command only for a certain line in the file. How can I do that?
 
Old 01-10-2018, 09:26 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,056

Rep: Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349
Code:
line=$(some process to get the line you need)
eval $line
probably, but this is not the best way. And works only if the line has valid syntax. Otherwise you can somehow grep out the value you need and:
Code:
VAR=$(grep pattern file)
will do the assignment.
 
1 members found this post helpful.
Old 01-10-2018, 09:50 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Right, my problem was that I kept trying to execute the line itself, the line which assigns the variable. So what I'm basically doing is var=$(var=...whatever) or some other var name, but that's irrelevant. Thanks.
 
Old 01-10-2018, 09:57 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
No, that's not right. I got a little bit ahead of myself. A particular example:
Code:
awk 'NR==3 { print }' script
my_ip=$(ip address | grep inet | grep eth0 | awk '{ print $2 }' | cut -d "/" -f 1)
If I try var=$(awk 'NR==3 { print }'), I'd expect the variable to be assigned the output of the awk command, but that's not the case. When I echo $var, it says:
Code:
my_ip=$(ip address | grep inet | grep eth0 | awk '{ print $2 }' | cut -d "/" -f 1)
Which is normal, of course, because the output of that line is another command in itself. If I try var=$($(awk...)), then I get var=$(ip: command not found
So it doesn't seem to be working after all.

The first solution doesn't work either:
Code:
echo $line
my_ip=$(ip address | grep inet | grep eth0 | awk '{ print $2 }' | cut -d "/" -f 1)
eval $line
[no output]

Last edited by vincix; 01-10-2018 at 11:10 AM.
 
Old 01-11-2018, 01:05 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,056

Rep: Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349
sorry, I don't understand what you posted.
Code:
ip address | grep inet | grep eth0 | awk '{ print $2 }' | cut -d "/" -f 1
returns an IP address, or nothing. so my_ip will be set to that IP (of your eth0).
when you set mp_ip, eval $line has no any meaning, because the variable line was not set at all.
by the way:
Code:
my_ip=$(ip address | awk -F"[ /]*" '/inet.*eth0/ { print $3 }')
looks better a bit (at least for me)
 
1 members found this post helpful.
Old 01-11-2018, 02:47 AM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Sorry, I posted in a hurry and wasn't paying attention to some basic things.
You are obviously correct.

And yes, I agree with the fact that your solution is more elegant, especially because you're using only one tool instead of three.
I do have a question, though. How does the * work within the [ /]* regex? If I don't include it, then, in order to get the IP, I have to print $6. If I do, then, ofc, it works as you said it works.
Does it mean any number (including 0) of spaces or slashes? And the reason for it is that the indentation has several spaces? Is there any difference in this case between * and +? I know + means 1 or infinite, but in this case with * I'm guessing you couldn't have zero spaces or slashes as a delimiter. So are they equivalent when it comes to awk delimiters?

Another question:
Is inet.*eth0 a greedy regex? If "eth0" showed up twice, then it would match the string up to the last occurrence, right?

Last edited by vincix; 01-11-2018 at 02:56 AM.
 
Old 01-11-2018, 03:41 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,056

Rep: Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349
about [ /]*: you are right, + can be used too, zero is meaningless in this case, because -F specifies a delimiter, which cannot be empty (in this case, but there are cases where it is accepted).
with * and/or + subsequent spaces will count as one single delimiter, without that you need to use $6 - as you have already find this.

about inet.*eth0: in this case it is used to replace grep inet | grep eth0, so we need both (inet.*eth0 is order dependent, grep is not). Yes, it is greedy, but it is not really important here.
 
1 members found this post helpful.
  


Reply

Tags
bash, variable



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to import a file into bash script? fanoflq Linux - Newbie 10 03-21-2016 11:57 AM
bash: export variable in current or parent shell hydraMax Programming 2 09-14-2013 10:46 PM
[SOLVED] BASH: variable to get the current command_line paziulek Linux - General 14 07-19-2013 10:38 PM
how to setup session variable in mysql using option file vincent.dang Linux - Software 1 06-07-2010 11:21 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration