学习编写shell脚本(一)

xiaoxiao2021-02-28  188

学习编写shell脚本(一)


作为java程序员,虽然主流开发是在windows上使用eclipse等工具进行开发,但是进行现场环境的搭建、项目部署、项目监控等都需要在linux上进行,掌握linux的常用命令,是一个程序员的基本技能,但是对于shell脚本的编写,其实很多人在这个方面的技能的掌握还是比较不尽人意。 本人基本上对linux是比较菜的,都怪之前大学逃课有点多,现在才后悔莫及啊。所以准备从现在开始,每天都学习一点linux shell脚本,编写的实例放到上,作以笔记和监督之用。 来一波hello world #!/bin/bash echo "Hello World !" 变量定义 #!/bin/bash your_name="liqiuyu" echo ${your_name} 隐式变量赋值 #!/bin/bash for file in `ll` echo ${file} readonly 不允许对象改变值 #!/bin/bash myUrl="www.baidu.com" readonly myUrl myUrl="www.jd.com" 删除对象 #!/bin/bash thisTime=`date` echo ${thisTime} unset thisTime echo ${thisTime} 三种变量 #!/bin/bash echo "局部变量 在脚本或命令中定义,仅在当前shell实例有效" echo "环境变量,所有的程序,包括shell启动的程序,都可访问" echo "shell变量 由shell程序设置的特殊变量,部分是局部变量,部分是环境变量" 字符串 #!/bin/bash/ #字符串是最常用的数据类型,可以用单引号或者双引号,也可以不用引号 #单引号:任何字符原样输出;单引号中不能出现单引号(转义后也不行) #双引号:可以出现变量;可以出现转义字符 str1=aaa bbb ccc echo ${str1} str2='aaa ${str1}' echo ${str2} str3="aaa ${str1}" echo ${str3} your_name='qinjx' str="Hello, I know your are \"$your_name\"! \n" echo ${str} 拼接字符串 #!/bin/bash #拼接字符串 your_name="liqiuyu" greeting="hello, "${your_name}" !" greeting_1="hello, ${your_name} !" echo ${greeting} ${greeting_1} 获取字符串的长度 #!/bin/bash #获取字符串长度 string="abcde efwef" echo ${#string} #输出11 string1="我爱中国" echo ${#string1}#输出4 截取子字符串 #!/bin/bash #提取子字符串 ${string:i:n} 从第 i + 1 个字符(从一开始)开始截取 n 个字符 string="this is a string" echo ${string:1:4} #输出 ”his “ 查找字符串 #!/bin/bash #查找字符串 string="runoob is a great company" echo `expr index "$string" is`
转载请注明原文地址: https://www.6miu.com/read-24002.html

最新回复(0)