Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

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

like image 535
user895740 Avatar asked Dec 02 '11 10:12

user895740


People also ask

What is ${} in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is the difference between $* and $@ in shell script?

There is no difference if you do not put $* or $@ in quotes. But if you put them inside quotes (which you should, as a general good practice), then $@ will pass your parameters as separate parameters, whereas $* will just pass all params as a single parameter.

Which command is used to create read only variable in shell scripts?

Read-only Variables Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed. /bin/sh: NAME: This variable is read only.


2 Answers

From the bash manpage part about read:

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

like image 60
Michael Krelin - hacker Avatar answered Sep 28 '22 16:09

Michael Krelin - hacker


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
like image 44
Adam Zalcman Avatar answered Sep 28 '22 15:09

Adam Zalcman