Last line in while do function being ignored

Hello everyone,

I'm practically new to scripting (first post here), so I'm having just a slight issue running the code snippet below correctly.

Here's the flow: An email is received such as this one:

Then copy its contents and run the script below:

read -p "Ticket ID:" ticketidvar

while read line ; do

     case "$line" in

	"Your name:"*)   uservar="${line#*: }" ;;
	"Email:"*)       emailvar="${line#*: }" ;;
	"Subject:"*)     subject="${line#*: }" ;;
	"Description:"*) description="${line#*: }" ;;

     esac
	
done < <( pbpaste )

echo ""
echo "Details:"
echo ""
echo "Name:" $uservar
echo "E-mail:" $emailvar
echo "Subject:" $subject
echo "Description:" $description
echo "Ticket ID:" $ticketidvar

What happens here is that I manually enter the ticket id (unrelated to this) and then the remaining fields are stored as variables.

The issue here is that the Description field is not being saved as a variable. It doesn't display anything at all.

All fields are stored as variables, except this one.

Can you guys help, please?
Thank you :slight_smile:

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.

Hi there! Thanks for replying.

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.)

1 Like

The following seems to work with bash on macOS High Sierra (version 10.13.4):

while IFS= read -r line
do	case "$line" in
		"Your name:"*)   uservar="${line#*: }" ;;
		"Email:"*)       emailvar="${line#*: }" ;;
		"Subject:"*)     subject="${line#*: }" ;;
		"Description:"*) description="${line#*: }" ;;
	esac
done <<-EOF
	$(pbpaste)
EOF
printf 'description="%s"\nemailvar="%s"\nsubject="%s"\nuservar="%s"\n' \
	"$description" "$emailvar" "$subject" "$uservar"

If you were using ksh instead of bash , the code suggested by durden_tyler in post #5 would also work.

1 Like

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.

2 Likes

Hi durden_tyler and Don Cragun,

Thank you so much for your additional input on this matter.
Since Don explained what pbpaste is already, there's no need for me to explain :slight_smile:

I used Don Cragun's example and I was able to get what I wanted correctly. Thank you, everyone, who contributed here!