首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bash脚本自动化

Bash脚本自动化
EN

Stack Overflow用户
提问于 2017-07-11 19:26:50
回答 2查看 93关注 0票数 0

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

在文件夹位置创建文件

它必须是一个新文件夹

在文件中添加文本数据

查找并替换文件中的句子

将权限更改为只对除所有者以外的所有人读取,必须为读取、写入和执行

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

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

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

请看我下面的尝试:

代码语言:javascript
复制
#!/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

这些都在不同的行上。

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

亲切地问候阿里

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-11 19:43:35

代码语言:javascript
复制
#!/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
票数 0
EN

Stack Overflow用户

发布于 2017-07-11 20:21:47

您可以使用'set -v‘或sh script |tee log

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45033253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档