shell 编程基础

本文介绍了Shell脚本的基础知识,包括基本结构、变量赋值、条件判断、循环控制语句等,并通过实例演示了如何使用这些功能进行脚本编写。
shell 结构:
1.#!指令执行脚本的shell
2.#注释
3.程序

shell 程序:
1.创建shell程序
2.修改执行权限 chmod u+x
3.执行 ./example.sh   或  sh ./example.sh

设置定时器:
crontab
 -e  编辑该用户的计时器设置。
 -l  列出该用户的计时器设置。
 -r  删除该用户的计时器设置。
 -u<用户名称>  指定要设定计时器的用户名称。

分 时 天 月 周 sh
0  3   ×  × 2,5  /bin/sh /shell/autobak.sh /etc  #每周2和5凌晨3点备份etc

调试:sh -x test.sh
检查语法:sh -n test.sh

*/10 * * * * command  每10分钟一次
0 * * * * command  每小时一次
0 */3 * * * command  每三小时一次



变量赋值:不能有空格
[root@localhost shell]# date="2011-05-27"
[root@localhost shell]# echo $date
2011-05-27
[root@localhost shell]# date=`date`
[root@localhost shell]# echo $date
2011年 05月 28日 星期六 08:58:23 CST
[root@localhost shell]# date=$(date +%F)
[root@localhost shell]# echo $date
2011-05-28
[root@localhost shell]# date=$(date +'%Y-%m-%d %H:%M:%S')
[root@localhost shell]# echo $date
2011-05-28 09:06:33
[root@localhost shell]# time=$date
[root@localhost shell]# echo $time
2011-05-28 09:06:33
[root@localhost shell]# time="date is $date"
[root@localhost shell]# echo $time
date is 2011-05-28 09:06:33
[root@localhost shell]# time='date is $date'
[root@localhost shell]# echo $time
date is $date


查看变量:set | more
删除变量:unset time

位置变量 $# $* $? $$ :
[root@localhost shell]#sh bianliang.sh file1 file2 file3
$# is: 3 #统计参数个数
$* is: file1 file2 file3 #所有参数
$? is: 0 #执行返回值(0成功,非0失败)
$$ is: 3500 #pid
$2 is: file2 #第二个参数
$0 is: bianliang.sh #脚本

交互:
[root@localhost shell]# vim read.sh
#!/bin/sh
read first second third
echo "first is $first"
echo "second is $second"
echo "second is $third"
[root@localhost shell]# chmod u+x read.sh
[root@localhost shell]# sh read.sh
100 200 300
first is 100
second is 200
second is 300

运算:
expr 8 + 3 #加
expr 8 - 3 #减
expr 8 \* 3 #乘
expr 8 / 3 #除
[root@localhost shell]# var=`expr 8 % 3` #余
[root@localhost shell]# echo $var
2
[root@localhost shell]# expr `expr 5 - 2` \* 8
24

变量测试:test -d $1 等价于 [ -d $1 ]
[root@localhost shell]# test 2 = 2
[root@localhost shell]# echo $?
0
[root@localhost shell]# test 2 = 3
[root@localhost shell]# echo $?
1
字符串测试:
test str1 = str2 #判断字符串是否相等
test str1 != str2 #判断字符串是否不相等
test str1  #判断字符串是否不为空
test -n str1  #判断字符串是否不为空
test str1  #判断字符串是否为空
整数测试:
test int1 -eq int2 #测试是否相等
test int1 -ne int2 #测试是否不相等
test int1 -ge int2 #测试是否int1>=int2
test int1 -gt int2 #测试是否int1>int2
test int1 -le int2 #测试是否int1<=int2
test int1 -lt int2 #测试是否int1<int2
文件测试:
test -d file #文件是否目录
test -f file #文件是否常规文件
test -x file #文件是否可执行
test -r file #文件是否可读
test -w file #文件是否可写
test -a file #文件是否存在
test -s file #文件大小是否非0

流程控制语句if_else:
[root@localhost shell]# vim if_else.sh
#!/bin/sh
if [ $1 -eq 1 ];then
   echo "a"
elif [ $1 -eq 2 ];then
   echo "b"
elif [ $1 -eq 3 -o $1 -eq 4 ];then#等于3或等于4
   echo "c"
elif [ $1 -gt 0 -a $1 -lt 100 ];then#大于0且小于100
   echo "d"
else
   echo "e"
fi

流程控制语句for:
[root@localhost shell]# vim for.sh
#!/bin/sh
day="Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
for d in $day
do
  echo "the day is:$d"
done

流程控制语句select:
[root@localhost shell]# ./select.sh
what is your os?
1) linux
2) unux
3) windows
4) other
#? 1
is linux
[root@localhost shell]# cat select.sh
#!/bin/sh
echo "what is your os?"
select s in linux unux windows other
do
  break
