Such as I have one file:
file1
hostname=abcd1234
server name=qwer1234
I need get the value of hostname: abcd1234 and the server name value to input file2, how to write a script do that? Thanks a lot!
file2.sh
#!/bin/sh
var1=$hostname
var2=$server name
echo $var1
echo $var2
Is the space in the variable name a requirement? Else you can just source file1 in file2.sh:
#!/bin/sh
. ./file1
var1=$hostname
var2=$server_name
echo $var1
echo $var2
sea
3
Did you try source file1
before the var= parts from within file2.sh yet?
hth
Sorry, maybe I don't write clearly.
my means if the file1 not hostname, just like A=abcd1234, how to get the abcd1234 and input in file2?
File1:
A=abc1234d
B=qwer1234
File2:
var1=$A
var2=$B
echo $var1
echo $var2
Thanks a lot!
RudiC
6
What's the difference? Try the solutions proposed...
Maybe we have not been clear. There is obviously a language barrier here.
You cannot have a space character in a shell variable name. So you cannot have a variable named server name
.
If you change file1
to contain the text:
hostname=abcd1234
server_name=qwer1234
then if you change the contents of file2.sh
as Walter Misar suggested to:
#!/bin/sh
. ./file1
var1=$hostname
var2=$server_name
echo $var1
echo $var2
and run your script, it should print:
abcd1234
qwer1234
If this isn't what you are trying to do, we have completely misunderstood what you are trying to accomplish.
1 Like
Thank you, the . ./file1 works