Shell script exercise - file related

Get file name without extension

use the % operator

[root@wenzi ~]#vim example.sh
#!/bin/bash
file_name="abc.txt"
name=${file_name%.*}
echo $name
[root@wenzi ~]#./example.sh 
abc

illustrate:

${variable%keyword}: If the data from the end of the variable matches the keyword, delete the shortest matching data.

file_name is abc.txt ; * is a wildcard, matching 0 or infinite arbitrary characters, so .* matches .txt , so delete the matching result from abc.txt to get abc

Get the file extension

Use the # operator

[root@wenzi ~]#vim example.sh
#!/bin/bash
file_name="abc.txt"
extension=${file_name#*.}
echo $extension
[root@wenzi ~]#bash example.sh 
txt

illustrate:

${variable#keyword}: If the data from the beginning of the variable matches the keyword, delete the shortest matching data

* is a wildcard, matching 0 or infinite arbitrary characters, so *. matches abc. , so delete the matching result from abc.txt to get txt

backup file

[root@wenzi data]#vim backup.sh 
#!/bin/bash
echo -e "\033[1;32mStarting backup...\033[0m"
sleep 2
cp -av /etc/ /data/etc_`date +%F`/
echo -e "\033[1;32mBackup is finished\033[0m"

Execute scripts on remote hosts

#主机192.168.29.142
[root@wenzi ~]#yum -y install httpd
[root@wenzi ~]#vim /var/www/html/hello.sh
#!/bin/bash
echo "hello world"
[root@wenzi ~]#systemctl start httpd

#主机192.168.29.141
[root@wenzi data]#curl http://192.168.29.142/hello.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    31  100    31    0     0  31000      0 --:--:-- --:--:-- --:--:-- 31000
hello world
[root@wenzi data]#curl -s http://192.168.29.142/hello.sh | bash
hello world

Safe Implementation of the rm Command

[root@wenzi data]#vim rm.sh 
#!/bin/bash
warning_color="echo -e \E[1;31m"
end="\E[0m"
dir=/tmp/`date +%F%T`
mkdir $dir
mv $* $dir
${warning_color}Move $* to $dir $end
[root@wenzi data]#chmod 777 /data/rm.sh 
[root@wenzi data]#alias rm='/data/rm.sh'
[root@wenzi data]#touch {1..3}.txt
[root@wenzi data]#ll
total 4
-rw-r--r-- 1 root root   0 Jul 22 10:43 1.txt
-rw-r--r-- 1 root root   0 Jul 22 10:43 2.txt
-rw-r--r-- 1 root root   0 Jul 22 10:43 3.txt
-rw-r--r-- 1 root root 138 Jul 22 10:42 rm.sh
[root@wenzi data]#rm *.txt
Move 1.txt 2.txt 3.txt to /tmp/2023-07-2210:45:20 

illustrate:

$*: Position variable, representing all parameters except $0 (the name of the script itself), as above, namely "opt1 opt2 opt3 opt4", each variable is separated by a space, sharing a double quotation mark

Create users in batches and set random passwords

[root@wenzi data]#cat batchCreateUser.sh 
#!/bin/bash
userNum=$1

for ((i=1;i<=${userNum};i+=1))
do
	useradd wenzi$i
	pwd=`cat /dev/urandom | tr -d -c '[:alnum:]' | head -c 12`
	echo $pwd | passwd --stdin wenzi$i &> /dev/null
	echo wenzi$i:$pwd >> /data/user.log
	echo "wenzi$i is created"
done
[root@wenzi data]#bash batchCreateUser.sh 5
wenzi1 is created
wenzi2 is created
wenzi3 is created
wenzi4 is created
wenzi5 is created
[root@wenzi data]#cat /data/user.log 
wenzi1:aBfx6xKXXWXd
wenzi2:zeiXcSdeECga
wenzi3:XXWhxovUy9o1
wenzi4:mk6wF4gqQxbP
wenzi5:YvcHWo6UYQcP

#删除生成的用户
[root@wenzi data]#for i in {1..5};do userdel -r wenzi$i;done

illustrate:

/dev/urandom is a random number generator

[:alnum:] represents English uppercase and lowercase characters and numbers, namely 0-9, AZ, az

tr's -d option: delete the specified content in the information; -c option: replace the string with the complement of the specified string; so tr deletes the content that is not '[:alnum:]'

Change the suffix of all files in the specified directory to bak

Continually updated

Guess you like

Origin blog.csdn.net/qq_40875048/article/details/131711237