Reputation: 371
I have problem executing a simple script in bash. The script is like this:
#! /bin/sh
read -p 'press [ENTER] to continue deleting line'
sudo sed -ie '$d' /home/hpccuser/.profile
and when I execute the script with ./script the output is like this:
press [ENTER] to continue deleting line./script: 3: read: arg count
[sudo] password for user
I run the read command directly in terminal (copy and paste from script to terminal) and it works fine; it waits for an ENTER to be hit (just like a pause).
Upvotes: 25
Views: 24279
Reputation: 576
Try this:
read -p 'press [ENTER] to continue deleting line' < /dev/tty
Forces the input to come from the terminal.
Tested/works using bash script on UBUNTU's GNOME terminal.
Upvotes: 2
Reputation: 867
If you use pipe to redirect contents to your function/script it will run your command in a sub-shell and set stdin (0
) to a pipe, which can be checked by
$ ls -l /dev/fd/
lr-x------ 1 root root 64 May 27 14:08 0 -> pipe:[2033522138]
lrwx------ 1 root root 64 May 27 14:08 1 -> /dev/pts/11
lrwx------ 1 root root 64 May 27 14:08 2 -> /dev/pts/11
lr-x------ 1 root root 64 May 27 14:08 3 -> /proc/49806/fd
And if you called read
/readarray
/... command in that function/script, the read
command would return immediately whatever read from that pipe as the stdin has been set to that pipe rather than the tty, which explained why read
command didn't wait for input. To make read
command wait for input in such case you have to restore stdin to tty by exec 0< /dev/tty
before the call to read
command.
Upvotes: 3
Reputation:
Seems I'm late to the party, but echo -n "Your prompt" && sed 1q
does the trick on a POSIX-compliant shell.
This prints a prompt, and grabs a line from STDIN.
Alternatively, you could expand that input into a variable:
echo -n "Your prompt"
YOUR_VAR=$(sed 1q)
Upvotes: 0
Reputation: 109
You can do this with echo command too!:
echo "press [ENTER] to continue deleting line"
read continue
Upvotes: 3
Reputation: 295272
Because your script starts with #!/bin/sh
rather than #!/bin/bash
, you aren't guaranteed to have bash extensions (such as read -p
) available, and can rely only on standards-compliant functionality.
See the relevant standards document for a list of functionality guaranteed to be present in read
.
In this case, you'd probably want two lines, one doing the print, and the other doing the read:
printf 'press [ENTER] to continue deleting...'
read _
Upvotes: 32