Shell学习从入门到实战

前言

因为之前对项目的部署只是停留在用xshell连接云服务器,然后运行jar包,tomcat运行war包等形式进行部署,到了每次要部署的时候都要重复输入指令,效率太差。所有就想是不是可以利用脚本来进行项目的部署,毕竟大家都是这么干滴!所以博主就在b乎上找了尚硅谷的linux教学视频(这里不得不称赞一下,尚硅谷的教学视频还是做的挺不错的),从linux常用指令入手到shell脚本编写,博主真的是收益匪浅,感觉对linux的熟悉程度又上升了一个层次了。

好的,废话不多说,我们开始讲讲shell有哪些基础知识,并且针对每一个点都会进行实战操作,让大家快速上手shell。

shell基础

linux系统中,由下到上分为硬件层,linux内核层,shell层,应用软件层。所有shell就是操作linux内核的一种脚本语言,具体关系如图:
在这里插入图片描述

入门介绍,如何输出一个"hellow word"

创建一个shell脚本步骤:

  • 通过vi helloword.sh来进行创建(后缀可以不是.sh,主要看个人习惯)
  • 脚本里面第一行写:#!/bin/bash
  • 利用echo "hellow word"输出
  • 为脚本赋予权限:chmod 755 helloword.sh
  • 执行脚本./helloword.sh 或 sh helloword.sh
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

变量

shell中变量分为自定义变量和环境变量。

自定义变量

定义变量:变量=值
撤销变量:unset 变量
声明静态变量:readonly 变量 (注意:该类型变量不能被unset)
案例:

A = 100
echo "A=$A"
unset A
echo "A=$A"

在这里插入图片描述
在这里插入图片描述
注意事项:

  • 变量名不能以数字开头
  • =号两边不能又空格
  • 变量名一般大写
  • 引用变量时用$符号
  • 赋值采用反引号RESULT=ls -l /home
  • 赋值采用括号RESULT=(date)

环境变量

环境变量位于/etc/profile下,可以使用vi /ect/profile查看。
在这里插入图片描述
这里可以定义环境变量,格式为:export vsftpd=vsftpd8。
通过source /ect/profile更新环境变量。
在脚本里面输出设置好的环境变量:echo “$vsftpd”
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

位置参数

位置参数主要有四种,分为:

$n,$*,$@,$#,其中:
$n,表示第几个参数
$*,表示参数整体
$@,表示参数分区展示
$#,表示参数个数

具体实例:

#!/bin/bash
echo "$0,$1,$2"
echo "$*"
echo "$@"
echo "$#"

在这里插入图片描述
在这里插入图片描述

预定义变量

预定义变量主要分为3个,分别是:

$$,$!,$?,其中:
$$ 表示当前进程号的pid
$!表示最后一个进程号的pid
$?表示最后一次命令的返回状态,为0则执行成功,为1则失败。

在这里插入图片描述
在这里插入图片描述

条件判断

shell中条件判断通过[ condition ],注意中括号两头有空格,常用判断条件:

整数判断:
= 字符串比较
-lt 小于
-le 小于等于
-eq 等于
-gt 大于
-ge 大于等于
-ne  不等于
文件权限判断:
-r 读权限
-w 写权限
-x 有执行权限
文件类型判断:
-f 文件存在并且是常规文件
-e 文件存在
-d 文件存在并是目录

案例:通过if判断/ect/vsftpd.conf文件是否存在,是否有可执行权限。步骤:

  if [ /etc/vsftpd.conf -e ]
  then 
  echo "文件存在"
  elif [ /etc/vsftpd.conf -x ]
  then
  echo "文件有可执行权限"
  fi

在这里插入图片描述
在这里插入图片描述

流程控制

case判断

shell中多分之判断类似于java中的switch,具体用法如下:

输入一个变量,根据输入判断是星期几。
case $1 in 
"1") 
echo "周一"
;;
"2")
echo "周二"
;;
"3")
echo "周三"
;;
esac

在这里插入图片描述
在这里插入图片描述

for循环

for循环输出控制台输入的参数:

for i in "$@"
do
echo "the num is $i"
done

在这里插入图片描述
在这里插入图片描述

while循环

获取控制台参数进行求和:

k=0
sum=0
while [ $k -le $1 ]
do
sum=$[$sum+$k]
k=$[$k+1]
done
echo "sum为$sum"

在这里插入图片描述
在这里插入图片描述

尾声

以上就是学习shell过程中的基础知识和案例了,看到这里,你兴许就知道了如何快速写出部署jar包的脚本了吧。没错,就是几行shell就能搞定的事。
附图:
在这里插入图片描述
是不是感觉太水了,哈哈。博主下学期的学习目标是尚硅谷的SpringCloud课程,然后通过实战方式来重构自己的微招聘App后台部分。
谢谢阅读,不好之处欢迎指点。
勿喷渣渣博主,与君共勉!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux Shell高级技巧实战总结 一、将输入信息转换为大写字符后再进行条件判断 二、为调试信息设置输出级别 三、判断参数是否为数字 四、判断整数变量的奇偶性 五、将Shell命令赋值给指定变量,以保证脚本的移植性 六、获取当前时间距纪元时间(1970年1月1日)所经过的天数 七、非直接引用变量 八、在循环使用管道的技巧 九、自链接脚本 十、Here文档的使用技巧 十一、获取进程的运行时长(单位: 分钟) 十二、模拟简单的top命令 十三、格式化输出指定用户的当前运行进程 十四、用脚本完成which命令的基本功能 十五、验证输入信息是否合法 十六、整数验证 十七、判断指定的年份是否为闰年 十八、将单列显示转换为多列显示 十九、将文件的输出格式化为指定的宽度 二十、监控指定目录下磁盘使用空间过大的用户 二十一、编写一个更具可读性的df命令输出脚本 二十二、编写一个用于添加新用户的脚本 二十三、kill指定用户或指定终端的用户进程 二十四、判断用户输入(是/否)的便捷方法 二十五、通过FTP下载指定的文件 二十六、文件锁定 二十七、用小文件覆盖整个磁盘 二十八、统计当前系统不同运行状态的进程数量 二十九、浮点数验证 三十、统计英文文章每个单词出现的频率 Linux Shell经典实例解析--Oracle启动脚本(上) Linux Shell经典实例解析--Oracle启动脚本(下)
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值