I have a file name inputfile having following data:
database=mydb
table=mytable
partitions=p1,p2,p3
I pass this file as first argument as following:
bash mybashfile.sh inputfile
I want to use this file in my bash script that input database, table and partitions as variables
I tried this by putting the whole content of myfile in a variable
#!/bin/bash
var=$(cat $1)
echo $var
I am new to bash script so I need help
CodePudding user response:
Consider using source
, which will load the variables from that file
#!/bin/bash
source "$1"
echo "$database"
# mydb
echo "$table"
# mytable
echo "$partitons"
# p1,p2,p3
For more information about source
see this superuser-question
CodePudding user response:
You can read key, value pairs from a file with a while
loop:
#!/bin/bash
declare -A kv # declare an associative array
while IFS="=" read -r k v; do
kv["$k"]="$v"
done < "file"
declare -p kv # print the array
Prints:
declare -A kv=([table]="mytable" [database]="mydb" [partitions]="p1,p2,p3" )