linux shell read command-Getting User Input Via Keyboard--re
ref:http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard You can accept input from the keyboard and assign an input value to a user defined shell variable using?. Contents
variable1 variable2 variableN
Where,
Create a script called greet.sh as follows:
name
. Let us be friends!"
Save and close the file. Run it as follows: +x greet.sh
.greet.sh
Sample Outputs: Enter your name?: Vivek Gite Hi,Vivek Gite. Let us be friends! Try the following examples.
n1
n2
n3
?
"
"
"
A shell script to display the Internet domain name owner information (domain.sh):
domain_name
You can time out read command using the -t option. It causes read to time out and return failure if a complete line of input is not read within TIMEOUT seconds. For example,if no input provided within 10 second,program will be aborted (domain2.sh):
domain_name
The -s option causes input coming from a terminal do not be displayed on the screen. This is useful for password handling (readpass.sh):
my_password
"
Consider the following example:
"
Sample outputs: Enter directory to delete?: foo bar /tmp/data foo bar /tmp/data The user supplied three values instead of one. The string is now made of three different fields. All three words are assigned to dirname using??internal field separator. The??determines how shell recognizes fields. To display default value of?,enter: "
You will see a whitespace which is nothing but a space,a tab,and a newline (default). You can print actual values of IFS using the following command (see?): "
Sample outputs: ^I$ $ Where,
Create a variable called nameservers and give it total 3 values as follows (note all values are separated by a whitespace): =
Display the value of a variable nameservers with??or?: "
OR Now,you can simply split $nameservers using the??as follows (see?): ns1 ns2 ns3 "
Where,
"
"
"
OR use the? #2?%s #3?%s"
Sample outputs: DNS Server #1 ns1.nixcraft.net #2 ns2.nixcraft.net #3 ns3.nixcraft.net Consider the following??line: gitevivek:x:1002:1002::/home/gitevivek:/bin/sh Assign the above line to a variable called pwd: =
Save the Internal Field Separator to a variable called old: ="
Set the Internal Field Separator to a colon (i.e. change the Internal Field Separator): =:
Read $pwd and generate tokens using $IFS and store them into respective fields: password uid gid info home shell "
"
Sample outputs: Your login name is gitevivek,uid 1002,gid 1002,home dir set to /home/gitevivek with /bin/sh as login shell Finally,restore the Internal Field Separator value using $old: ="
Where,
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |