linux - Bash 脚本自动化

标签 linux bash shell

我需要创建一个执行以下操作的 bash 脚本:

在文件夹位置创建文件

必须是一个新文件夹

在文件中添加文本数据

查找并替换文件中的句子

将权限更改为只读给除了所有者之外的所有人它必须是读写和exc

将文件移动到另一个文件夹位置

将文件的所有权更改为 root:root

对于每个步骤,脚本都需要在 cmd 中显示操作

请看下面我的尝试:

#!/bin/sh    

#Creates a file in a folder location
#It has to be a new folder
mkdir Alitest /var

#add text data in the file
vim /var/Alitest/action.txt echo "This is a test that I have created. This is to 
see if the output is successful I normally do this manually but a script is required"

#find and replace the sentence in the file 
replace "This is a test that I have created" "The test has been successful" -- action.txt

#Change permissions to be read only to everyone apart from the owner it has to be read write and exc
chmod 004 -- action.txt

chmod 700 -- action.txt

#move file to another folder location
mv /var/Alitest/action.text /var/log

#Change ownership on file to be root:root
chown root:root

这些都在不同的行上。

请就此提出一些建议,以及如何让脚本显示每个任务的进度?

亲切的问候 阿里

最佳答案

#!/bin/sh -x

#Create a directory "Alitest" is /var if it doesn't exist
mkdir -p /var/Alitest

#add text data in the file
echo "This is a test that I have created. This is to \
see if the output is successful I normally do this manually but a script is required" > /var/Alitest/action.txt

#find and replace the sentence in the file
sed -i 's/This is a test that I have created/The test has been successful/g' \
    /var/Alitest/action.txt

#Change ownership and permissions on file
chown root:root /var/Alitest/action.txt
chmod 744 /var/Alitest/action.txt

#move file to another folder location
mv /var/Alitest/action.txt /var/log/action.txt

关于linux - Bash 脚本自动化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45033253/

相关文章:

arrays - Bash 在变量或数组的每三个字段后插入一个逗号 (,)?

shell - Unix : fork and wait

linux - 无法将字符串转换为 bool 值 - 可以在 Linux 上转换为 powershell

linux - 通过使用匹配多列的一列连接两个 csv 文件

bash - BASH 很慢吗?

使用 MagickWand C API 自定义浮雕

linux - 如何在 Linux 上按名称而不是 PID 杀死进程?

linux - 通过sh运行ddl文件

linux - 无法在 Linux 上安装 gsoap

c - QDevelop问题调试位置指针消失