-1

whats wrong with this bash script:

acme2=$(dig txt @$1 _acme-challenge.$1.de)                                            
acme3=$(echo $acme2 | grep "^_acme")                                                    

acme2 has the whole output, but acme3 is always empty

I searched several solutions, and tried other possibilities, but nothing works...

acme3=echo $acme2|grep acme                                                          
acme3=$($acme2|grep "^_acme")                                                        
acme3=$(grep "acme" $acme2)                                                          
acme3=$(echo "$acme2" | grep "^_acme")                                              
7
  • 1
    What is $1? You should post the debug with set -x Commented Feb 12 at 15:32
  • Please show us the output of printf '%s\n' "$acme2". We really can't help you parse data you don't show us. And clarify exactly what you want to do here. What is the expected output? Why are you expecting a line beginning with _acme?
    – terdon
    Commented Feb 12 at 15:41
  • 1
    consider updating the question with the complete output from a) typeset -p acme2 and b) the expected contents of acme3
    – markp-fuso
    Commented Feb 12 at 15:57
  • 1
    obligatory reading: Why does my shell script choke on whitespace or other special characters?
    – ilkkachu
    Commented Feb 12 at 19:40
  • 2
    anyway, instead of spamming random variations blindly and hoping something useful happens, it's often more fruitful to run the parts you have individually, to see what they produce. E.g. if you'd tried echo $acme2 as-is, you'd have seen it folds all the lines into one, perhaps explaining why the grep doesn't stick.
    – ilkkachu
    Commented Feb 12 at 19:42

2 Answers 2

3

We really can't help since we have no idea what $acme2 contains, but I can tell you that your grep command is looking for lines that start with _acme. Since the output of dig is multiple lines and you are echoing the variable unquoted, that will put everything on a single line, so unless the very first string in the output is _acme, your grep will match nothing. To illustrate:

$ seq 3
1
2
3

$ acme=$(seq 3)

$ echo $acme
1 2 3

$ echo "$acme"
1
2
3

$ echo $acme | grep '^2'
$

$ echo "$acme" | grep '^2'
2

So, assuming you have at least one line in your dig output that begins with _acme, correctly quoting your variable (which you should ALWAYS do, by the way) should work:

acme3=$(echo "$acme2" | grep "^_acme")

Or, better, especially for arbitrary data like this:

acme3=$(printf '%s\n' "$acme2" | grep "^_acme")                 
3
  • Or grep "^_acme" <<<$acme2 -- herestring doesn't wordsplit or glob and adds newline (except in rc-family, per Stephane C) Commented Feb 13 at 2:38
  • "We really can't help since we have no idea what $acme2 contains," –– I thought it was easy to understand, i wannto check the acme-challenge from dns / dig until all dnsservers are updated __–– i just want to see /get the acme-challenge...1st to see it, and compare with my eyes, later to compare with the first one i checked... like a fifo var... remember 1st one, and check the next, if changed, display and stop script...––– but the very1st step is to get the challenge...
    – dg1kpc
    Commented Feb 21 at 10:04
  • @dg1kpc yes, exactly. But you did not show the output of dig txt @$1 _acme-challenge.$1.de, which means we cannot know what you have in the acme2 variable. When you ask questions asking for help parsing data, it is important to show the data you want to parse. Your dig might not be the same as my dig, and even if they are, the output depends on what you are looking at. So please show us the actual content of your variables.
    – terdon
    Commented Feb 21 at 11:41
2

Both lines could be coalesced by telling dig you're only interested in its direct output:

#!/bin/sh
acme3=$(dig +short txt @"$1" "_acme-challenge.$1.de")

This also sidesteps the issue with echo $acme2, which should almost certainly have been echo "$acme2" or even printf '%s\n' "$acme2". (Omitting the double quotes causes the contents of the variable $acme2 to be split on characters of $IFS (space, tab and newline by default) and subject to glob expansion which when passed to echo likely results in the full output ending up as a single line, possibly modified beyond recognition.)

2
  • I tried to answered: --- sorry, dont work,... variable keeps empty... i copied it 1:1 ... and... dont work... ufff... i don't know... I wanna check the acme-challenge until all dns servers are updated... I thought it could be caused by different shell... and then i failed on sh... (bash and fish are also installed) ... uff... --- but a common answer is not allowed so this comment ---- Did you test my idea on sh or bash? I always get an empty var from grep...
    – dg1kpc
    Commented Feb 21 at 10:01
  • @dg1kpc it will have an empty value if you haven't set $1. You used that in your example so I assumed you'd have known how to use it in in my solution. (Min d you, I don't think your dig command would ever have returned anything. But without knowing what you're trying to do, and having an example of $1 I can't correct your code.) Commented Feb 21 at 14:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.