Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read command doesn't wait for input

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

like image 342
mohammadh montazeri Avatar asked Apr 01 '13 13:04

mohammadh montazeri


2 Answers

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 _
like image 106
Charles Duffy Avatar answered Oct 14 '22 16:10

Charles Duffy


You can do this with echo command too!:

    echo "press  [ENTER]  to continue deleting line"
    read continue
like image 3
Pouya Avatar answered Oct 14 '22 15:10

Pouya