美文网首页
sh脚本语法

sh脚本语法

作者: dpplh | 来源:发表于2017-10-23 14:11 被阅读49次

开头#!/bin/sh

符号#!用来告诉系统后面的参数是用来执行该文件的程序。

要使编写脚本可执行 编译chmod +x filename 这样才能用./filename来运行

特殊符号

通配符 *

通过find命令查找.sh结尾的文件,使用*代替文件名

find . -name '*.sh'

{}

用来生成序列

$ echo {1..5}
1 2 3 4 5

单引号、双引号、不加引号、反引号

单引号

什么内容就显示什么内容

$ echo '$PWD hostname'
$PWD hostname

双引号

对里面的特殊符号进行解析

$ echo "$PWD hostname"
/Users/lvhan/Desktop/dirName/subDir hostname

不加引号

echo $PWD hostname
/Users/lvhan/Desktop/dirName/subDir hostname

反引号

先运行,把结果留下 与$()作用相同

单小括号、双小括号

()

表达式

文件表达式

if [ -f file ] 文件存在
if [ -d ...  ] 目录存在
if [ -s file ] 文件存在且非空
if [ -r file ] 文件存在且可读
if [ -w file ] 文件存在且可写
if [ -x file ] 文件存在且可执行

整数变量表达式

if [ int1 -eq int2 ] int1 == int2
if [ int1 -ne int2 ] int1 != int2
if [ int1 -ge int2 ] int1 >= int2
if [ int1 -gt int2 ] int1 >  int2
if [ int1 -le int2 ] int1 <= int2
if [ int1 -lt int2 ] int1 <  int2

字符串变量表达式

if [ $string1 = $string2 ]  string1 == string2 // 字符串允许使用赋值号做等号
if [ $string1 != $string2]  string1 != string2
if [ -n $string ] string非空(非0), 返回0(true)
if [ -z $string ] string为空
if [ $string] 类似-n

cat 命令

1. cat filename => 一次显示整个文件
2. cat > filename => 创建一个文件
3. cat file1 file2 > file => 将file1 file2 合并成file
4. cat file1 >> file2 => 将file1的内容写到file2
参数:
-n 或 --number 由 1 开始对所有输出的行数编号
-b 或 --number-nonblank 和 -n 相似,只不过对于空白行不编号
-s 或 --squeeze-blank 当遇到有连续两行以上的空白行,就代换为一行的空白行
-v 或 --show-nonprinting

find 命令

find pathName -name "文件名" -exec ... \;

字符串包含判断

方法一:利用grep查找

strA="Hello world"
strB="llo"
result=$(echo $strA | grep "${strB}")
if [[ "$result" != "" ]];then
    echo "包含"
else
    echo "不包含"
fi

方法二:利用字符串运算符

strA="Hello world"
strB="llo"
if [[ $strA =~ $strB ]];then
    echo "包含"
else
    echo "不包含"
fi

方法三:利用通配符

strA="Hello world"
strB="llo"
if [[ $strA == *$strB* ]];then
    echo "包含"
else
    echo "不包含"
fi

未完待续...持续更新

相关文章

  • sh脚本语法

    开头#!/bin/sh 符号#!用来告诉系统后面的参数是用来执行该文件的程序。 要使编写脚本可执行 编译chmod...

  • shell脚本调试

    一.sh [参数] 文件名.sh -n 不要执行script,仅查询语法的问题-x 将使用的脚本的内容输出到屏幕,...

  • Shell脚本day4~文件包含/cat命令/ read命令/p

    一、文件包含 1.1、语法一:./filename文件A->fileA.sh,脚本内容如下#!/bin/bashe...

  • Linux Xshell 基本语法

    Linux基本语法 编写一个脚本vim test.sh#!/bin/bash#上面的是解释器 还有一种sh解释器...

  • linux中的sh脚本语法

    本文转自于: 本站(SimonSu’Blog)欢迎各类网站交换链接。只要你的站点有特色,不是垃圾采集站都可以。需要...

  • [linux] 监听指定进程,挂掉后自动重启

    创建脚本,后缀为 sh 文件,例如 test.sh 给脚本授权 chmod 777 test.sh 启动脚本 no...

  • 定时秒

    */1****/你要执行的sh脚本.sh */1****sleep20&&/你要执行的sh脚本.sh */1***...

  • Linux基础

    shell脚本运行方式 shell脚本权限:chmod +x test.sh ./test.sh /bin/sh ...

  • 【Shell】语法检查模式

    verbose模式 shell读取脚本时,显示读到的每一行$ bash -v script.sh 语法检查调试模式...

  • shell脚本启动jar

    1.编辑启动脚本start.sh 2.编辑停止脚本stop.sh 3.编辑查看日志脚本log.sh 4.给脚本授权...

网友评论

      本文标题:sh脚本语法

      本文链接:https://www.haomeiwen.com/subject/pncauxtx.html