美文网首页
Shell脚本(上)

Shell脚本(上)

作者: chanzd | 来源:发表于2020-03-19 15:01 被阅读0次

1.基本

  • PS1 PS2 控制提示符格式
    详情点击
  • echo 输出命令,可以用来输出数字、变量、字符串(字符串可以在“”内换行)
  • read 从终端读取用户输入 记住用$读取输入(可以使用{}为变量加边界),给变量赋值 =周围不能有空格
  • 执行脚本 sh xxx.sh 、. ./xxx.sh 、 source xxx.sh 、chmod +x ./xxx.sh 增加执行权限之后之后 ./xxx.sh
  • 如果变量的内容是数字,那么可以不加引号;如果真的需要原样输出就加单引号;其他没有特别要求的字符串等最好都加上双引号,定义变量时加双引号是最常见的使用场景,不加引号的输出不能有空格
  • 将命令的结果赋值给变量 variable=$(command)
  • readonly 可以将变量变为只读变量
  • unset 删除变量(但是不能删除只读变量)
  • export 修饰变为环境变量(临时的,关闭shell就销毁,如果需要全局环境变量,需要写入启动文件),可以传给子shell(bash创建子shell,exit 可以退出子shell)
  • 定义函数 function func() 变量默认全是全局变量(在当前shell内有效) local 修饰变为局部变量
  • 传递参数用 $n 的形式
变量 含义
$0 当前脚本文件名
$n(n>=1) 传递给脚本或者函数的参数
$# 传递给脚本或函数的参数个数
$* 传递给脚本或函数的所有参数
$@ 同上 微有不同
$? 上个命令的退出状态,或函数的返回值
$$ 当前shell的进程ID,对于shell脚本,就是脚本所在进程ID

  • $* 会将所有参数整体当作一份数据
  • $@仍然将每个参数都看作一份数据
  #!/bin/bash
echo "print each param from \"\$*\""
for var in "$*"
do
    echo "$var"
done
echo "print each param from \"\$@\""
for var in "$@"
do
    echo "$var"
done

$?获取上一个命令的退出状态 或者函数返回值

#a.sh
#!/bin/bash
if [ $1 == 100 ]
then
   return 0  #参数正确,返回0
else
   return 1  #参数错误,返回1
fi
#b.sh
#!/bin/bash
echo $?
$ . ./a.sh 100
$ . ./b.sh
0
#!/bin/bash
#得到两个数相加的和
function add(){
    return `expr $1 + $2`
}
add 23 50  #调用函数
echo $?  #获取函数返回值
  • ${#string_name} 获取字符串长度
#!/bin/bash
str="http://c.biancheng.net/shell/"
echo ${#str}
  • 字符串拼接,直接放在一块就可以,但是不能有空格
#!/bin/bash
name="Shell"
url="http://c.biancheng.net/shell/"
str1=$name$url  #中间不能有空格
str2="$name $url"  #如果被双引号包围,那么中间可以有空格
str3=$name": "$url  #中间可以出现别的字符串
str4="$name: $url"  #这样写也可以
str5="${name}Script: ${url}index.html"  #这个时候需要给变量名加上大括号
echo $str1
echo $str2
echo $str3
echo $str4
echo $str5
运行结果:
Shellhttp://c.biancheng.net/shell/
Shell http://c.biancheng.net/shell/
Shell: http://c.biancheng.net/shell/
Shell: http://c.biancheng.net/shell/
ShellScript: http://c.biancheng.net/shell/index.html
  • 字符串截取

    1. 从字符串左边开始计数

    ${string: start :length}

    url="c.biancheng.net"
    echo ${url: 2: 9}
    结果  biancheng
    url="c.biancheng.net"
    echo ${url: 2}  #省略 length,截取到字符串末尾
    结果为biancheng.net
    
    1. 从字符串右边开始计数

    ${string: 0-start :length}

    1. 从指定字符开始截取

    使用#号可以截取指定字符(或者子字符串)右边的所有字符,具体格式如下:
    ${string#*chars}

    如果希望直到最后一个指定字符(子字符串)再匹配结束,那么可以使用##,具体格式为:
    $ {string##*chars}

    格式 说明
    ${string: start :length} 从 string 字符串的左边第 start 个字符开始,向右截取 length 个字符。
    ${string: start} 从 string 字符串的左边第 start 个字符开始截取,直到最后。
    ${string: 0-start :length} 从 string 字符串的右边第 start 个字符开始,向右截取 length 个字符。
    ${string: 0-start} 从 string 字符串的右边第 start 个字符开始截取,直到最后。
    ${string#*chars} 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
    ${string##*chars} 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
    ${string%*chars} 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。
    ${string%%*chars} 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • shell脚本上

    | 对于初学者而言,因为没有实战经验,写不出来Shell[https://www.linuxcool.com/]脚...

  • Shell脚本(上)

    1.基本 PS1 PS2 控制提示符格式详情点击 echo 输出命令,可以用来输出数字、变量、字符串(字符串可以...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

网友评论

      本文标题:Shell脚本(上)

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