user895740
user895740

Reputation: 73

How i can make read doesn't show its value in Shell Scripting?

Hello guys i'm using ubuntu 11, and I'm doing some shell scripting with it. the only problem i face it right now is "read" show it value, i don't know how it happening with mE cuz this the 1st. time i use "read" and i can see the value of it !!!! any way here is my codes:

#!/bin/bash

echo -n "Which file you want 2 store: "
read fname
echo -n "Do u want 2 delete file $fname in its current location? [Y\N]: "
read ansr

sudo tar -c -v -f The_Store.tar $fname
sudo chmod 700 The_Store.tar

if [ "$ansr" = "Y" ]; then
rm $fname
fi

echo "Congrats, ur file been stored"

the value of "fname" shows up after the user answer the Q: Do u want 2 delete file $fname in its current location?. Could any one help mE with this.

All what i want is to keep the value of "fname" hiding ..

Upvotes: 5

Views: 6668

Answers (2)

Adam Zalcman
Adam Zalcman

Reputation: 27233

In case your shell's read buildin does not support -s option you can also use stty command:

echo "Which file you want 2 store: "
stty -echo
read fname
stty echo

echo "Do u want 2 delete file $fname in its current location? [Y\N]: "
stty -echo
read ansr
stty echo

Upvotes: 4

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143051

From the bash manpage part about read:

-s Silent mode. If input is coming from a terminal, characters are not echoed.

Upvotes: 8

Related Questions