打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
shell脚本基础-2
userphoto

2022.12.26 北京

关注

1.脚本执行的4种方法

#!/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 跟踪调试脚本

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 :当命令进行读取时禁止打印输入

3.输出格式化

1.C语言风格格式化输出

#!/bin/bash

printf " % -5s %-10s %-4s\n" NO. Name Mark
printf " % -5s %-10s %-4.2f\n" 1 joe 80.55 
printf " % -5s %-10s %-4.1f\n" 2 joe 80.5
printf " % -5s %-10s %-4.3f\n" 3 joe 80.500

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"

4.数据类型 

1.字符串

获取字符串长度

echo ${#str}

2.数组

数组的定义
方法一:
arr=(1 2 3 4 5)
方法二:
arr[0]=1
arr[1]=2
arr[2]=3
echo ${arr[*]}
1 2 3

打印数组中的值

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

3.关联数组

普通数组只能使用整数作为索引值,而关联数组可以使用任意文本作为索引值(有点类似于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

5.重定向

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

6.变量

 1.只读变量

#!/bin/bash
readonly hours_per_day=24
hours_per_day=12

更改变量会触发异常

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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux变量
Bash脚本编程基础
Shell入门-Shell变量数值计算
Shell脚本编程初体验
Linux学习笔记(一)——Shell
Shell中的函数调用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服