1基本命令
不带引号,输出变量
[root@spark001 ~]# echo $JAVA_HOME
/usr/java/jdk1.8.0_144
双引号输出变量
[root@spark001 ~]# echo "$JAVA_HOME"
/usr/java/jdk1.8.0_144
单引号输出字符串,不解析变量
[root@spark001 ~]# echo '$JAVA_HOME'
$JAVA_HOME
[root@cqk ~]# echo 'hello word!'
hello word!
[root@cqk ~]# echo hello word!
hello word!
一些特殊的字符串.双引号输出不对
[root@cqk ~]# echo "hello word!"
-bash: !": event not found
不带引号使用特殊字符串2条语句不能放在同一行
[root@cqk ~]# echo hello word!;echo helllo
-bash: !: event not found
[root@cqk ~]# echo 'Hello World!';echo 'Hello'
Hello World!
Hello
[root@cqk ~]# echo hello word;echo helllo
hello word
helllo
2变量定义
变量不管加不加""类型都是string
root@cqk ~]# t1 ="123"
-bash: t1: 未找到命令
[root@cqk ~]# $t1
[root@cqk ~]# t1="123"
[root@cqk ~]# $t1
-bash: 123: 未找到命令
[root@cqk ~]# echo $t1
123
[root@cqk ~]# t1=123
[root@cqk ~]# echo $t1
123
[root@cqk ~]# $t1
-bash: 123: 未找到命令
[root@cqk ~]#
3常用特殊变量
echo $# 脚本传递参数的个数
echo $0 脚本本身的名字
echo $1 脚本传递的第一个参数
echo $2 脚本传递的第2个参数
echo $@ 传递给脚本的所有参数列表
echo $* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
echo $$ 脚本运行当前id进程号
echo $? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
[root@cqk ~]# sh test.sh 1 2 3 4
4
test.sh
1
2
1 2 3 4
1 2 3 4
3306
0
4shell 命令基本运算
命令行变量都被解析成字符串
[root@cqk ~]# one=1
[root@cqk ~]# two=2
会进行字符串拼接
[root@cqk ~]# $one+$two
-bash: 1+2: 未找到命令
用let ,(()) and [] 进行基本运算加减乘除
let命令
[root@cqk ~]# let res=$one+$two
[root@cqk ~]# echo $res
3
[root@cqk ~]# let res=one+two
[root@cqk ~]# echo $res
3
自增
[root@cqk ~]# let one++
[root@cqk ~]# echo $one
2
[root@cqk ~]# echo ${one}
2
[]命令
[root@cqk ~]# res=[one+two]
[root@cqk ~]# echo $res
[one+two]
[root@cqk ~]# res=$[one+two]
[root@cqk ~]# echo $res
4
(())命令
[root@cqk ~]# res=$((one+two))
[root@cqk ~]# echo $res
4
[root@cqk ~]# res=$(($one+$two))
[root@cqk ~]# echo $res
4
[root@cqk ~]# res=(($one+$two))
-bash: 未预期的符号 `(' 附近有语法错误
上边命令只对正数有效
[root@cqk ~]# one=1.2
[root@cqk ~]# two=1.8
[root@cqk ~]# let res=one+two
-bash: let: 1.2: 语法错误: 无效的算术运算符 (错误符号是 ".2")
如果有浮点数参与运算,可以将echo与bc命令结合起来使用
[root@cqk ~]# echo "$one+$two" | bc
3.0
5. 文件描述符与文件重定向
在Linux操作系统当中,文件描述符(File descriptors )与文件的输入输出相关,用整数表示,最常用的三种文件描述符号为stdin、stdout及stderr。stdin表示标准输入(standard input),文件描述符为0;stdout表示标准输出(standard output),文件描述符为1;stderr表示标准错误(standard error),文件描述为2。
标准输出指的是命令执行正常时显示到终端的信息
//利用>命令将标准输出重定向输出到文件,>命令首先清空shell.txt文件
//然后将信息写到文件当中,相当于覆盖了以前文件的内容
[root@cqk ~]# echo "shell 1" > shell.txt
[root@cqk ~]# cat shell.txt
shell 1
//以追加的方式将标准输出重定向到文件
[root@cqk ~]# echo "shell 2" >> shell.txt
[root@cqk ~]# cat shell.txt
shell 1
shell 2
echo “Shell Scripting 1” > shell.txt命令是将标准输出(文件描述符为1)重定向到文件shell.txt当中,它其实相当于echo “Shell Scripting 1” 1> shell.txt,只不过默认可以省略
[root@cqk ~]# echo "shell 3" 1 >> shell.txt
[root@cqk ~]# cat shell.txt
shell 1
shell 2
shell 3 1
标准错误输出也可以重定向到文件当中,与标准输出重定向不同的是,其文件描述符不能省略
将标准错误信息重定向到文件中,这里的文件描述符2不能省略
[root@cqk ~]# cat ls 2 > shellError.txt //不能有空格
cat: ls: 没有那个文件或目录
cat: 2: 没有那个文件或目录
[root@cqk ~]# cat ls 2> shellError.txt
[root@cqk ~]# cat shellError.txt
cat: ls: 没有那个文件或目录
重定向时可以根据将重定向命令结合起来使用标准就匹配1 错误就匹配2
[root@cqk ~]# cat ls 2>shellError.txt 1>shell.txt
[root@cqk ~]# cat shellError.txt
cat: ls: 没有那个文件或目录
[root@cqk ~]# ls 2>shellError.txt 1>shell.txt
[root@cqk ~]# cat shell.txt
readme.txt
shellError.txt
shell.txt
test.sh
在实际使用时,有些时候可能会将标准输出与标准错误输出都重定向到一个文件,此时可以使用下列命令
//&>将标准错误输出转换为标准输出,相当于2>&1
[root@cqk ~]# sl &> shellError.txt
[root@cqk ~]# cat shellError.txt
-bash: sl: 未找到命令
[root@cqk ~]# ls &> shellError.txt
[root@cqk ~]# cat shellError.txt
output.txt
readme.txt
shellError.txt
shell.txt
test.sh
有时命令运行时,对于出错信息我们并不关心,又不想浪费存储空间存储这些错误信息,此时可以将其丢弃,具体做法是将标准错误输出重定向到/dev/null文件当中,/dev/null就像一个垃圾黑洞
[root@cqk ~]# sl 2>/dev/null
标准错误输出或标准输出还可以作为管道命令的标准输入
//标准输出作为另外一个命令的标准输入
[root@cqk ~]# cat shellError.txt | more
output.txt
readme.txt
shellError.txt
shell.txt
test.sh
//标准错误输出作为另一个命令的标准输入
[root@cqk ~]# ls + | more
ls: 无法访问+: 没有那个文件或目录
有时我们既想将标准错误输出或标准输出重定向到一个文件当中,又想它作为另外一个命令的标准输入,这时可以使用tee命令
//标准输出重定向到文件shellError.txt当中,同时又作为more的标准输入
[root@cqk ~]# ls | tee shellError.txt | more
output.txt
readme.txt
shellError.txt
shell.txt
test.sh
6shell数组
shell数组有2种,一种为普通数组,一种为关联数组
普通数组存取通过整数进行,关联数组的存取进行通过字符串进行
//用()定义一个数组,注意数组元素间不能用,否则达不到预期目的
//用,号的话,数组只有一个元素
[root@cqk ~]# arr=(1,2,3,4,5,6)
[root@cqk ~]# echo ${arr[0]}
1,2,3,4,5,6
[root@cqk ~]# arr=(1 2 3 4 5 6)
[root@cqk ~]# echo ${arr[0]}
1
[root@cqk ~]# echo ${arr[6]}
除了()定义数组外,还可以采用逐个赋值的方法
[root@cqk ~]# arrstr[0]="hello"
[root@cqk ~]# arrstr[1]="word"
[root@cqk ~]# echo ${arrstr[0]}
hello
//用*号将输出数组中的所有内容
[root@cqk ~]# echo ${arrstr[*]}
hello word
//${#arrstr[*]}取得数组的长度
[root@cqk ~]# echo ${#arrstr[*]}
2
关联数组的定义与普通数组不一样,关联数组需要使用declare命令进行声明
[root@cqk ~]# declare -A array
[root@cqk ~]# array=([key1]=value1 [key2]=value2 [key3]=value3)
[root@cqk ~]# echo ${array[key1]}
value1
获取关联数组索引
[root@cqk ~]# echo ${!array[*]}
key3 key2 key1
获取普通数组索引
[root@cqk ~]# echo ${!arr[*]}
0 1 2 3 4 5
7shell命令别名
alias
[root@cqk ~]# alias eee='echo ${!arr[*]}'
[root@cqk ~]# eee
0 1 2 3 4 5