S1syphus
S1syphus

Reputation: 1441

Loading variables from a text file into bash script

Is it possible to load new lines from a text file to variables in bash?

Text file looks like?

EXAMPLEfoo 
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

Variables become

$1 = EXAMPLEfoo 
$2 = EXAMPLEbar 

ans so on?

Upvotes: 12

Views: 37539

Answers (5)

AnrDaemon
AnrDaemon

Reputation: 365

None of the above will work, if your values are quoted with spaces.

However, not everythinf is lost.

Try this:

eval "$(VBoxManage showvminfo "$VMname" --details --machinereadable | egrep "^(name|UUID|CfgFile|VMState)")"
echo "$name {$UUID} $VMState ($VMStateChangeTime) CfgFile=$CfgFile"

P.S. Nothing will ever work, if your names are quoted or contain dashes. If you have something like that, as is the case with VBoxManage output ("IDE-1-0"="emptydrive" and so on), either egrep only specific values, as shown in my example, or silence the errors.

However, silencing erors is always dangerous. You never know, when the next value will have unquoted "*" in it, thus you must treat values loaded this way very careful, with all due precaution.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 343077

$ s=$(<file)
$ set -- $s
$ echo $1
EXAMPLEfoo
$ echo $2
EXAMPLEbar
$ echo $@
EXAMPLEfoo EXAMPLEbar EXAMPLE1 EXAMPLE2 EXAMPLE3 EXAMPLE4

I would improve the above by getting rid of temporary variable s:

$ set -- $(<file)

And if you have as input a file like this

variable1 = value
variable2 = value

You can use following construct to get named variables.

input=`cat filename|grep -v "^#"|grep "\c"`
set -- $input

while [ $1 ]
 do
  eval $1=$3
  shift 3
 done

Upvotes: 28

Andy
Andy

Reputation: 17791

This can be done be with an array if you don't require these variables as inputs to a script. push() function lifted from the Advanced Scripting Guide

push()            # Push item on stack.
{
if [ -z "$1" ]    # Nothing to push?
then
  return
fi

let "SP += 1"     # Bump stack pointer.
stack[$SP]=$1

return
}

The contents of /tmp/test

[root@x~]# cat /tmp/test
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

SP=0; for  i in `cat /tmp/test`; do push $i ; done

Then

[root@x~]# echo ${stack[3]}
EXAMPLE1

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360685

saveIFS="$IFS"
IFS=$'\n'
array=($(<file))
IFS="$saveIFS"
echo ${array[0]}    # output: EXAMPLEfoo 
echo ${array[1]}    # output: EXAMPLEbar
for i in "${array[@]}"; do echo "$i"; done    # iterate over the array

Edit:

The loop in your pastebin has a few problems. Here it is as you've posted it:

for i in "${array[@]}"; do echo "  "AD"$count = "$i""; $((count=count+1)); done

Here it is as it should be:

for i in "${array[@]}"; do declare AD$count="$i"; ((count=count+1)); done

or

for i in "${array[@]}"; do declare AD$count="$i"; ((count++)); done

But why not use the array directly? You could call it AD instead of array and instead of accessing a variable called "AD4" you'd access an array element "${AD[4]}".

echo "${AD[4]}"
if [[ ${AD[9]} == "EXAMPLE value" ]]; then do_something; fi

Upvotes: 3

cat somefile.txt| xargs bash_command.sh

bash_command.sh will receive these lines as arguments

Upvotes: 4

Related Questions