(a) Title: Accept an input, if the uppercase letter is converted into lowercase letters, lowercase letters to uppercase.
For example:
[Email protected] hushuai]# bash 4.sh
Input:dingxue
Dingxue
[Email protected] hushuai]#
Method (1)
The shell code is as follows:
#!/bin/bash
Read-p "Input:" DD
echo $DD | TR ' [a-z][a-z] ' [a-z][a-z] '
~
Method (2)
The shell code is as follows:
#!/bin/bash
Read-p "Input word:" Word
[-Z $word] && echo "\033[32M input Word!!! \033[0m "
a=${#word}
For i in ' seq $a '
Do
B= ' echo $word | Cut-c$i '
C= ' echo $b |tr ' A-Z ' 1 '
If ["$c" = 1];then
E= ' echo $b |tr ' A-Z ' A-Z '
Else
E= ' echo $b |tr ' A-Z ' A-Z '
Fi
Echo-n $e
Done
Echo
But there are two different ways to do it, such as:
[Email protected] hushuai]# bash 3.sh
Input word:ding XUE
3.sh:line 4: [: ding:binary operator expected # error; it's because I've got one more space in between.
Dingxue
[Email protected] hushuai]# bash 3.sh
Input Word:dingxue # So it's all right.
Dingxue
[Email protected] hushuai]#
At this time we can broaden our mind, if we get rid of the problem
(1) Allow the user to enter any character, including spaces
(2) If it is not a letter, return the input value, do not make any changes
(3) If uppercase is converted to lowercase, lowercase to uppercase
For example:
[Email protected] hushuai]# bash 7.sh
Plz Input something:ding%s js
DIng%s JS
[Email protected] hushuai]#
A very good idea.
The shell code is as follows:
!/bin/bash
Swap () {
A= ' echo $ | Grep-c ' [A-z] ' # The parameters passed in can be filtered to any of [a-z]
B= ' echo $1| Grep-c ' [A-z] ' letter, definitely on one line, so the value of a and B is not 0
If [$a-eq 1];then is 1
Echo-n $ | Tr ' A-Z ' A-Z
elif [$b-eq 1];then # in the loop each time you do a change of line
Echo-n $ | Tr ' A-Z ' A-Z '-n ensures that the loop does not
else line break
Echo-n "$"
Fi
}
Read-p "Plz Input Something:" String
For i in ' seq ${#string} ' # Number of input strings, including spaces
Do
Xxoo= ' echo $string |cut-c $i '
Swap "$xxoo" # must be double quotation marks, otherwise the space is not resolved, the details of the problem
Done
Echo
Title (ii): Application of a simple random number
After executing the script from the 1-100 count, randomly pops an odd number.
The shell script code is as follows:
#!/bin/bash
For i in ' SEQ 100 '
Do
If [$ (i%2))-ne 0];then
echo $i >>123
Fi
Done
A= ' cat 123 |wc-l '
b=$ ((random% $a + 1))
Cat 123 | Sed-n ' $b ' P '
This principle is very simple, after seeing this we can also make a point roster, a list of many students in a list of names in a row, such as executing a script, random pop-up name.
My summary of the above script, personal experience:
(1) TR replacement problem:
[Email protected] hushuai]# echo ABCDSDDSFF | TR ' [a-za-z] ' [a-za-z] '
Abcdsddsff
[Email protected] hushuai]#
This can be done, but you must pay attention to the position, and do not add brackets, such as
[Email protected] hushuai]# echo ABCDSDDSFF | Tr ' a-za-z ' a-za-z '
Abcdsddsff
[Email protected] hushuai]#
But you can't write like that, and that's stupid.
[Email protected] hushuai]# echo ABCDSDDSFF | Tr ' a-za-z ' a-za-z '
Abcdsddsff
[Email protected] hushuai]#
And if you want to replace enough, what will happen, will be the last number or letter to be filled with = = For example:
[Email protected] hushuai]# echo {A.. Z} | Tr ' A-Z ' 0-9 '
0 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
TR also has the often used option is tr-s compression tr-d Delete
For example: Do you see the effect? I've deleted all the blanks.
[Email protected] hushuai]# echo 123 456 | Tr-d "
123456
[Email protected] hushuai]# echo 123 456 | Tr-d ' 6 ' # Remove spaces and 6
12345
[Email protected] hushuai]# echo 123 456 | Tr-d ' 6 ' # delete only 6 do not remove spaces
123 45
[Email protected] hushuai]# echo 123 456 | Tr-d ' [6] ' # can also be written like this
12345
[Email protected] hushuai]#
For example: So, what if there are multiple lines in the text?
"[email protected] hushuai]# Cat 000
123 QWD
456 DSJ
[email protected] hushuai]# Cat 000 | cut-c-10 | Tr-d ' # to remove every line of space, so to speak
123QWD Delete is for each line in the
456dsj
[Email protected] hushuai]#
Tr-s is for a row to another line.
For example:
[[email protected] hushuai]# Cat 000
123 QWD
# There's a blank line #
O
456 DSJ
[email protected] hushuai]# Cat 000 | cut-c-10 | tr-d ' |tr-s ' \ n '
123QWD tr-d: Delete every line of space, but each row has a newline
o character, compressed in line break
456dsj
[Email protected] hushuai]#
With this we can take out the name of the network card inside the machine: for example
[Email protected] hushuai]# Ifconfig |cut-c-10 | tr-d ' |tr-s ' \ n '
Eth0
Eth1
Lo
[Email protected] hushuai]#
(3) Grep-c grep is read in a row, statistics filtered to how many rows, not how many
For example:
[Email protected] hushuai]# echo 123 | Grep-c ' [0-9] '
1
[Email protected] hushuai]# echo 123 | Grep-c ' 0-9 ' # must be enclosed in single quotes, brackets, and single quotes
0
[Email protected] hushuai]# echo 123 | grep ' [0-9] '
123
[Email protected] hushuai]# echo 123 | Grep-o ' [0-9] ' # so we can see [0-9] that
1 matches any one of the letters inside the brackets.
2 [^0-9] This means to take the inverse
3 ^[0-9] Start with any one of the 0-9 letters
This article from "It Life" blog, declined reprint!
Shell finishing (26) = = = Uppercase and lowercase letters replace and randomly take out an odd number