1、echo中双引号和单引号的区别
使用单引号时,括号内的变量就会失效
[root@server ~]# a=1
[root@server ~]# echo $a
1
[root@server ~]# echo '$a'
$a
;本来的作用是执行下一条名,使用单引号则失效
[root@server ~]# echo hello;echo world
hello
world
[root@server ~]# echo 'hello;echo world'
hello;echo world
*普通字符变量,尽量把变量的内容用双引号括起来""
*单纯数字变量的内容可以不加引号
*希望变量的内容原样输出时需要加单引号''
*希望变量值引用命令并获取命令结果时就用反引号``或$()
2、测试当前用户是否为root用户脚本
[root@server testsh]# cat superuser.sh
#! /bin/bash
if [ $UID -ne 0 ]; then
echo Non root user. Please run as root.
else
echo "Root user"
fi
[root@server testsh]# ./superuser.sh
Root user
3、数学计算
3.1 let(只能用于整数运算)
[root@server ~]# a=1
[root@server ~]# b=2
[root@server ~]# let c=a+b
[root@server ~]# echo $c
3
3.2 bc(支持浮点数运算)
[root@server ~]# echo "4 * 0.56" | bc
2.24
[root@server ~]# a=6
[root@server ~]# b=`echo "$a * 1.5" | bc`
[root@server ~]# echo $b
9.0
[root@server ~]# echo "$a * 3.3" | bc
19.8
设定小数精度(数值范围)
[root@server ~]# echo "scale=3;9/8" | bc
1.125
进制转换
[root@server testsh]# cat jzzh.sh
#!/bin/bash
#用途;进制转换
a=100
echo "obase=2;$a" | bc
b=1100100
echo "obase=10;ibase=2;$b" | bc
[root@server testsh]# sh jzzh.sh
1100100
100
3.3 双括号(())数值运算实践
[root@server testsh]# cat jisuan.sh
#!/bin/bash
a=6
b=3
echo "a+b=$(($a+$b))"
echo "a-b=$(($a-$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@server testsh]# bash jisuan.sh
a+b=9
a-b=3
a*b=18
a/b=2
a**b=216
a%b=0
计算平方及平方根
[root@server ~]# echo "10^10" | bc
10000000000
[root@server ~]# echo "sqrt(100)" | bc
10
4、文件描述符合重定向
文件描述符是与文件输入、输出相关联的整数
用来跟踪已打开的文件
最常见的文件描述符是stdin(标准输入)、stdout(标准输出)、stderr(标准错误)
可以将某个文件描述符的内容重定向到另一个文件描述符中
4.1 使用 > 和 >> 将输出文本重定向或保存到一个文件中
## 使用 > ,temp.txt首先会被清空
[root@server testsh]# echo "This is a sample text 1" > temp.txt
## 使用 >> ,将文本追加到目标文件
[root@server testsh]# echo "This is a sample text 2" >> temp.txt
[root@server testsh]# cat temp.txt
This is a sample text 1
This is a sample text 2
4.2 对标准错误的重定向
当命令输出错误信息时,stderr信息就会被打印出来
[root@server ~]# ls +
ls: 无法访问+: 没有那个文件或目录
命令执行状态,成功返回0,错误返回非0
[root@server ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@server ~]# echo $?
0
[root@server ~]# ls +
ls: 无法访问+: 没有那个文件或目录
[root@server ~]# echo $?
2
5、数组与关联数组
5.1 数组操作
## 定义一个数组
[root@server ~]# var=(1 2 3 4 5 6)
## 重新给数组赋值
[root@server ~]# var[0]="test1"
[root@server ~]# var[1]="test2"
[root@server ~]# var[2]="test3"
[root@server ~]# var[3]="test4"
[root@server ~]# var[4]="test5"
[root@server ~]# var[5]="test6"
## 打印特定索引数组的元素内容
[root@server ~]# echo ${var[1]}
test2
[root@server ~]# intex=5
[root@server ~]# echo ${var[$intex]}
test6
## 查看数组
[root@server ~]# echo ${var[*]}
test1 test2 test3 test4 test5 test6
## 查看数组长度
[root@server ~]# echo ${#var[*]}
6
5.2 关联数组
普通数组中只能用整数作为数组索引,关联数组中可以用任意文本作为数组索引。
5.2.1 定义关联数组
##使用单独的语句讲一个变量名声明为关联数组
[root@server ~]# declare -A ass_array
##两种方法将元素添加到关联数组里
(1)利用内嵌索引-值
[root@server ~]# ass_array=([index1]=val1 [index2]=val2)
(2)使用独立的索引-值
[root@server ~]# ass_arry[index1]=val1
[root@server ~]# ass_arry[index2]=val2
##显示数组的内容
[root@server ~]# echo "The first value is ${ass_arry[index1]}"
The first value is val2
5.2.2 列出数组索引
[root@server ~]# echo ${!ass_arry[*]}
6.获取终端信息
使用stty实现输入的密码不显示,-echo禁止输出送到终端,echo为允许
[root@server testsh]# cat nopass.sh
#!/bin/bash
echo -e "Enter password: "
stty -echo
read password
stty echo
echo
echo Password read.
[root@server testsh]# sh nopass.sh
Enter password:
Password read.
7.日期与延时
##输入指定的日期判断为星期几
[root@server ~]# date --date "Jan 21 2017" +%A
星期六
##测试脚本执行所需时间
[root@server testsh]# times jzzh.sh
0m0.012s 0m0.060s
0m0.001s 0m0.013s
8.函数和参数
8.1 函数
##定义函数
[root@server testsh]# function fname()
> {
> statements;}
#或者
[root@server testsh]# fname()
> {
> statements;}
##调用函数
[root@server testsh]# fname;
##参数传递给函数
[root@server testsh]# fname arg1 arg2;
[root@server testsh]# fname()
> {
> echo $1 42;
> echo "$@";
> echo "$*";
> return 0;
> }
[root@server testsh]# fname 1 2;
1 42
1 2
1 2
8.2 Bash函数的技巧
1.递归函数
2.导出函数
将函数像环境变量一样使用export导出后,函数的作用域就可以扩展到子进程中了
[root@server testsh]# export -f fname
3.读取命令返回值(状态)
##若命令成功,状态为0,否则为非0
[root@server testsh]# ping www.baidu.com
^C
[root@server testsh]# echo $?;
130
##监测命令是否成功结束的脚本
[root@server testsh]# cat sucess_test.sh
#!/bin/bash
IFC="ifconfig"
status
$IFC
if [ $? -eq 0 ];
then
echo "$IFC executed sucessfully"
else
echo "$IFC terminated unsucessfully"
fi
4.向命令传递参数
9.读取命令序列输出
组合多个命令以及如何读取其输出
[root@server testsh]# ls | cat -n > out.txt
[root@server testsh]# cat out.txt
1 jzzh.sh
2 nopass.sh
3 out.txt
4 sucess_test.sh
5 superuser.sh
6 temp.txt
10.条件表达式
[root@server testsh]# [ -f out.txt ]&&echo 1 || echo 0
1
[root@server testsh]# [ -r out.txt ]&&echo 1 || echo 0
1
[root@server testsh]# [ -x out.txt ]&&echo 1 || echo 0
0
[root@server testsh]# [ -w out.txt ]&&echo 1 || echo 0
1
[root@server testsh]# [ -e out.txt ]&&echo 1 || echo 0
1
[root@server testsh]# [ -d out.txt ]&&echo 1 || echo 0
0
命令执行判断依据:&&与||
cmd1&&cmd2:
若cmd1执行完毕且正确执行($?=0),则开始执行cmd2
若cmd1执行完毕且错误执行($?!=0),则开cmd2不执行
cmd1||cmd2:
若cmd1执行完毕后正确执行($?=0),则cmd2不执行。
若cmd1执行完毕后为错误($?!=0),则开始只执行cmd2
11.字符串表达式
-n : 判断字符串是否有长度,真为1,否为0
-z : 判断字符串是否有长度,真为0,否为1
= : 判断字符串是否相等
!= : 判断字符串是否不等
[root@server home]# test=1
[root@server home]# [ -n "$test" ]&&echo 1||echo 0
1
[root@server home]# [ -z "$test" ]&&echo 1||echo 0
0
[root@server home]# [ "$test" = "1" ]&&echo 1||echo 0
1
[root@server home]# [ "$test" != "1" ]&&echo 1||echo 0
0
12.整数表达式

