custom prompt for the select loop inside a shell script, using the PS3 environment variable, as explained below.
Shell script and output without PS3:
Ramesh @ Dev-DB ~> Cat ps3.shselect I in Mon Tue wed exitdo case $ I in Mon
Use case statements in Shell programming, and use case statements in shell programming
Hello everyone, today we bring you the use of Shell programming case statements!
In fact, the case
: ========================
Copy Code code as follows:
SH testwhile1.sh
10
9
8
7
6
Instance 2: (Looping through the contents of the file:)
Copy Code code as follows:
#!/bin/sh
While Read Line;doEcho $line;Done
Run Result: ===================
Copy Code code as follows:
SH testwhile2.sh
# do don't remove the following line, or various programs# that require network functionality would fail.127.0.0.1 centos5 localhost.l
In the shell script, you find a problem with the case statement.This method of specifying lowercase letters [A-z] and uppercase letters [A-z] is no use.
The following conditions occur:
Copy Code code as follows:
[Root@station1 ~]# Cat case.sh
#!/bin/bash
While:
Todo
Echo-n "Input a Letter:"
Read Var
Case "$var" in
[A-z] echo "lower
Tags: Shell linux ops script caseIn general, case is a judgment statement that is easier to understand than if.Case Statement Formatcase in 变量 值1) 内容 ;;值2) 内容 ;;esacNote: Each content needs to be added after;; , can be written across rows or peers.
Instance: Executes the statement based on the selection entered by the user.
#!/bin/bash -# 打印选择菜单cat [Email protected] scripts]# bash case.s
Case statements are used to implement multiple if. else, but the usage is somewhat different, people who have learned other languages know that the case to match the variables, if there is a matching successful words to execute the corresponding statement. The case statement in Shell programming is also this meaning, c
The Shell Case statement is a multiple-selection statement. You can use a case statement to match a value with a pattern, and if the match succeeds, execute the matching command. The case statement is formatted as follows:
Copy Code code as follows:
Case val
Example of a bash case statement.
Sharing a bash shell code is helpful in understanding the use of case statements by students who learn about bash.
Example:
Copy Code code as follows:
#!/bin/bash
##
# program:
# File operation
# 1.) Open file 2.) Display file 3.) Edit file 4.) Delete file
# Site:WWW.JB51.NET
Path=/bin:/sbin:/usr/bi
Most people will change it when they use Bash, because the default settings are really hard to use.
Refer to the following ~/.INPUTRC settings:
Copy Code code as follows:
# don't show hidden files in the list
Set Match-hidden-files off
# Auto Complete Ignoring case
Set show-all-if-ambiguous on
Set Completion-ignore-case on
"\ep": History-search-backward
"\e[a": History-search-backward
"
Tags: tmp One mode add error file Erro script case. ShLogical judgments in shell scripts
Format 1:if condition; Then statement; Fi (Common)
#以命令的方式表达[[emailprotected] ~]# for i in `seq 1 5`; do echo $i; done12345[[emailprotected] ~]# for i in `seq 1 5`> do> echo $i> done12345[[emailprotected] ~]# a=5[[emailprotected] ~]# if [ $a -gt 3 ]> then> echo ok> fiok[[emailprotected] ~]# if [ $a -gt 3 ]
2.2.6.1, Case statements(1) The case statement in the shell acts in the same way as the switch case statement in the C language, with a different format(2) The case statement in the shell is inherently free of break and does not r
'] means that the value of variable A is not empty
[[emailprotected] shell]# if [ -n if.sh ]; then echo ok;fiok#可以判断一个文件是否为空[[emailprotected] shell]# if [ -n "$b" ]; then echo $b;else echo "b is null";fib is null
If Grep-q ' 123 ' 1.txt;then indicates what happens if the 1.txt contains ' 123 ' Rows
[[emailprotected] shell]# if grep -w ‘user‘ /
negation
**
move left
>>
move right
bit Boolean and
|
bit Boolean or
Logic and
||
Logical OR
The role of various parentheses in the shell (), (()), [], [[]], {}: The difference between $ (()) and $ (), ${} in the
the corresponding response. The above method is directly issued. If interactive mode is used, remove the "#" of the 10th and 11 rows above and add the 12 rows with comments (#)
Practice:
1 #! /Bin/bash234 echo "practice to use" one "" two "" three ""5 read-P "input your choise (from the up suggest):" answer67 case "$ answer" in8 "one ")9 echo "your input is 'one', that is $ answer"10 ;;11 "two ")12 echo "your input is 'two', that is $ answer"13 ;;14
. implementation of the case
Case: Throwing interface dat files from another system and storing them in/home/applvis/bts/interface, controlling file reads via UNIX shellStep1. Establish control table to test the Unix shell to store the unread file name in the table1 Create TableBXJ_INTERFACE_CONTROL_TB (2batch_id Number,3File_seq Number,4 file_name varchar2
Tags: case shell ifCase Base Syntax:Format case?? Variable name invalue1)Command;;value2)Command;;*)Commond;;EsacIn a case program, you can use the |, meaning, or means of a condition, such as???2|3)Command;;Script[[emailprotected] shell]# vim case.sh #!/bin/bashread -p "Ple
directories exist[-R File] to determine if the document is readable[-W file] Determines whether the file is writable[-X file] Determines whether the file is executableDetermine if the Lx.txt file exists in the/tmp directory, if any, the output is not created#!/bin/bashf="/tmp/lx.txt"if [ -f $f ]then echo $f existelse touch $ffi
If special usageIf [-Z ' $a '] This indicates what happens when the value of variable A is empty#!/bin/bashn=‘wc -l /tmp/test.txt‘if [ -z "$n" ]then echo
Case commands in shell in Linux (regular break, unconditional continuation, and conditional continuation)
In linux, the case name in shell is similar to the switch in C/C ++, but the case in shell is more powerful and complex.
1.
script prompts you to enter 1 to 4 to match each pattern:#!/bin/BashEcho 'Input A number between 1 to 4'Echo-E'Your number is:\c'Read Anum Case$aNuminch 1)Echo 'You select 1' ;; 2)Echo 'You select 2' ;; 3)Echo 'You select 3' ;; 4)Echo 'You Select 4' ;; *)Echo 'You does not select a number between 1 to 4' ;;Esac:EOF I just want to think about Java, if it's 1, do 1 2, but no. Case$aNuminch
. Otherwise, execute action.
Instance (testuntil. Sh ):
#! /Bin/sh
A = 10;
Until [[$ A-lt 0]; do
Echo $;
(-));
Done;
Result:
Sh testuntil. Sh
109876543210
Iii. Shell selection statement (case and select usage)
Use Case/esac)
Syntax structure
Case $ ARG inPattern | sample) # ARG in pattern or sample;;Pa
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.