25

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

matsjoyce
  • 5,744
  • 6
  • 31
  • 38

6 Answers6

31

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 _
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
3

You can do this with echo command too!:

    echo "press  [ENTER]  to continue deleting line"
    read continue
Pouya
  • 109
  • 1
  • 8
  • This doesn't really solve the problem if your shell is `sh` because e.g. Dash does not allow you to call `read` without the name of a variable. – tripleee Dec 03 '14 at 17:10
  • With `echo` (unless you have one that supports `-n`, which is an optional feature shells aren't required to implement), you get an extra newline this way that isn't present with the original `read -p` code. – Charles Duffy Jul 02 '17 at 19:05
1

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.

j5shi
  • 797
  • 1
  • 8
  • 21
0
read -p " Ici mon texte " continue

it works on raspbian

chr
  • 25
  • 1
0

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

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.

Jay Marm
  • 556
  • 5
  • 12