![常用的shell脚本(shell脚本大全) 图片[1]-常用的shell脚本(shell脚本大全)-不念博客](https://www.bunian.cn/wp-content/uploads/2023/04/image-13.png)
Shell脚本是一种用于自动化任务的脚本语言,它使用shell命令来执行操作。
以下是一些常用的shell脚本示例:
输出”Hello, World!”:
#!/bin/shecho "Hello, World!"#!/bin/sh echo "Hello, World!"#!/bin/sh echo "Hello, World!"
创建一个名为new_folder的文件夹并在其中创建一个名为file.txt的文件:
#!/bin/shmkdir new_foldertouch new_folder/file.txt#!/bin/sh mkdir new_folder touch new_folder/file.txt#!/bin/sh mkdir new_folder touch new_folder/file.txt
在当前目录下列出所有文件,并将其保存到名为files_list.txt的文件中:
#!/bin/shls > files_list.txt#!/bin/sh ls > files_list.txt#!/bin/sh ls > files_list.txt
在给定目录下查找所有扩展名为.txt的文件:
#!/bin/shfind /path/to/directory -type f -name "*.txt"#!/bin/sh find /path/to/directory -type f -name "*.txt"#!/bin/sh find /path/to/directory -type f -name "*.txt"
递归地更改目录和其子目录中所有文件的权限:
#!/bin/shchmod -R 755 /path/to/directory#!/bin/sh chmod -R 755 /path/to/directory#!/bin/sh chmod -R 755 /path/to/directory
删除扩展名为.log的所有文件:
#!/bin/shfind /path/to/directory -type f -name "*.log" -exec rm -f {} \;#!/bin/sh find /path/to/directory -type f -name "*.log" -exec rm -f {} \;#!/bin/sh find /path/to/directory -type f -name "*.log" -exec rm -f {} \;
使用grep在文件中查找特定文本:
#!/bin/shgrep -r "search_term" /path/to/directory#!/bin/sh grep -r "search_term" /path/to/directory#!/bin/sh grep -r "search_term" /path/to/directory
将一个文件的内容添加到另一个文件:
#!/bin/shcat file1.txt >> file2.txt#!/bin/sh cat file1.txt >> file2.txt#!/bin/sh cat file1.txt >> file2.txt
将文本文件的行数计算并输出:
#!/bin/shwc -l file.txt#!/bin/sh wc -l file.txt#!/bin/sh wc -l file.txt
通过SSH在远程服务器上执行命令:
#!/bin/shssh user@remote-server.com "command_to_run"#!/bin/sh ssh user@remote-server.com "command_to_run"#!/bin/sh ssh user@remote-server.com "command_to_run"
列出当前目录下的所有文件和子目录
#!/bin/bashls -R#!/bin/bash ls -R#!/bin/bash ls -R
在Ubuntu系统中更新所有软件包
#!/bin/bashsudo apt-get updatesudo apt-get upgrade#!/bin/bash sudo apt-get update sudo apt-get upgrade#!/bin/bash sudo apt-get update sudo apt-get upgrade
请注意,首行 #!/bin/sh
或 #!/bin/bash
是用于指定解释器的,它告诉系统使用哪个shell程序来执行脚本。
此外,记得为脚本文件添加可执行权限,以便可以运行它:
chmod +x script.shchmod +x script.shchmod +x script.sh
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END