Jahid
Jahid

Reputation: 22438

prompt for input in a root session in a shell script

I am trying to read input with read command within superuser privilege:

sudo read p

returns error:

sudo: read: command not found

And in the following:

sudo -s <<EOF
read p
EOF

read is being skipped.

So, How can I use read in a root session in a shell script or is there any alternatives? A little insight about why the above doesn't work would be appreciated.

EDIT:

I want the input to be stored in the variable p to be used later. Basically I want to do this inside the heredoc, i.e p should be usable inside the heredoc, outside the heredoc it's of no concern.

Upvotes: 1

Views: 358

Answers (2)

anishsane
anishsane

Reputation: 20980

This could help:

$ my_tty=`tty`

$ sudo -s <<EOF
read -p "Some prompt: " p < $my_tty
echo "You supplied '\$p'"... #use $p
EOF

#Output
Some prompt: somevalue
You supplied 'somevalue'...

However, as I already said, this $p value is not visible outside sudo.

You could have rather used it like:

$ read -p "Some prompt: " p
$ sudo -s <<EOF
echo "You supplied '$p'"... #use $p
EOF

Upvotes: 3

Mahesh Kharvi
Mahesh Kharvi

Reputation: 399

sudo -s read p

You can't use sudo for shell built-in commands since sudo doesn't create shell

Upvotes: 2

Related Questions