shell 脚本入门到精通(中级)

一、shell 脚本的执行

二、输出格式化

三、数据类型

四、重定向

五、变量

一、shell 脚本的执行

1. 脚本执行的4种方法

$ ls /tmp/test.sh
/tmp/test.sh
#!/bin/bash
# test.sh
# 这里借助SHLVL这个变量,SHLVL可以显示shell的层级,
# 每启动一个shell,这个值就加1
echo "shell level :$SHLVL"
echo "hello world!"
  1. 切换到shell脚本所在目录执行

    root@localhost:/# cd /tmp/

    root@localhost:/tmp# chmod +x test.sh

    root@localhost:/tmp# ./test.sh

    shell level :2

    hello world!

  2. 以绝对路径执行

    root@localhost:~# chmod +x /tmp/test.sh

    root@localhost:~# /tmp/test.sh

    shell level :2

    hello world!

  3. 直接使用bash或sh 来执行bash shell脚本

    root@localhost:/tmp# bash test.sh

    shell level :2

    hello world!

    root@localhost:/tmp# sh test.sh

    shell level :1

    hello world!

  4. 在当前shell 环境中执行

    root@localhost:/tmp# . test.sh

    shell level :1

    hello world!

    root@localhost:/tmp# source test.sh

    shell level :1

    hello world!

总结:注意看SHLVL的值,前3种方式都在子shell中执行(sh除外),第4种在当前shell种执行。

2.调试脚本

bash -x script.sh 跟踪调试shell脚本

例:

root@localhost:/tmp# bash -x test.sh
+ echo 'shell level :2'
shell level :2
+ echo 'hello world!'
hello world!

-x 打印所执行的每一行命令以及当前状态

set -x : 在执行时显示参数和命令

set +x : 禁止调试

set -v : 当命令进行读取时显示输入

set +v : 禁止打印输入

二、输出格式化

1. C语言风格的格式化

#!/bin/bash
printf "%-5s %-10s %-4s\n" NO. Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
root@localhost:/tmp# ./test.sh
NO. Name Mark
1 Sarath 80.35
2 James 91.00

2. echo

  1. 不换行

    echo -n "hello world"
  2. 转义

    echo -e "hello\t\tworld"
  3. 彩色输出
颜色 重置 绿
前景色 0 30 31 32 33 34 35 36 37
背景色 0 40 41 42 43 44 45 46 47

echo -e "\e[1;31m This is red test \e[0m"



echo -e "\033[47;31m This is red test \033[0m"

三、数据类型

1.字符串

获取字符串长度

root@localhost:/# str="hello"
root@localhost:/# echo ${#str}
5

2. 数组

  1. 数组的定义

方法一:

arr=(1 2 3 4 5)

方法二:

arr[0]=1
arr[1]=2
arr[2]=3
echo ${arr[*]}
1 2 3
  1. 打印数组中的值

    root@localhost:~# arr=(1 2 3 4 5)

    root@localhost:~# echo ${arr[2]}

    3

    root@localhost:~# echo ${arr[*]}

    1 2 3 4 5

    root@localhost:~# echo ${arr[@]}

    1 2 3 4 5

  2. 关联数组 -- 数组的高级用法

普通数组只能使用整数作为索引值,而关联数组可以使用任意文本作为索引值(有点类似于Python中的字典,不知道这样理解对不对),关联数组只在bash 4.0以上支持。

查看bash版本的方法:

bash -version

关联数组的定义和使用

root@localhost:~# declare -A person
root@localhost:~# person=([name]="Wang" [age]=18)
root@localhost:~# echo ${person[name]}
Wang
root@localhost:~# echo ${person[age]}
18
root@localhost:~# echo ${person[*]}
Wang 18

四、重定向

符号 含义 用法
< 标准输入 从文件中输入 wc -l file.txt
> 标准输出 目标文件不存在会新建一个;目标文件存在会覆盖原内容 echo "" > /var/www/html/index.php
>> 追加到文件 目标文件不存在会新建一个;目标文件存在会在文件末尾追加 echo "add text" >> file.txt

例:

从定向的用法

# cat >> file.sh << EOF
> #!/bin/bash
> echo "hello"
> EOF

/dev/null 相当于垃圾桶

例:把标准输出重定向到/dev/null

yum makecache > /dev/null

五、变量

1. 只读变量

root@localhost:~# cat file.sh
#!/bin/bash
readonly hours_per_day=24
hours_per_day=12

更改变量会触发异常

root@localhost:~# ./file.sh
./file.sh: line 3: hours_per_day: readonly variable

2. 展开运算符

