zsh-users
 help / color / mirror / code / Atom feed
* string overwrites string when echoed
@ 2017-09-09 12:55 Emanuel Berg
  2017-09-09 17:01 ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-09-09 12:55 UTC (permalink / raw)
  To: zsh-users

Here is a problem that really puzzles me.
I re-wrote the original DWIM function, that
branched on $#, into two function to identify
the issue. Now I know where the issue is - the
commented lines - but not why it
happens. Ideas?

TIA.

url-exists () {
    local url=$1
    curl -s -I $url | head -n 1 | cut -d\  -f2,3,4
}
alias url=url-exists

urls-exist () {
    local -a urls
    urls=($@)

    local reply
    for u in $urls; do
        reply=$(url $u)
        echo "$u $reply" # works
        echo "$reply $u" # doesn't work, $u overwrites $reply
    done
}
alias urls=urls-exist

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-09 12:55 string overwrites string when echoed Emanuel Berg
@ 2017-09-09 17:01 ` Bart Schaefer
  2017-09-10 16:35   ` Emanuel Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2017-09-09 17:01 UTC (permalink / raw)
  To: zsh-users

On Sep 9,  2:55pm, Emanuel Berg wrote:
}
}         reply=$(url $u)
}         echo "$u $reply" # works
}         echo "$reply $u" # doesn't work, $u overwrites $reply

The value of $reply has an embedded carriage-return.  You can see
this if you use:

    echo "$u ${(V)reply}"
    echo "${(V)reply} $u"


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-09 17:01 ` Bart Schaefer
@ 2017-09-10 16:35   ` Emanuel Berg
  2017-09-10 16:59     ` Peter Stephenson
  2017-09-10 21:50     ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-09-10 16:35 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote:

> The value of $reply has an embedded
> carriage-return. You can see this if you use:
>
>     echo "$u ${(V)reply}" echo "${(V)reply} $u"

echo "${(V)reply} $u" works, only how do I drop
the ^M then?

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 16:35   ` Emanuel Berg
@ 2017-09-10 16:59     ` Peter Stephenson
  2017-09-10 18:23       ` Emanuel Berg
  2017-09-10 21:50     ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2017-09-10 16:59 UTC (permalink / raw)
  To: zsh-users

On Sun, 10 Sep 2017 18:35:25 +0200
Emanuel Berg <moasen@zoho.com> wrote:
> Bart Schaefer wrote:
> 
> > The value of $reply has an embedded
> > carriage-return. You can see this if you use:
> >
> >     echo "$u ${(V)reply}" echo "${(V)reply} $u"
> 
> echo "${(V)reply} $u" works, only how do I drop
> the ^M then?

Replacing $reply with ${reply%%$'\r'} would do the trick.

