4

I have a small script (called stc) to create a SecureToken with stoken

#!/bin/bash

Read Password

read -p "Password " -s PASS

TOKEN=$(stoken --stdin <<< "$PASS") xclip -sel c <<< "$TOKEN"

if [[ ! -t 1 ]]; then echo $TOKEN fi

In normal circumstances it copies the token to the clipboard. When there is a pipe the token should be written to stdout (this is working) but for some reason the next command in the pipe is not executed. I think this has to do with the read but have no idea how to solve this.

For example

$ stc | tee

results in

$ stc | tee
Password <token echo>
<hangs forever>
Pascal
  • 153
  • 7

1 Answers1

5

xclip forks a child process that runs in background to handle CLIPBOARD selection requests. Here, that child process will inherit the pipe and hold it open until it exits when some other process reclaims CLIPBOARD the selection. Here, just do:

#! /bin/bash -
# Read Password
IFS= read -rsp "Password: " PASS

TOKEN=$(printf %s "$PASS" | stoken --stdin) printf %s "$TOKEN" | xclip -sel c > /dev/null

[ -t 1 ] || printf '%s\n' "$TOKEN"

(you also forgot the IFS=, -r option to read, and the quotes around $TOKEN and that echo can't be used for arbitrary data).

  • Ah cool thank you! The token will be numbers only so echo should be okay but the IFS stuff I have not understood. Is this related to a password with spaces? – Pascal Sep 22 '22 at 08:54
  • 1
    @Pascal, I've added links to relevant Q&As. You almost never want to use read without setting IFS for it and without -r. If $TOKEN is numeric only and $IFS is guaranteed to contain its default value, echo $TOKEN, though still nonsensical as there's no reason you would want to invoke split+glob, would be fine, but there's also no reason not to write the correct printf '%s\n' "$TOKEN". – Stéphane Chazelas Sep 22 '22 at 08:56
  • Many thanks!! All clear to me now ;-) – Pascal Sep 22 '22 at 09:00