2

Example file:

/Variable1=Value1/Variable2=Value Value Value/Variable3=Value3 Value3/ 

I want it to read the variables and set them. Meaning I would be able to type echo $Variable1 and get Value1 as the output, etc. I tried a lot of things, but they were very long, and didn't work when the variables changed names. Does anyone know an effective method for this?

DisplayName
  • 11,688
  • 1
    We may be able to help you to implement a parser, but we can't tell you how to parse it: what are the tokens and delimiters? – steeldriver Dec 17 '15 at 20:34
  • I assume you want to set "Variable1=Value1", "Variable2=Value Value Value", and "Variable3=Value3 Value3" by reading the file somehow? – Jeff Schaller Dec 17 '15 at 20:37
  • Can you also clarify the environment you want them set in? (which shell or scripting language) – Jeff Schaller Dec 17 '15 at 20:47

5 Answers5

3

Here's a bash starting point:

OIFS="$IFS"
IFS=/; set -f
for piece in $(cat file)
do
  var=$(echo "$piece" | cut -d= -f1)
  val=$(echo "$piece" | cut -d= -f2-)
  [ -n "$var" ] && eval $var=\"$val\"
done
IFS="$OIFS"
unset piece var val

While it skips empty variable names, this doesn't do any sanity-checking of the variable names themselves.

Second way, since I was determined to find a way to do it with read:

declare -a vars
IFS=/ read -a vars < file
for piece in ${vars[*]}
do 
  mangle=$(echo "$piece" | sed 's/=\(.*\)/="\1"/')
  eval $mangle
done
unset vars piece mangle

The mangle line is just there to quote the value after the equals-sign.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • You can just use declare "$piece" without mangling. – glenn jackman Dec 17 '15 at 21:18
  • @glennjackman - the real trouble with that is what $piece is - it might be TERM= or PATH= or PS4=. the question is how to parse - validation ought to go along with that. – mikeserv Dec 17 '15 at 21:24
1

similar to Jeff Schaller's answer:

IFS=/; set -f
set -- $(tr /\\n \\n/ <file |
         sed -ne's/^[_[:alpha:]][_[:alnum:]]*=/_&/p' |
         tr /\\n \\n/)
for v do eval "${v%%=*}=\${v#*=}"; done
mikeserv
  • 58,310
1
. <(
    awk '
      BEGIN{RS="/";FS="="}
      NF==2{printf "%s=\"%s\"\n", $1, $2}
    ' file
)

This one uses awk to make the following text:

Variable1="Value1"
Variable2="Value Value Value"
Variable3="Value3 Value3"

This is sourced, so Variable{1,2,3} are available in sh.

Some points: The values are assumed to not contain ant /, =, or ".

joepd
  • 2,397
1
$ printf "%s\n" "$Variable1" "$Variable2" "$Variable3"



$ cat file
/Variable1=Value1/Variable2=Value Value Value/Variable3=Value3 Value3/ 
$ source <(tr / "\n" < file | sed -r 's/=(.*)/="\1"/')
$ printf "%s\n" "$Variable1" "$Variable2" "$Variable3"
Value1
Value Value Value
Value3 Value3
glenn jackman
  • 85,964
0

For bash: (if your string is in a file called 'liste.txt')

IFS="/"
for val in $(cat liste.txt); do echo "$val" | sed -r "s/=(.*)$/=\"\1\"/g" | eval; done

After, you just have to try:

echo "$Varaible1"
echo "$Variable2"

and so on

dervishe
  • 465