简体   繁体   English

Bash脚本自动化

[英]Bash script automation

I need to create a bash script which does the following: 我需要创建一个执行以下操作的bash脚本:

Creates a file in a folder location 在文件夹位置创建文件

It has to be a new folder 它必须是一个新文件夹

add text data in the file 在文件中添加文本数据

find and replace the sentence in the file 查找并替换文件中的句子

Change permissions to be read only to everyone apart from the owner it has to be read write and exc 将权限更改为仅对所有人(所有者除外)具有只读权限,必须具有读写权限和排除权限

move file to another folder location 将文件移到另一个文件夹位置

Change ownership on file to be root:root 将文件所有权更改为root:root

For each step the script needs to display the action in cmd 对于每个步骤,脚本都需要以cmd显示操作

Please see my attempt below: 请在下面查看我的尝试:

#!/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

These are all on separate lines. 这些都在单独的行上。

Please can some advise on this and how to get the script to show the progress of each task? 请对此提供一些建议,以及如何获取脚本以显示每个任务的进度?

Kind Regards Ali 亲切的问候阿里

#!/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

您可以使用“ set -v”或sh脚本| tee日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM