Ian Dubya
Ian Dubya

Reputation: 181

Bash script, read ends the script?

#!/bin/sh
echo "one"
read host
echo "two"
read ip
echo "three"
read oid
read oid
echo $oid $host >> logger.txt

it never makes it to echoing "two"

No matter if I pass parameters (this is to receive SNMP traps, and the parameters come) manually in any varied way.

EDIT: This has permissions etc etc, I am testing it by launching it manually, "one" is echoed.

Upvotes: 0

Views: 128

Answers (2)

Alessandro Pezzato
Alessandro Pezzato

Reputation: 8802

read waits for input from STDIN.

If you do not insert any input by hand (in a interactive terminal) or you do not provide any input from STDIN like this:

echo -e "my_host\n192.168.1.100\nfoo\nbar" | ./myscript

it will hang waiting for input

In the example \n is a newline.

If you want to access parameters, do not use read, but the $1...$n variable.

./myscript my_host 192.168.1.100 foo

You need this:

#!/bin/sh
host=$1
ip=$2
oid=$3
echo $oid $host >> logger.txt

Upvotes: 4

Vijay
Vijay

Reputation: 67221

read actually waits for user input. read host will wait for a user to enter any data till he presses the return key and what ever the data user enters will be stored in host.

Upvotes: 0

Related Questions