There may be something you can do with stty, but that's
a bit out of the scope of the shell (or indeed sanity :-().

pws


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 16:59     ` Peter Stephenson
@ 2017-09-10 18:23       ` Emanuel Berg
  2017-09-10 18:57         ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg @ 2017-09-10 18:23 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:

> Replacing $reply with ${reply%%$'\r'} would
> do the trick.
>
> There may be something you can do with stty,
> but that's a bit out of the scope of the
> shell (or indeed sanity :-().

Ha!

Yes, it works. Thank you!

Feel free to elaborate on ${reply%%$'\r'}.
What does it do? Remove the last char of the
string if it matches the char given? I take it
the $ is because of the special char single
quotes. I get the same result with only one %
by the way.

Does this look good?

url-exists () {
    local url=$1
    curl -s -I $url | head -n 1 | cut -d\  -f2,3,4
}

urls-exist () {
    local -a urls
    urls=($@)

    local pad_max=$(echo "404 Not Found" | wc -m)

    local reply
    for u in $urls; do
        reply=$(url-exists $u)
        echo ${(r:$pad_max:: :)reply%%$'\r'} $u
    done
}
alias url=urls-exist

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 18:23       ` Emanuel Berg
@ 2017-09-10 18:57         ` Peter Stephenson
  2017-09-10 19:01           ` Emanuel Berg
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2017-09-10 18:57 UTC (permalink / raw)
  To: zsh-users

On Sun, 10 Sep 2017 20:23:08 +0200
Emanuel Berg <moasen@zoho.com> wrote:
> Feel free to elaborate on ${reply%%$'\r'}.
> What does it do? Remove the last char of the
> string if it matches the char given?

%% is a standard pattern match operator (works in most shells) that
removes the longest possible match of the following pattern.  % is
similar but removes the shortest possible match: I used %% because they
do the same thing in this case (it's a single fixed character) and I
happen to know the longest match is generally more efficient to
evaluate.  This is documented in the PARAMETER EXPANSION section of the
zshexpn manual.

$'...' is an increasingly common form of quoting that allows escape
sequences like the print builtin, so $'\r' exands to a carriage return.
This is documented in the section QUOTING in the zshmisc manual page
(except it doesn't say much more than what I did before you have to go
away and read the entry for the print builtin).

pws


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 18:57         ` Peter Stephenson
@ 2017-09-10 19:01           ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-09-10 19:01 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:

> %% is a standard pattern match operator
> (works in most shells) that removes the
> longest possible match of the following
> pattern. % is similar but removes the
> shortest possible match: I used %% because
> they do the same thing in this case (it's
> a single fixed character) and I happen to
> know the longest match is generally more
> efficient to evaluate. This is documented in
> the PARAMETER EXPANSION section of the
> zshexpn manual.
>
> $'...' is an increasingly common form of
> quoting that allows escape sequences like the
> print builtin, so $'\r' exands to a carriage
> return. This is documented in the section
> QUOTING in the zshmisc manual page (except it
> doesn't say much more than what I did before
> you have to go away and read the entry for
> the print builtin).

Okay cool, thanks a lot.

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 16:35   ` Emanuel Berg
  2017-09-10 16:59     ` Peter Stephenson
@ 2017-09-10 21:50     ` Bart Schaefer
  2017-09-10 22:11       ` Emanuel Berg
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2017-09-10 21:50 UTC (permalink / raw)
  To: Emanuel Berg, zsh-users

On Sep 10,  6:35pm, Emanuel Berg wrote:
}
} > The value of $reply has an embedded
} > carriage-return.
} 
} echo "${(V)reply} $u" works, only how do I drop
} the ^M then?

There have been several reasonable replies to this, but it might be
useful to point out that the ^M is there in the first place because
curl is emitting it.  A quick glance through the curl documentation
doesn't reveal a way to prevent that.  You could strip it off when
postprocessing the curl output in your url-exists function, which
might prevent confusion if you ever use that separately from the
urls-exist wrapper.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: string overwrites string when echoed
  2017-09-10 21:50     ` Bart Schaefer
@ 2017-09-10 22:11       ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2017-09-10 22:11 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote:

> There have been several reasonable replies to
> this, but it might be useful to point out
> that the ^M is there in the first place
> because curl is emitting it. A quick glance
> through the curl documentation doesn't reveal
> a way to prevent that. You could strip it off
> when postprocessing the curl output in your
> url-exists function, which might prevent
> confusion if you ever use that separately
> from the urls-exist wrapper.

Right, thank you, the original idea was to have
a single function but I did them separately to
track down the error. Now I will merge them
into a single DWIM function that doesn't output
the URL if a single URL is provided, however if
two or more are, the URLs will be to tell the
response codes apart.

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-09-10 22:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-09 12:55 string overwrites string when echoed Emanuel Berg
2017-09-09 17:01 ` Bart Schaefer
2017-09-10 16:35   ` Emanuel Berg
2017-09-10 16:59     ` Peter Stephenson
2017-09-10 18:23       ` Emanuel Berg
2017-09-10 18:57         ` Peter Stephenson
2017-09-10 19:01           ` Emanuel Berg
2017-09-10 21:50     ` Bart Schaefer
2017-09-10 22:11       ` Emanuel Berg

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).