Learning cycle, see a lot of for,while, or until, and select with less, here is a little summary of the next select this cycle.
Basic structure:
Select VALUE in LIST
Do
COMMAND
Done
And the reality out of the structure is probably a number before the value, the choice of the number, is the value of the $reply, here do not know nothing, you can continue to see the following program. The $PS 3 variable is equivalent to the shell's default prompt, which is empty by default and can be used with select.
#!/bin/bash#save $PS 3ops3= $PS 3ps3= "Please select favorite fruit Q exit:" Select fruit in pear Apple banana others quitdo if [!-Z "$frui T "]; Then ["$fruit" = = "Quit"] && Exit 0 | | echo "You choosed $REPLY and $fruit" Else echo "wrong argv $REPLY" Fidon E#restore $PS 3ps3= $oPS 3
Execution results
[[email protected] shell]#/select.sh 1) pear2) apple3) banana4) quit Please select favorite fruit Q exit: 1you others5 1 and you like pea R Please select the fruit you like Q exit: 6wrong argv 6 Please select favorite Fruit Q exit: 5
Then write a program, by the way, the usual string to the number of operations, their own writing, a bit lame, the program to note that some places can not have spaces such as let, and expr must have a space, in order to insure, I used a single quotation mark
#!/bin/bash#save ps3ops3= $PS 3ps3= "Choose the way:" function way1 () {echo "You are using () to add "tmp1=$ ((NU1 + NU2)) echo " value is $tmp 1 "}function way2 () {echo " You are using expr to add "echo " Value is ' expr ${nu1} + ${nu2} ' "}function way3 () {echo " You are using let to add "let tmp2=${nu1}+${nu2}echo " value is $tmp 2 "}read -p " input two to test: " nu1 nu2select cal in " (()) ' ' expr ' ' let ' ' Q ' do case $cal in ' (()) ') way1;; ' expr ') way2;; ' let ') way3;; ' q ') exit 0;; * ) echo "WRONG ARGV";; esacdone#restore ps3ps3= $oPS 3
Execution results are
[e-mail protected] shell]#./select2.sh please input (test:2) (()) 2) Expr3 Let4) Qchoose the way:3you is using Let to AddValue was 5choose the way:1you is using () to AddValue is 5choose the way:2you be using expr to AddValue is 5ch Oose the Way:4
Nothing to do with this time.
Suddenly want to write a script like this
Say hello to every non-system user, user name, I know you is form user group name
My idea is that the shell can't be/sbin/nologin, the home directory can't be/sbin my files.
Well, I'll admit it's even more lame, and take a look at the detailed awk command.
#!/bin/bash#change ps2tmp= ' cat/etc/passwd ' cat/etc/passwd 2>/dev/null|grep-v/sbin/nologin|grep-v/sbin|awk-f ":" ' {print ' Hello, ' $ ', I know you is from ' $ $ '. '} '
The result of the execution is:
[Email protected] shell]#/while.sh hello,root,i know you is from root.hello,liuliancao,i know.
I'll learn about awk another day and change it.
Here's a book on tips for writing variables:
Abbreviated as follows
A) constant, string, file name recommended capitalization
b) numbers and other data variables recommended lowercase
c) The name of the essence is not good or bad, it is important to always maintain a style, as if this I have to continue to work hard ...
This article is from the "Learning Path of Kai Learning" blog, please make sure to keep this source http://qixue.blog.51cto.com/7213178/1656117
Shell Learning Select cycle