shell
- 01.什么是shell/Shell?
1.什么是shell脚本呢?shell使用c语言实现,shell既是一种脚本语言,也是一种程序设计语言。
2.shell脚本语言和python一样都是解释型语言,动态类型,但是与python不同的是shell是弱类型,python则是强类型。
- 02.shell中的变量
1.shell中的变量不用申明类型
2.在变量赋值时中间不能有空格
3.用$来取变量的值
4.用#注释
5.用unset命令删除变量
[skin@bogon shell]$ cat test.sh
#!bin/bash
str='hello,world'
echo $str
str=1234567
echo $str
#程序执行结果
[skin@bogon shell]$ sh test.sh
hello,world
1234567
- 03.shell中的运算符
1.整形的运算符
2.字符串的运算符
[skin@bogon shell]$ cat test.sh
#!bin/bash
# 整型
# -gt :大于
# -lt :小于
# -eq :等于
# -ge :大于等于
# -le :小于等于
int=123
if test $int -eq 123 ;then
echo 'eq'
elif test $int -gt 123 ;then
echo 'gt'
else
echo 'lt'
fi
# 字符串
str='miss'
if test "$str" == 'miss';then
echo '=='
else
echo '!='
fi
#程序执行结果
[skin@bogon shell]$ sh test.sh
eq
==
- 04.shell中的判断
1.用test
2.[ ]
3.下面举例:
[skin@bogon shell]$ cat test.sh
#!bin/bash
# []
str="hello,world"
if [ "X$str" == "Xhello,world" ];then
echo 'YES'
else
echo 'NO'
fi
# test
if test "X$str" == "Xhello,world" ;then
echo 'YES'
else
echo 'NO'
fi
#程序执行结果
[skin@bogon shell]$ sh test.sh
YES
YES
- 05.shell中的循环
1.C/C++一样都有for,while循环
2.但是shell的for有一种比较特殊的用法,那就是和python一样都有for in
下面看下实际用法
[skin@bogon shell]$ cat test.sh
#!bin/bash
#for
#C语言形式
for((i=1;i<3;++i))
do
echo "now_1 : $i"
done
#python形式 ( {}包裹的左闭右闭 )
for i in {1..3}
do
echo "now_2 : $i"
done
#while
i=1
while [ $i -lt 3 ]
do
echo "now_3 : $i"
let i++
done
#程序执行结果
[skin@bogon shell]$ sh test.sh
now_1 : 1
now_1 : 2
now_2 : 1
now_2 : 2
now_2 : 3
now_3 : 1
now_3 : 2
- 06.shell中的数组
1.数组可以放多个值,不用初始化大小
2.数组也是下标从0开始
3.数组中元素用空格分隔
[skin@bogon shell]$ cat test.sh
#!bin/bash
#1. ${array[@]} 或者${array[*]} 代表array数组中所有元组
#2. ${#array[@]} 或者${#array[*]} 获取数组长度
#3. ${array[index]} 进行访问特定元素
array=(1 2 3)
for i in ${array[@]}
do
echo $i
done
for ((i=0;i<${#array[@]};++i))
do
echo ${array[i]}
done
#程序执行结果
[skin@bogon shell]$ sh test.sh
1
2
3
1
2
3
- 07.shell中的重定向及文件读写
1.shell中的文件操作,相比其他语言要就按但很多,因为他可以用重定向解决了一切。
2. " > " " < " 单纯重定向 " >>" “<<” 追加重定向
3.下面进行实例演示:
[skin@bogon shell]$ cat test.sh
#!bin/bash
#利用重定向写入文件file1 ">>" 追加写入否则覆盖
echo 'Today is good day' >file1
echo 'now i am writing 优快云' >>file1
#读取文件到并且重定向到file2文件中
while read oneline
do
echo $oneline
done <file1 >>file2
#程序执行结果
[skin@bogon shell]$ sh test.sh
[skin@bogon shell]$ cat file1
Today is good day
now i am writing 优快云
[skin@bogon shell]$ cat file2
Today is good day
now i am writing 优快云
#当然也可以在外面将1重定向到文件中
- 08.引用其他文件
1.和C语言的include,python中的import一样,shell也可以引用其他文件。
2.语法 source filename 或者 . filename
3.实例测一下
#hello.sh文件
[skin@bogon shell]$ cat hello.sh
#!bin/bash
echo 'now in hello.sh '
#test.sh文件
[skin@bogon shell]$ cat test.sh
#!bin/bash
source ./hello.sh
echo 'now in test.sh'
#程序执行结果
[skin@bogon shell]$ sh test.sh
now in hello.sh
now in test.sh
- 其他一些特殊的文件
$0 : 相当于c语言中的 argv[0],即第一个参数 $# : 相当于c语言中argc-1 ,即参数列表个数减1 $@ : 相当于所有argv,即 $1 ,$2 ,$3 ... $? : 上个进程的退出码 $$ : 当前shell的进程号
[skin@bogon shell]$ cat test.sh
#!bin/bash
echo " \$0 -> $0"
echo " \$@ -> $@"
echo " \$# -> $#"
echo " \$? -> $?"
echo " \$$ -> $$"
#程序执行结果
[skin@bogon shell]$ sh test.sh argv
$0 -> test.sh
$@ -> argv
$# -> 1
$? -> 0
$$ -> 8071
如有错误,可以私信我,这里表示感谢!