The shell creates text, renames the file, batch converts the encoding format, and redirects to markdown

1. Text content to be created:

b.txt.bak

cat /root/shellDir/program/sedTest/testFile/b.txt.bak
Linux系统发送告警脚本【www.vipsrc.com】.sh
MySQL数据库备份多循环【www.vipsrc.com】.sh
nginx 访问访问日志按天切割【www.vipsrc.com】.sh

2. Create the corresponding file based on the text content:

touchFile.sh.bak

cat touchFile.sh.bak

#!/bin/bash
currPathFile=/root/shellDir/program/sedTest/testFile/b.txt.bak
IFS=$'\n'

for i in `cat $currPathFile`
do
        touch $i
done

Effect after execution:
Insert picture description here

3. Rename the file (remove the following website name)

rename.sh.bak

cat rename.sh.bak

#!/bin/bash
IFS=$'\n'

pwd=/root/shellDir/program/sedTest/testFile
#oraginalName=($(ls $pwd *.sh)) 原先这里是这样写的,发现这种写法会产生多种数据,它显示通配符的数据和绝对路径的所有文件,很J2怪。
oraginalName=($(ls $pwd | ls *.sh))
changeName=($(ls $pwd | awk -F '【' '/sh$/{print $1 ".sh"}'))

count=1
for (( i = 0; i < ${#oraginalName[@]}; ++i ));
do
        count=1
        for (( j = 0; j < ${#oraginalName[@]}; ++j ));
        do
                if [ $count==$i  ];then
                        # echo " ${oraginalName[i]} ---> ${changeName[i]}";
                       #mv  ${oraginalName[i]} ${changeName[i]}
                       mv  "$pwd/${oraginalName[i]}" "$pwd/${changeName[i]}"

                        break
                fi
        done
        let count++
done

Execution effect:
Insert picture description here

2021年1月14日15:49:28 时隔几个月,然后发现 rename更好用。。。 执行下面操作即可,上面的代码就当锻炼一点逻辑思维了
rename "【www.vipsrc.com】" "" *.sh

4. Batch conversion of encoding format

convertFile.sh.bak

cat convertFile.sh.bak

#!/bin/bash
### 将 values_here 替换为输入编码
FROM_ENCODING="iso-8859-1"
### 输出编码 (UTF-8)
TO_ENCODING="UTF-8"
### 转换命令
CONVERT=" iconv  -f   $FROM_ENCODING  -t   $TO_ENCODING"
### 使用循环转换多个文件
for  file  in  *.converted; do
$CONVERT   "$file"   -o  "${file%.sh}.gbk.converted"
done
exit 0

The file command can view the encoding format of the file

ls *.sh | xargs -I {
    
    } file {
    
    }file -i *.sh

Reference blog: https://linux.cn/article-7959-1.html

Execution effect:
Before
Insert picture description here
conversion:
Insert picture description here
After conversion: But after conversion, it is still garbled, which is very annoying. The display on win is normal, and it is converted to UTF-8, and then it is garbled when it is executed in Linux. echo $LANG ------> en_US.UTF-8
(It is estimated to be like what the group said: edit and copy text under Windows, vim on Linux, and right-click and paste)
If there is any good method, please discuss it together.

5. Redirect to markdown

Script content:
appendFile.sh.bak

#!/bin/bash
IFS=$'\n'

pwd=/drives/e/jpg/test/convert
fileName=($(ls $pwd| ls  *.sh))

for (( i = 0; i < ${#fileName[@]}; ++i ));
do
cat >> $pwd/test.md <<EOF
\`\`\`bash
# ${
      
      fileName[i]}
`cat ${fileName[i]}`
\`\`\`


EOF
	
done

The pits encountered in the above script:
1. The markdown format `number in cat should be escaped, otherwise it will remain stuck. After ctrl+c exits, the terminal is directly disconnected and requires reconnection.
2. The last EOF of cat redirection should be written in the top grid, otherwise an inexplicable error should be reported.
Insert picture description here
The effect after execution: (but the content is garbled, if you have a better solution, welcome to discuss and discuss)
Add all the *.sh content to the markdown format
Insert picture description here

This article is almost over. The purpose of doing so many operations above is to comment out the understanding and comment of the shell script that I learned before. Take a note. such as:
Insert picture description here

Guess you like

Origin blog.csdn.net/Nightwish5/article/details/109332594