bash - How to set variables from each line store in file -


i want read file line line , pass first , second fields arguments bash script, iterate next line , same thing.

my file pwd.out:

/path/dir/name1/date name1 /path/dir/name2/date name2 

i have tried following without success:

while read line; dir=`awk '{print $1}'`; name=`awk '{print $2}'`; echo "./myprogram $dir somethinghere $name"; done < pwd.out 

where outputs:

./myprogram /path/dir/name1/date /path/dir/name2/date somethinghere 

i think somehow $dir getting values lines , $name not being set.

what have is:

./myprogram /path/dir/name1/date somethinghere name1 ./myprogram /path/dir/name2/date somethinghere name2 

thanks in advance

you don't need awk this. read variables in order come, such this:

while read dir name    ./myprogram $dir somethinghere $name done < pwd.out 

test

see example in echo dir=$dir, name=$name given file:

$ while read dir name; echo "dir=$dir, name=$name"; done < pwd.out dir=/path/dir/name1/date, name=name1 dir=/path/dir/name2/date, name=name2 

your awk command not working because not giving input it.

it work if did this, although unnecessary use external command awk bash can handle can see above.

while read line    dir=$(awk '{print $1}' <<< "$line")    name=$(awk '{print $2}' <<< "$line")    echo "./myprogram $dir somethinghere $name" done < pwd.out 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -