(a) Title:
(1) Create a folder
(2) There are 10 files in the folder that begin with any letter or number
(3) If the first digit in the beginning does not change
(4) If the first letter at the beginning is changed to uppercase
For example, the modified result
[[email protected] hushuai]# ls
06e28fe4 5b1775ab 74D1BADF cccb27b2 F3619201
3bfaa07c 6bd6b971 C4a1f7d8 db3308c4 F6403885
[Email protected] hushuai]#
The shell code is as follows:
#!/bin/bash
A= ' ls hushuai/|xargs-n1 '
echo "$a" |while read Line # This place must be enclosed in double quotes, details, without parsing spaces
Do
B= ' echo $line | Cut-c1 '
C= ' echo $line |cut-c2-'
D= ' echo $b |grep-c ' [A-z] ' # must be added [], not the same as TR
If [$d-eq 1];then
E= ' echo $b | Tr ' A-Z ' A-Z '
MV hushuai/$b $c hushuai/$e $c 2>/dev/null
Fi # MV old name New name
Done
This kind of writing seems disorganized, complicated, we can change the judgment into a function, the result of the modification is as follows:
#!/bin/bash
Swap () {b= ' echo $line | cut-c1 '
C= ' echo $line |cut-c2-'
D= ' echo $b |grep-c ' [A-z] '
}
A= ' ls hushuai/|xargs-n1 '
echo "$a" |while read line
Do
Swap
If [$d-eq 1];then
E= ' echo $b | Tr ' A-Z ' A-Z '
MV hushuai/$b $c hushuai/$e $c 2>/dev/null
Fi # If you say you executed multiple scripts, the first name has been changed.
Done will give an error, we let the errors output to null
And then we'll do an upgrade on the subject.
(1) Create a folder
(2) The folder contains 10 arbitrary 6-10-bit random numbers starting with Dingxue
(3) Replace Dingxue's first letter ding with any uppercase letter
(4) Replace the trailing 6-10-bit random numbers with equal-length lowercase letters
For example:
[[email protected] hushuai]# ls ding/
dingxue87495987 dingxue885527619 dingxue894674790 dingxue908168
dingxue87819699 dingxue8884654 dingxue8974959
dingxue8810688 dingxue89173327 dingxue903851012
The shell code is as follows:
#!/bin/bash
#for i in ' seq 10 '
#do
# b=$ ((random%4+6)) # random number Application
# a= ' Date +%n '
# c= ' echo $a | cut-c1-$b '
# Touch Dingxue$c
#done
A= ' ls ding/|xargs-n1 '
echo {A.. Z} |xargs-n1>file
D= ' Cat file | Wc-l '
echo "$a" |while read line
Do
e=$ ((random% $d + 1))
W= ' echo $line |awk-f ' [0-9] ' {print '} '
B= ' echo $line | Cut-c 1 '
f= ' Cat file | Sed-n ' $e ' P '
G= ' echo $w |sed ' s/' $b '/' $f '/' #sed, awk reference variable all add single quotes
#echo $g Specific circumstances
H= ' echo $line | Awk-f ' [A-z] ' {print $8} '
I= ' echo $h |tr ' $h ' A-Z ' # here too, be sure to pay attention to my quotes
Echo $g $i
Done
This is what I just started to write, is not looking very messy, we can change the judgment part into a function, the result of the modification is as follows:
Shell code:
#!/bin/bash
Prepare () {
A= ' ls ding/|xargs-n1 '
echo {A.. Z} |xargs-n1>file
D= ' Cat file | Wc-l '
}
Random () {
e=$ ((random% $d + 1))
}
Virable () {
W= ' echo $line |awk-f ' [0-9] ' {print '} '
B= ' echo $line | Cut-c 1 '
f= ' Cat file | Sed-n ' $e ' P '
G= ' echo $w |sed ' s/' $b '/' $f '/'
H= ' echo $line | Awk-f ' [A-z] ' {print $8} '
I= ' echo $h |tr ' $h ' A-Z '
}
Prepare
echo "$a" |while read line
Do
Random
Virable
Echo $g $i
Done
Isn't that a lot clearer?
This article from "It Life" blog, declined reprint!
Shell grooming (27) = = = Uppercase and lowercase letters replace modified file names