0

I do have a config file which has a structure like this:

username abc123
licensekey qwer1234

The file is located in $HOME/.test.config.

Now I want to search for the keyword licensekey and want to copy the following word qwer1234 as my key. If the word license key is not present or the file doesn't exist I want the user to add a license key manually and crate the file.

Do you have any suggestions? And did I made some mistakes in my code so far?

My code so far is:

keyfile='$HOME/.test.config'
if grep -q licensekey "$keyfile"; then
    #what now?
else
    while true; do
        read -p "Whats your License Key? " key
        key >> ${keyfile}
        break
    done
fi
ChrizZly
  • 124
  • 3
  • 12
  • 3
    Couple of points: `'$HOME'` won't expand, you have to use double quotes: `"$HOME"`; `key >> ${keyfile}` will try to run a (non-existing) command `key`, you probably meant something like `echo "$key" >> "$keyfile"`. You also don't seem to need the while loop as you break out of it after the first iteration anyway. – Benjamin W. Mar 15 '17 at 13:44
  • I had "\$HOME". I want it to be $HOME. it is a variable in the system. not in the script. I think "$HOME" woul search for the variable HOME in my script. ye. it has to be echo -e "${key}". I forgot it. It was just fast writing here while adding it. I think I need the while loop. If there is not licensekey I want the user to write one by himself. Somewhere I read that I need a while loop for it or else the input won't be correct. – ChrizZly Mar 15 '17 at 13:53
  • `$HOME` expands to the current user's home directory, typically `/home/$USER`, where `$USER` is the username. If you write `'$HOME'` or `"\$HOME"`, it expands to literally `$HOME`, and that directory most likely doesn't exist and isn't what you meant. Quoting prevents the shell from word splitting and globbing, but in this case, because `$HOME` is on the right-hand-side of an assignment, you can actually leave it safely unquoted: `keyfile=$HOME/.test.config` should work fine. – Benjamin W. Mar 15 '17 at 13:56
  • And `while true; do command; break; done` is the same as `command`. – Benjamin W. Mar 15 '17 at 13:58
  • The `while` loop is not necessary as you are not validating the user input. Also in the code above the `while` will not help much because we are `break`ing after the first read – nu11p01n73R Mar 15 '17 at 13:58

3 Answers3

1

You can use awk to match the string in the file and extract the key

Example

$ cat file
username abc123
licensekey qwer1234

$ awk '$1 == "licensekey"{print $2}' file
qwer1234

To read the key from the user if not in file, we can write something like

key=$(awk '$1 == "licensekey"{print $2}' file)
if [[ -z $key ]]
then
        read -p "Whats your License Key? " key
        echo "licensekey $key" >> file
fi
# Do something with the kye
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • It looks like OP also wants to let the user enter the license key manually if not present. – jraynal Mar 15 '17 at 13:47
  • @jraynal I agree, my post answers *How to find a string in a file and take next word as input?* part of the question only. But the reading part is not completely clear from op's questions. – nu11p01n73R Mar 15 '17 at 13:51
  • Ye. I want to user to add a license key if there is none. I forgot it. – ChrizZly Mar 15 '17 at 13:56
  • @ChrizZly I have added an update to read the key and write it to the file. – nu11p01n73R Mar 15 '17 at 14:05
  • Thank you. Can you tell me what exactly -z does? And why do I need 2 brackets? – ChrizZly Mar 15 '17 at 14:08
  • `[[` are for testing stuff. `-z` checks wether `$key` is of zero length – nu11p01n73R Mar 15 '17 at 14:11
  • How can I suppress the output of awk? I always get the error message: There is no file /home/username/test.config. (the file shouldnt exist by opening the script first time) – ChrizZly Mar 15 '17 at 14:16
0

With grep and cut:

grep licensekey config_file | cut -d' ' -f2
Zumo de Vidrio
  • 2,021
  • 2
  • 15
  • 33
0

You can also use sed to parse out the licensekey:

#/usr/bin/env bash
set -o nounset # error if variable not set
set -o errexit # exit if error

# Using double quote let bash expand the variable
# Local limit the scope of the variable
local keyfile="${HOME}/.test.config"

#gets the first valid key
local key=$(sed -n 's/licensekey[[:space:]]//p')

#Append the key to file if not existing
if [ -z "${key}" ]; then
    read -r -p "Whats your License Key? " key
    echo "licensekey ${key}" >> ${keyfile}
fi

echo "License key is: ${key}"
jraynal
  • 507
  • 3
  • 10