It works for me, if the while loop reads from a text file.
What is pbpaste ?
How does your input file look like? Is it Unix style? (where newline is LF, also the last line ends with LF, as opposed to DOS style where newline is CR LF and the last line has no newline)
pbpaste is an OSX command - copies from the clipboard to the terminal (stdin). I do not know if there is a direct UNIX/Linux equivalent, X11 xsel behaves the same.
pbpaste is a native command of MacOS (testing on a Mac at the moment). It pastes the contents you have in the clipboard.
If you have X, you can use this command instead
alias pbpaste='xsel --clipboard --output'
My input file is Unix style. I really don't know why this behavior is happening
EDIT: So I found out the script works correctly if the Description example has more than one line. However, if it's only one line, it doesn't. Is there any logic that I'm missing here?
Description field is not stored as a variable
Description field is stored as a variable successfully
Upon searching about this topic in the Internet, I found that the "read" command may be unable to read a line that is not terminated by a newline.
However, I could not test it in my Cygwin Bash on Win 7 setup. If I create an input file that has your email input and remove "\n" from the end of the "Description" line, the script still works.
With "printf" though, I see that read fails if the "\n" is missing:
$
$ (printf "%s\n%s\n" "the woods are lovely" "dark and deep") | while read line; do echo $line; done
the woods are lovely
dark and deep
$
$ (printf "%s\n%s" "the woods are lovely" "dark and deep") | while read line; do echo $line; done
the woods are lovely
$
$
Could you confirm if the "pbpaste" command adds a newline at the end of the "Description" line?
For example, if you type "pbpaste" on the $ prompt of the Terminal, does the next prompt occur on its own line or right after the "Description" text?
If it is the latter, then the following code change might help:
echo "$(pbpaste)" | while read line ; do
<process_line>
done
(I don't have a Mac and don't have much idea about pbpaste - this is just a hunch.)
Hi durden_tyler,
The macOS pbpaste command writes whatever was last copied into the paste buffer. If the selected text included a trailing <newline>, a trailing <newline> will be included in the output; otherwise, there will not be a line-terminating <newline> for the last output partial line.