


34 Linux Shell scripts commonly used for operation and maintenance will definitely help you!
#!/bin/bash# 脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了,# 猜小了或猜大了,直至用户猜对脚本结束。# RANDOM 为系统自带的系统变量,值为 0‐32767的随机数# 使用取余算法将随机数变为 1‐100 的随机数num=$[RANDOM%100+1]echo "$num"# 使用 read 提示用户猜数字# 使用 if 判断用户猜数字的大小关系:‐eq(等于),‐ne(不等于),‐gt(大于),‐ge(大于等于),# ‐lt(小于),‐le(小于等于)while :do read -p "计算机生成了一个 1‐100 的随机数,你猜: " cai if [ $cai -eq $num ] then echo "恭喜,猜对了" exit elif [ $cai -gt $num ] then echo "Oops,猜大了" else echo "Oops,猜小了" fidone
#!/bin/bash#!/bin/bash# 查看有多少远程的 IP 在连接本机(不管是通过 ssh 还是 web 还是 ftp 都统计) # 使用 netstat ‐atn 可以查看本机所有连接的状态,‐a 查看所有,# -t仅显示 tcp 连接的信息,‐n 数字格式显示# Local Address(第四列是本机的 IP 和端口信息)# Foreign Address(第五列是远程主机的 IP 和端口信息)# 使用 awk 命令仅显示第 5 列数据,再显示第 1 列 IP 地址的信息# sort 可以按数字大小排序,最后使用 uniq 将多余重复的删除,并统计重复的次数netstat -atn | awk '{print $5}' | awk '{print $1}' | sort -nr | uniq -c
#!/bin/bashfunction example {echo "Hello world!"}example
#!/bin/sh`v1="Hello"v2="world"v3=${v1}${v2}echo $v3pidlist=`ps -ef|grep apache-tomcat-7.0.75|grep -v "grep"|awk '{print $2}'`echo $pidlistecho "tomcat Id list :$pidlist" //显示pid
#!/bin/bashgame=(石头 剪刀 布)num=$[RANDOM%3]computer=${game[$sum]}echo "请根据下列提示选择您的出拳手势"echo " 1. 石头"echo " 2. 剪刀"echo " 3. 布 "read -p "请选择 1-3 :" personcase $person in1) if [ $num -eq 0 ] then echo "平局" elif [ $num -eq 1 ] then echo "你赢" else echo "计算机赢"fi;;2) if [ $num -eq 0 ] then echo "计算机赢" elif [ $num -eq 1 ] then echo "平局" else echo "你赢"fi;;3) if [ $num -eq 0 ] then echo "你赢" elif [ $num -eq 1 ] then echo "计算机赢" else echo "平局"fi;;*) echo "必须输入1-3 的数字"esac
#!/bin/bashfor i in `seq 9`do for j in `seq $i` do echo -n "$j*$i=$[i*j] " done echodone
#!/bin/bash# 一键部署 memcached # 脚本用源码来安装 memcached 服务器# 注意:如果软件的下载链接过期了,请更新 memcached 的下载链接wget http://www.memcached.org/files/memcached-1.5.1.tar.gzyum -y install gcctar -xf memcached‐1.5.1.tar.gzcd memcached‐1.5.1./configuremakemake install
#!/bin/bash# 检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不# 是,则提示您非管理员(使用字串对比版本) if [ $USER == "root" ] then yum -y install vsftpdelse echo "您不是管理员,没有权限安装软件"fi
#!/bin/bash -xvif [ $1 -eq 2 ] ;then echo "wo ai wenmin"elif [ $1 -eq 3 ] ;then echo "wo ai wenxing "elif [ $1 -eq 4 ] ;then echo "wo de xin "elif [ $1 -eq 5 ] ;then echo "wo de ai "fi
#!/bin/bash#kill tomcat pidpidlist=`ps -ef|grep apache-tomcat-7.0.75|grep -v "grep"|awk '{print $2}'` #找到tomcat的PID号echo "tomcat Id list :$pidlist" //显示pidkill -9 $pidlist #杀掉改进程echo "KILL $pidlist:" //提示进程以及被杀掉echo "service stop success"echo "start tomcat"cd /opt/apache-tomcat-7.0.75pwd rm -rf work/*cd bin./startup.sh #;tail -f ../logs/catalina.out
#!/bin/bash# 打印国际象棋棋盘# 设置两个变量,i 和 j,一个代表行,一个代表列,国际象棋为 8*8 棋盘# i=1 是代表准备打印第一行棋盘,第 1 行棋盘有灰色和蓝色间隔输出,总共为 8 列# i=1,j=1 代表第 1 行的第 1 列;i=2,j=3 代表第 2 行的第 3 列# 棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块# 使用 echo ‐ne 打印色块,并且打印完成色块后不自动换行,在同一行继续输出其他色块for i in {1..8}do for j in {1..8} do sum=$[i+j] if [ $[sum%2] -eq 0 ];then echo -ne "\033[46m \033[0m" else echo -ne "\033[47m \033[0m" fi done echodone
#!/bin/bash# 统计当前 Linux 系统中可以登录计算机的账户有多少个#方法 1:grep "bash$" /etc/passwd | wc -l#方法 2:awk -f : '/bash$/{x++}end{print x}' /etc/passwd
#!/bin/shsource /etc/profiledbName=mysqltableName=dbecho [`date +'%Y-%m-%d %H:%M:%S'`]' start loading data...'mysql -uroot -proot -P3306 ${dbName} -e "LOAD DATA LOCAL INFILE '# /home/wenmin/wenxing.txt' INTO TABLE ${tableName} FIELDS TERMINATED BY ';'"echo [`date +'%Y-%m-%d %H:%M:%S'`]' end loading data...'exitEOF
#!/bin/bash# 使用死循环实时显示 eth0 网卡发送的数据包流量 while :do echo '本地网卡 ens33 流量信息如下:' ifconfig ens33 | grep "RX pack" | awk '{print $5}' ifconfig ens33 | grep "TX pack" | awk '{print $5}' sleep 1done
#!/bin/bash# 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机# 状态(for 版本)for i in {1..254}do # 每隔0.3秒ping一次,一共ping2次,并以1毫秒为单位设置ping的超时时间 ping -c 2 -i 0.3 -W 1 192.168.1.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.1.$i is up" else echo "192.168.1.$i is down" fidone
#!/bin/bash# 编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户# 不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默# 认的 123456 作为默认密码。read -p "请输入用户名:" user#使用‐z 可以判断一个变量是否为空,如果为空,提示用户必须输入账户名,并退出脚本,退出码为 2#没有输入用户名脚本退出后,使用$?查看的返回码为 2if [ -z $user ]; then echo " 您不需要输入账户名" exit 2fi #使用 stty ‐echo 关闭 shell 的回显功能#使用 stty echo 打开 shell 的回显功能stty -echo read -p "请输入密码:" passstty echo pass=${pass:-123456}useradd "$user"echo "$pass" | passwd --stdin "$user"
#!/bin/bash# 依次提示用户输入 3 个整数,脚本根据数字大小依次排序输出 3 个数字read -p " 请输入一个整数:" num1read -p " 请输入一个整数:" num2read -p " 请输入一个整数: " num3# 不管谁大谁小,最后都打印 echo "$num1,$num2,$num3"# num1 中永远存最小的值,num2 中永远存中间值,num3 永远存最大值# 如果输入的不是这样的顺序,则改变数的存储顺序,如:可以将 num1 和 num2 的值对调tmp=0# 如果 num1 大于 num2,就把 num1 和和 num2 的值对调,确保 num1 变量中存的是最小值if [ $num1 -gt $num2 ];then tmp=$num1 num1=$num2 num2=tmpfi# 如果 num1 大于 num3,就把 num1 和 num3 对调,确保 num1 变量中存的是最小值if [ $num1 -gt $num3 ];then tmp=$num1 num1=$num3 num3=$tmpfi# 如果 num2 大于 num3,就把 num2 和 num3 对调,确保 num2 变量中存的是最小值if [ $num2 -gt $num3 ];then tmp=$num2 num2=$num3 num3=$tmpfiecho "排序后数据(从小到大)为:$num1,$num2,$num3"
#!/bin/bash# 根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动 # 00‐12 点为早晨,12‐18 点为下午,18‐24 点为晚上# 使用 date 命令获取时间后,if 判断时间的区间,确定问候语内容tm=$(date +%H)if [ $tm -le 12 ];then msg="Good Morning $USER"elif [ $tm -gt 12 -a $tm -le 18 ];then msg="Good Afternoon $USER"else msg="Good Night $USER"fiecho "当前时间是:$(date +"%Y‐%m‐%d %H:%M:%S")"echo -e "\033[34m$msg\033[0m"
#!/bin/bashcd /home/wenmin/touch wenxing.txtecho "I lov cls" >>wenxing.txt
#!/bin/bashs=0;for((i=1;i<100;i++))do s=$[$s+$i]done echo $sr=0;a=0;b=0;for((x=1;x<9;x++))do a=$[$a+$x] echo $xdonefor((y=1;y<9;y++))do b=$[$b+$y]echo $ydoneecho $r=$[$a+$b]
#!/bin/bashfor i in "$*"do echo "wenmin xihuan $i"donefor j in "$@"do echo "wenmin xihuan $j"done
#!/bin/bash# 每周 5 使用 tar 命令备份/var/log 下的所有日志文件# vim /root/logbak.sh# 编写备份脚本,备份后的文件名包含日期标签,防止后面的备份将前面的备份数据覆盖# 注意 date 命令需要使用反引号括起来,反引号在键盘<tab>键上面tar -czf log-`date +%Y%m%d`.tar.gz /var/log # crontab -e #编写计划任务,执行备份脚本00 03 * * 5 /home/wenmin/datas/logbak.sh
牛逼啊!接私活必备的 N 个开源项目!赶快收藏
#!/bin/bash function sum() { s=0; s=$[$1+$2] echo $s } read -p "input your parameter " p1 read -p "input your parameter " p2 sum $p1 $p2 function multi() { r=0; r=$[$1/$2] echo $r } read -p "input your parameter " x1 read -p "input your parameter " x2 multi $x1 $x2 v1=1 v2=2 let v3=$v1+$v2 echo $v3
#!/bin/bash case $1 in 1) echo "wenmin " ;; 2) echo "wenxing " ;; 3) echo "wemchang " ;; 4) echo "yijun" ;; 5) echo "sinian" ;; 6) echo "sikeng" ;; 7) echo "yanna" ;; *) echo "danlian" ;; esac
#!/bin/sh # function:自动监控tomcat进程,挂了就执行重启操作 # author:huanghong # DEFINE # 获取tomcat PPID TomcatID=$(ps -ef |grep tomcat |grep -w 'apache-tomcat-7.0.75'|grep -v 'grep'|awk '{print $2}') # tomcat_startup StartTomcat=/opt/apache-tomcat-7.0.75/bin/startup.sh #TomcatCache=/usr/apache-tomcat-5.5.23/work # 定义要监控的页面地址 WebUrl=http://192.168.254.118:8080/ # 日志输出 GetPageInfo=/dev/null TomcatMonitorLog=/tmp/TomcatMonitor.log Monitor() { echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]" if [ $TomcatID ] then echo "[info]tomcat进程ID为:$TomcatID." # 获取返回状态码 TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code}) if [ $TomcatServiceCode -eq 200 ];then echo "[info]返回码为$TomcatServiceCode,tomcat启动成功,页面正常." else echo "[error]访问出错,状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo" echo "[error]开始重启tomcat" kill -9 $TomcatID # 杀掉原tomcat进程 sleep 3 #rm -rf $TomcatCache # 清理tomcat缓存 $StartTomcat fi else echo "[error]进程不存在!tomcat自动重启..." echo "[info]$StartTomcat,请稍候......" #rm -rf $TomcatCache $StartTomcat fi echo "------------------------------" } Monitor>>$TomcatMonitorLog
#!/bin/bash # 通过位置变量创建Linux 系统账户及密码 # $1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数 useradd "$1" echo "$2" | passwd --stdin "$1"
#!/bin/bash echo "$0 $1 $2 $3" // 传入三个参数 echo $# //获取传入参数的数量 echo $@ //打印获取传入参数 echo $* //打印获取传入参数
#!/bin/bash # 实时监控本机内存和硬盘剩余空间,剩余内存小于500M、根分区剩余空间小于1000M时,发送报警邮件给root管理员 # 提取根分区剩余空间 disk_size=$(df / | awk '/\//{print $4}') # 提取内存剩余空空间 mem_size=$(free | awk '/Mem/{print $4}') while : do # 注意内存和磁盘提取的空间大小都是以 Kb 为单位 if [ $disk_size -le 512000 -a $mem_size -le 1024000 ] then mail ‐s "Warning" root <<EOF Insufficient resources,资源不足 EOF fi done
#!/bin/bash if [ -f /home/wenmin/datas ] then echo "File exists" fi
#!/bin/bash if [ -f /home/wenmin/datas ] then echo "File exists" fi [root@rich datas]# cat while.sh #!/bin/bash s=0 i=1 while [ $i -le 100 ] do s=$[$s + $i] i=$[$i + 1] done echo $s echo $i
#!/bin/bash # 一键部署 LNMP(RPM 包版本) # 使用 yum 安装部署 LNMP,需要提前配置好 yum 源,否则该脚本会失败 # 本脚本使用于 centos7.2 或 RHEL7.2 yum -y install httpd yum -y install mariadb mariadb-devel mariadb-server yum -y install php php-mysql systemctl start httpd mariadb systemctl enable httpd mariadb
#!/bin/bash read -t 7 -p "input your name " NAME echo $NAME read -t 11 -p "input you age " AGE echo $AGE read -t 15 -p "input your friend " FRIEND echo $FRIEND read -t 16 -p "input your love " LOVE echo $LOVE
#!/bin/bash cp $1 $2
#!/bin/bash if [ -f file.txt ];then echo "文件存在" else echo "文件不存在" fi
The above is the detailed content of 34 Linux Shell scripts commonly used for operation and maintenance will definitely help you!. For more information, please follow other related articles on the PHP Chinese website!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor
