0
#!/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.

2
  • 3
    read reads from standard input, and it blocks until it receives that input; so by "ends", do you mean "hangs"? Commented Nov 8, 2012 at 16:17
  • 1
    Are you trying to act on parameters passed to the script like so? - yourscript.sh HOST_NAME IP OID OID Commented Nov 8, 2012 at 16:18

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that explained where I was messing up in the CLI for passing parameters.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.