运算符 用途
${varname:-word} 如果变量存在且非null,则返回其值;
否则返回word
# echo ${service:-"service is not defined"}
service is not defined
${varname:=word} 如果变量存在且非null,则返回其值;
否则设置它的值为word并返回
root@localhost:~# echo ${service:=httpd}
${varname:+word} 如果变量存在且非null,则返回word;
否则返回null
echo ${service:+1}
${varname:?message} 如果变量存在且非null,则返回其值;
否则显示message,并退出当前脚本或命令;
message默认信息为:parameter null or not set
# echo ${b:?}
-bash: b: parameter null or not set
# echo ${b:?"not defined"}
-bash: b: not defined

shell脚本从入门到精通(中级)之提高篇的更多相关文章

  1. shell脚本从入门到精通(初级)之入门篇

    写在开头 本文是阅读<Linux命令行与shell脚本编程大全>时的一些笔记,主要是shell脚本的一些基本语法, 还有很多细节和高级内容没有写到. 笔者也是shell script菜鸟, ...

  2. shell脚本从入门到精通

    阿里云大学 shell脚本从入门到精通 第1 章 : shell脚本编程-变量-算术表达式-判断语句-if分支语句 第2 章 : case-for-While-双括号-循环嵌套-break-conti ...

  3. Android Studio2.0 教程从入门到精通Windows版 - 提高篇

    第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面和如何调试应用,接下来将介绍一些常用的快捷键和必备插件. 常用快捷键 代码跳转 描述:跳转是为了方便代码位置的定 ...

  4. 08 bash特性--shell脚本编程入门

    shell脚本编程入门 编程语言介绍 编程语言分为:机械语言.汇编语言和高级语言: 计算机能识别的语言为机械语言,而人类能学习的并且能够方便掌握的为高级语言,所以,我们所编写的程序就要通过编译来转换成 ...

  5. Shell脚本快速入门

    最近看了下Shell脚本.曾经遇到很多现成的工具包里边就多次用到了Shell脚本.总之这东西的作用无非就是将一系列的操作进行整合. ·整合后使得一套工作更加模块化规范化. ·批量处理要比手动操作快得多 ...

  6. Shell脚本学习入门(一)

    1.Shell脚本是解释型的语言. 2.Shell脚本建立的基本步骤: 3.Shell脚本文件的第一行一般可以是:"#! 路径名 -(选项)", 为了要使Shell脚本有移植性,请 ...

  7. Linux bash shell脚本语法入门

    1.基础 #!/bin/bash   //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 #                 //shell脚本注释符号 2.变量和使用 HOME= ...

  8. (转)Linux bash shell脚本语法入门

    http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...

  9. [拾 得] 一枚迷人的贝壳 SHELL / Linux | shell 脚本初步入门

    坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/   索引: 什么是shell shell的分类 shell脚本的执行方式   ...

随机推荐

  1. PyQt5+qtdesigner开发环境配置

    1.PyQt5安装 pip install PyQt5 2.qtdesigner安装 本来直接用pip install PyQt5-tools安装的,但是网速下的慢,中间还断了几次,在网上找到一个稳定 ...

  2. rename 重命名文件

    1.  使用范例 范例1: 批量修改文件名 [root@localhost data]# touch {a,b,c,d,e}.txt [root@localhost data]# ls a.txt  ...

  3. js实时计算价格

    //通过数量,单价的输入,实时显示总价 $("#number,#price").on("input",function(e){ $("#totalPr ...

  4. 学习python os commands socket模块

    import os print(os.getcwd()) #获取当前路径, 导包也是从这个路径下面才能找到 # os.chdir('./..') #返回上一级路径,再获取路径看看 # print(os ...

  5. Test 3.27 T2 旅行

    Description FGD 想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说 FGD 不希望在刚吃过一顿 ...

  6. 【leetcode】516. Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  7. django2 + python3 显示静态文件中的图片

    之前一直搞不出来 是因为图片的问题,步骤也就是固定的几步,到位了就差不多成了 文件夹结构: . ├── HelloWorld │   ├── __init__.py │   ├── __pycache ...

  8. Python3解leetcode Reach a Number

    问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...

  9. 约瑟夫环 c++ 循环输入

    #include<iostream> #include<string.h> #include<cstdio> #include <sstream> us ...

  10. VMware Workstation 官方正式版及激活密钥

    热门虚拟机软件VMware Workstation Pro现已更新至14.1.2,14.0主要更新了诸多客户机操作系统版本,此外全面兼容Wind10创建者更新.12.0之后属于大型更新,专门为Win1 ...