0

My config file looks as follows:

ver 3
file test1.conf ~/etc
file test2.conf ~/etc/conf
script tst.sh

I'd like my script to iterate through all lines prefixed with "file", extract filename and location values and execute some operation. Since now I got:

files=$(cat file.conf | grep file)

But then I'm not sure how to get into iteration to extract second and third string out of each line.

Kal800
  • 35
  • 1
  • ash is POSIX Shell compatible, and bash contains many extensions, but you might get some ideas in https://mywiki.wooledge.org/BashFAQ/001 – Bodo May 03 '22 at 15:50
  • If you need something as complicated as reading a config file, that is a strong indication that you should be using a proper programming/scripting language and not the shell. Do you have to use a shell for this? Can't you use perl or python or anything else? – terdon May 03 '22 at 16:25
  • No I cannot, because it is simple linux on the router. But I managed to solve it using read and awk. – Kal800 May 03 '22 at 17:17
  • @Kal800 You can write an answer for your own question. – Bodo May 03 '22 at 18:27

1 Answers1

0

Just like you would do with any POSIX shell:

while read type name path; do
  if [ "$type" = file ]; then
    echo "$path/$name";
  fi
done < your.conf

Use read to split your lines into variables, test for the first varible to be file and do what you want to do with the other elements. You don't need awk or the like for this.

Philippos
  • 13,453