[root@server testsh]# (( 2 > 1)) && echo 1 || echo 0
1
[root@server testsh]# [ 2 -eq 1 ] && echo 1 || echo 0
0
[root@server testsh]# [ 1 -eq 1 ] && echo 1 || echo 0
1
[root@server testsh]# [ 2 -ne 1 ] && echo 1 || echo 0
1
##测试结果结论
1、整数加双引号也是对的
2、[[]]用-eq的写法也是对的。[[]]用>写法也可能不对,只比较第一位,逻辑结果不对
例如:
[root@server testsh]# [[ 2 > 11 ]] && echo 1 || echo 0
1
3、[]用>的写法语法没错,逻辑结果不对
工作场景:推荐[]的-eq用法
13.逻辑操作符

[root@server testsh]# f1=/etc/rc.local ;f2=/etc/services
[root@server testsh]# [ -f "$f1" -a -f "$f2" ] && echo 1 || echo 0
1
[root@server testsh]# [[ -f "$f1" && -f "$f2" ]] && echo 1 || echo 0
1
[root@server testsh]# [ -f "$f1" ] && [ -f "$f2" ] && echo 1 || echo 0
1
[root@server testsh]# a=1;b=2
[root@server testsh]# [ $a -eq 2 -a $b -eq 2 ] && echo 1 || echo 0
0
[root@server testsh]# [ $a -eq 1 -a $b -eq 2 ] && echo 1 || echo 0
1
[root@server testsh]# [ $a -eq 2 -o $b -eq 2 ] && echo 1 || echo 0
1
[root@server testsh]# [ $a -eq 2 -o "$b" -eq "2" ] && echo 1 || echo 0
1
14.输出有颜色的字
[root@server testsh]# echo -e "\033[30m 黑色字test trainning \033[0m"
黑色字test trainning
[root@server testsh]# echo -e "\033[31m 红色字test trainning \033[0m"
红色字test trainning
[root@server testsh]# echo -e "\033[32m 绿色字test trainning \033[0m"
绿色字test trainning
[root@server testsh]# echo -e "\033[33m 棕色字test trainning \033[0m"
棕色字test trainning
[root@server testsh]# echo -e "\033[34m 蓝色字test trainning \033[0m"
蓝色字test trainning
[root@server testsh]# echo -e "\033[35m 洋红色字test trainning \033[0m"
洋红色字test trainning
[root@server testsh]# echo -e "\033[36m 蓝绿色字test trainning \033[0m"
蓝绿色字test trainning
[root@server testsh]# echo -e "\033[37m 白色字test trainning \033[0m"
白色字test trainning
