×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

bash read Frustration

bash read Frustration

bash read Frustration

(OP)
Ok, I'm mostly comfortable with the Korn shell. Since I'm doing some stuff on Linux now I'm trying to use bash a bit more. I'm finding that the 'read' command seems broken.

This test script in Korn shell works great...

CODE --> test.ksh

#!/bin/ksh

echo $SSH_CONNECTION | read FROM_IP FROM_PORT TO_IP TO_PORT

echo "SSH_CONNECTION = '${SSH_CONNECTION}'"
echo "From = ${FROM_IP}:${FROM_PORT}"
echo "  To = ${TO_IP}:${TO_PORT}" 

CODE --> Output

[bones@flea ~]$ ./test.ksh
SSH_CONNECTION = '10.8.24.105 58993 10.8.24.54 22'
From = 10.8.24.105:58993
  To = 10.8.24.54:22 

Everything working as expected.

Same code in bash, not so much...

CODE --> test.bash

#!/bin/bash

echo $SSH_CONNECTION | builtin read FROM_IP FROM_PORT TO_IP TO_PORT

echo "SSH_CONNECTION = '${SSH_CONNECTION}'"
echo "From = ${FROM_IP}:${FROM_PORT}"
echo "  To = ${TO_IP}:${TO_PORT}" 

CODE --> Output

[bones@flea ~]$ ./test.bash
SSH_CONNECTION = '10.8.24.105 58993 10.8.24.54 22'
From = :
  To = : 

I did find online that even though read is a shell builtin command, bash runs it as a subprocess if used in a pipe, thus no variables defined in the parent process. Uh, that effectively breaks it.

I did find that there was a /usr/bin/read that looked like this...

CODE --> /usr/bin/read

#!/bin/sh
builtin read "$@" 

So I renamed that out of the way, so it's no longer there...

CODE

[bones@flea ~]$ which read
/usr/bin/which: no read in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/mssql-tools/bin) 

The above scripts run exactly the same, ksh as expected, bash broken.

I do have workarounds, but I like this construct. Piping to a read to parse a line is very clean and easy to understand and support. Playing games with awk or sed to parse a line is, well, stupid.

So, all you bash aficionados, am I missing something, or should I stick with the Korn shell?


RE: bash read Frustration

Hi

Use a here-string :

CODE --> Bash

builtin read FROM_IP FROM_PORT TO_IP TO_PORT <<< "$SSH_CONNECTION" 
Though did not got why you use the builtin keyword.

Feherke.
feherke.github.io

RE: bash read Frustration

(OP)
Yeah, with or without "builtin", it still doesn't work as it should. I was just trying that because the /usr/bin/read version had it.

CODE --> /usr/bin/read

#!/bin/sh
builtin read "$@" 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close