Code for parsing a key/value in in file from shell script

前端 未结 6 1885
心在旅途
心在旅途 2020-11-30 06:44

I have a file that I need to look up a value by key using a shell script. The file looks like:

HereIsAKey This is the value

How can I do so

6条回答
  •  不知归路
    2020-11-30 07:20

    If you only need one variable at a time, you can do something like this:

    #!/bin/bash
    cat file | while read key value; do
      echo $key
      echo $value
    done
    

    The problem with this solution: The variables are only valid inside the loop. So don't try to do $key=$value and use it after the loop.

    Update: Another way is I/O redirection:

    exec 3

提交回复
热议问题