-1

I have a script which produces the following output:

[RESPONSE]  code = 211  description = Domain name not available queuetime = 0   runtime = 0.009

I need to assign only the code = 211 part to a variable. How will I do that?

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
turnic
  • 3
  • 2
  • 1
    You mean store `code = 211` in a variable say `var` – sjsam Jul 10 '16 at 03:13
  • 3
    Also is the output format constant? Also what efforts so far? – sjsam Jul 10 '16 at 03:20
  • 1
    Hi. No I do not need to store the output. The output is a result of domain check. code=211 means the domain is not available. Yes the code changes when the domain becomes available to code=210. Oh the format is yes constant. – turnic Jul 10 '16 at 10:40

2 Answers2

2

If you wish to store code = 211 to a variable say var do this :

$ var="$(./your_script | sed -E 's/.*(code = [[:digit:]]+).*/\1/')"
$ echo "$var"
code = 211

Instead, if you wish to assign 211 to code given the result of the script:

$ eval "$(./your_script | sed -E 's/.*(code = [[:digit:]]+).*/\1/;s/ //g')"
$ echo "$code"
211

Notes

  • s/.*(code = [[:digit:]]+).*/ retains the code = number part of the result
  • s/ //g deletes the spaces around = so that code=number is passed to eval
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • sjsam, I received this error. -bash-3.2$ ./check.sh sed: invalid option -- E Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... – turnic Jul 10 '16 at 10:52
  • @turnic : What platform are you on? You may replace `-E` with `-r` and lemme know the result' – sjsam Jul 10 '16 at 10:55
  • @turnic If I'm not mistaken you're running an infinite while loop? Am I right? – sjsam Jul 10 '16 at 11:27
  • It should be `if [ "$code" -eq "210" ] then`. The hint is `-eq` for comparing integers and `=` is for strings, sarcastic it might seem – sjsam Jul 10 '16 at 11:33
  • @turnic : If possible paste your script in [\[ pastebin \]](http://pastebin.com/) and gimme the link. Looks a bit obscure at the moment. – sjsam Jul 10 '16 at 11:35
  • I have put the error suppression inside quotes, Check [\[ this \]](http://pastebin.com/raw/6VrimYig) and please delete the comments except the relevant ones. – sjsam Jul 10 '16 at 12:26
  • @turnic Also could you update me what is the output of `/home/turnic/mreg/mreg checkdomain domain=netfair.de`. I mean just that command alone – sjsam Jul 10 '16 at 12:30
  • @turnic Is the output of `/home/turnic/mreg/mreg checkdomain domain=netfair.de` a one line output? – sjsam Jul 10 '16 at 12:38
  • @turnic : The link is not working. :( It's taking too long. Why don't you paste the output in pastebin and send me the link – sjsam Jul 10 '16 at 12:43
  • So a single instance of `mreg` will give you so many reponses. Am I right? – sjsam Jul 10 '16 at 13:02
1
#Assuming there is space between [response] and code.Seems there is from your example.

var=$(./yourScript.sh |awk '{print $2,$3,$4}')

echo $var
code = 211
P....
  • 17,421
  • 2
  • 32
  • 52