done
echo "is $s"

流程控制语句case:
[root@localhost shell]# ./case.sh
*******************
please select your operation:
press c to copy
press d to delete
press b to backup
*******************
d
is delete
[root@localhost shell]# cat case.sh
#!/bin/sh
echo "*******************"
echo "please select your operation:"
echo "press "c" to copy "
echo "press "d" to delete "
echo "press "b" to backup "
echo "*******************"
read o
case $o in
   c)
    echo "is copy"
   ;;
   d)
    echo "is delete"
   ;;
   b)
    echo "is backup"
   ;;
   *)
    echo "invalide"
    exit 1#退出
esac

流程控制语句while:
[root@localhost shell]# ./while.sh
1
4
9
16
25
36
49
64
81
[root@localhost shell]# cat while.sh
#!/bin/sh
num=1
while [ $num -lt 10 ]
do
 sum=`expr $num \* $num`
 echo $sum
 num=`expr $num + 1`
done


流程控制语句until:
[root@localhost shell]# sh until.sh
press Y/y to stop...
y
stop here
[root@localhost shell]# cat until.sh
#!/bin/sh
echo "press Y/y to stop..."
read i
until [ $i = 'y' ] || [ $i = 'Y' ]
do
  echo "error input"
  read i
done
echo "stop here"

流程控制语句break:
[root@localhost shell]# ./break.sh
**************************
please select your operation:
1 copy
2 delete
3 backup
4 quit
**************************
4
is exit...
[root@localhost shell]# cat break.sh
#!/bin/sh
while true
do
 echo "**************************"
 echo "please select your operation:"
 echo "1 copy"
 echo "2 delete"
 echo "3 backup"
 echo "4 quit"
 echo "**************************"
 read o
 case $o in
   1)
   echo "is copy";;
   2)
   echo "is delete";;
   3)
   echo "is back";;
   4)
   echo "is exit..."
    break;;
   *)
   echo "invalide"
   # continue;;
  esac
done

流程控制语句shift:
[root@localhost shell]# ./shift.sh 1 2 3
6
[root@localhost shell]# cat shift.sh
#!/bin/sh
sum=0
while [ $# -gt 0 ]
do
 sum=`expr $sum + $1 `
 shift
done
echo $sum

函数的调用:
[root@localhost shell]# ./function.sh zsc xhj
zsc
xhj
[root@localhost shell]# cat function.sh
#!/bin/sh

help(){
  for s in $*
  do
      echo $s
  done
}

help $1 $2
**项目名称:** 基于Vue.js与Spring Cloud架构的博客系统设计与开发——微服务分布式应用实践 **项目概述:** 本项目为计算机科学与技术专业本科毕业设计成果,旨在设计并实现一个采用前后端分离架构的现代化博客平台。系统前端基于Vue.js框架构建,提供响应式用户界面;后端采用Spring Cloud微服务架构,通过服务拆分、注册发现、配置中心及网关路由等技术,构建高可用、易扩展的分布式应用体系。项目重点探讨微服务模式下的系统设计、服务治理、数据一致性及部署运维等关键问题,体现了分布式系统在Web应用中的实践价值。 **技术架构:** 1. **前端技术栈:** Vue.js 2.x、Vue Router、Vuex、Element UI、Axios 2. **后端技术栈:** Spring Boot 2.x、Spring Cloud (Eureka/Nacos、Feign/OpenFeign、Ribbon、Hystrix、Zuul/Gateway、Config) 3. **数据存储:** MySQL 8.0(主数据存储)、Redis(缓存与会话管理) 4. **服务通信:** RESTful API、消息队列(可选RabbitMQ/Kafka) 5. **部署与运维:** Docker容器化、Jenkins持续集成、Nginx负载均衡 **核心功能模块:** - 用户管理:注册登录、权限控制、个人中心 - 文章管理:富文本编辑、分类标签、发布审核、评论互动 - 内容展示:首页推荐、分类检索、全文搜索、热门排行 - 系统管理:后台仪表盘、用户与内容监控、日志审计 - 微服务治理:服务健康检测、动态配置更新、熔断降级策略 **设计特点:** 1. **架构解耦:** 前后端完全分离,通过API网关统一接入,支持独立开发与部署。 2. **服务拆分:** 按业务域划分为用户服务、文章服务、评论服务、文件服务等独立微服务。 3. **高可用设计:** 采用服务注册发现机制,配合负载均衡与熔断器,提升系统容错能力。 4. **可扩展性:** 模块化设计支持横向扩展,配置中心实现运行时动态调整。 **项目成果:** 完成了一个具备完整博客功能、具备微服务典型特征的分布式系统原型,通过容器化部署验证了多服务协同运行的可行性,为云原生应用开发提供了实践参考。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值