跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

LINUX——shell编程

Shell 简介 Shell 是一个 C 语言编写的脚本语言,它是用户与 Linux 的桥梁,用户输入命令交给 Shell 处理, Shell 将相应的操作传递给内核(Kernel),内核把处理的结果输出给用户。

下面是流程示意图:

第一个 Shell 脚本 用 vi 打开 test.sh,编写: # vi test.sh #!/bin/bash echo "Hello world!" 方法 1:直接用 bash 解释器执行 # bash test.sh Hello world! 方法 2:添加可执行权限 # ll test.sh -rw-r--r--. 1 root root 32 Aug 18 01:07 test.sh # chmod +x test.sh # ./test.sh -bash: ./test.sh: Permission denied # chmod +x test.sh # ./test.sh # ./在当前目录 Hello world! 方法 3:source 命令执行,以当前默认 Shell 解释器执行 # source test.sh Hello world! Shell 变量 系统变量

普通变量与临时环境变量 普通变量定义:VAR=value 临时环境变量定义:export VAR=value 变量引用:$VAR 特殊变量

变量引用

条件表达式

整数比较符

字符串比较符

文件测试

布尔运算符

逻辑判断符

整数运算

Shell 括号用途总结

实验 前提准备 shell脚本的书写规范 通常情况下,我们会给shell脚本起名为filename.sh #编辑.sh文件时自动生成关于脚本文件说明的注释 [root@localhost ~]# cat /root/.vimrc autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()" func SetTitle() if expand("%:e") == 'sh' call setline(1,"#!/bin/bash") call setline(2,"#########################") call setline(3,"#File name:".expand("%"))     #文件名字 call setline(4,"#Version:v1.0")      #shell版本 call setline(5,"#Email:admin@test.com")   #作者Email call setline(6,"#Created time:".strftime("%F %T"))   #创建时间 call setline(7,"#Description:")   #当前文件备注 call setline(8,"#########################") call setline(9,"") endif endfunc 1、写一个脚本,给脚本传递两个参数,显示两者之和和两者之积 第一步 创建shell文件的存储目录 [root@localhost /]# mkdir scripts 第二步 创建num.sh文件,编写文件 [root@localhost scripts]# vim num.sh #!/bin/bash               #编译脚本配置 ############################################################## # File Name: num.sh       #文件名字 # Version: V1.0                #编译版本 # Author: Gao_XY            #作者名字 # Email: Gao_XY@163.com       #作者邮箱 # Organization:https://blog.csdn.net/ens33?type=blog        #作者网站 # Created Time : 2024-12-12 11:50:27               #创建文件时间 # Description: "给脚本传递两个参数,显示两者之和与两者之积"      #备注 ############################################################## read -p "请输入两个数字:" A B      #read -p  通过 read 读入持续等待输人 echo "他们的和为:" $[A+B]           #$[]执行内部的变量,并输出 echo "他们的积为:" $[A*B]

第三步 执行文件 sh num.sh

2、写一个脚本,判断nginx是否安装,没有则安装。

第一步 创建app.sh 文件 [root@localhost scripts]# vim app.sh 第二步 编译脚本文件 #!/bin/bash ############################################################## # File Name: app.sh # Version: V1.0 # Author: Gao_XY # Email: Gao_XY@163.com # Organization:https://blog.csdn.net/ens33?type=blog # Created Time : 2024-12-12 14:58:55 # Description:  "判断nginx是否安装,没有则安装" ############################################################## [ -f nginx ] && echo "nginx is exists" || yum -y install nginx # [ -f *** ] 判断该文件是否存在 若存在则输出 *** ,否则 下载该文件 # 或者 rpm -q nginx &>/dev/null && echo "nginx is exists"  || yum -y install nginx

第三步 执行脚本文件 [root@localhost scripts]# sh app.sh

3、写一个脚本,判断是否是root用户,如果不是输出"Please use the root user to execute the script" 第一步 创建user.sh脚本 [root@localhost scripts]# vim user.sh 第二步 编译文件 #!/bin/bash ############################################################## # File Name: user.sh # Version: V1.0 # Author: Gao_XY # Email: Gao_XY@163.com # Organization:https://blog.csdn.net/ens33?type=blog # Created Time : 2024-12-12 14:47:37 # Description: ############################################################## [ $UID -eq 0 ] && echo "this is root " || echo "Please use the root user to execute the script" # $UID=0 则为root用户 #[ $UID -eq 0 ] 判断UID是否为0 若是则输出 **** ,否则输出 *****

第三步 执行文件 [root@localhost scripts]# sh user.sh 第四步 编译环境 [root@localhost scripts]# vim /etc/profile          #编辑该配置文件 在末尾添加该配置 export PATH=$PATH:/scripts/user.sh    #使当前服务器下所有用户执行该目录下的变量 第五步测试 root用户测试

其他用户测试 [root@localhost scripts]# su gxy

相关文章