Shell
Shell是一种命令解释器
脚本执行的两种方式
- 赋予执行权限,直接执行
chmod 755 hello.sh
./hello.sh
用直接路径或者间接路径
- 通过bash调用执行脚本
bash hello.sh
[user@localhost Desktop]$ vi hello.sh
[user@localhost Desktop]$ echo --help
--help
[user@localhost Desktop]$ chmod u+x hello.sh
[user@localhost Desktop]$ ./hello.sh
I love you
[user@localhost Desktop]$ bash hello.sh
I love you
nano first.sh
echo"hello world"
date
s19@GOJ:~$ nano first.sh
s19@GOJ:~$ ./first.sh
-bash: ./first.sh: 权限不够
s19@GOJ:~$ bash first.sh
2021年 06月 08日 星期二 16:05:48 CST
HEllo popop
#用bash来执行脚本文件中的命令
s19@GOJ:~$ chmod a+x first.sh
s19@GOJ:~$ ./first.sh
2021年 06月 08日 星期二 16:06:45 CST
HEllo popop
#增加执行权限
2021年 06月 08日 星期二 16:07:25 CST
HEllo popop
s19@GOJ:~$ source first.sh
2021年 06月 08日 星期二 16:07:33 CST
HEllo popop
脚本不一样的进程
#!/bin/bash
#!/bin/csh
#! /usr/bin/python
变量赋值
s19@GOJ:~$ name='tom'
s19@GOJ:~$ echo $name
tom
s19@GOJ:~$ a=$name
s19@GOJ:~$ echo $a
tom
env
查看所有环境变量- 对于自定义变量name,在子bash里面是看不见的
export name
变成环境变量,是赋值,如果在子进程里面给name修改,父进程的name不会变.
或者source
来执行脚本文件是直接在当前shell进程中解释执行,所以如果修改,就改掉了
使用变量需要进行声明,默认类型字符串
使用变量要加$
s19@GOJ:~$ a=1
s19@GOJ:~$ a=$a+1
s19@GOJ:~$ echo $a
1+1
s19@GOJ:~$ declare -i a
s19@GOJ:~$ a=4
s19@GOJ:~$ a=a+1
s19@GOJ:~$ echo $a
5
命令替换:命令结果保存在变量里面
readonly
只读unset
清空变量read -p 'Please input my name' name
echo "Hello, $name"
要双引号,引用变量的值
s19@GOJ:~$ num=`who|wc -l`
s19@GOJ:~$ echo $num
51
s19@GOJ:~$ num=$(who|wc -l)
s19@GOJ:~$ echo $num
52
s19@GOJ:~$ unset num
s19@GOJ:~$ echo $num
s19@GOJ:~$ name='Jacl'
s19@GOJ:~$ readonly name
s19@GOJ:~$ name='333'
-bash: name: 只读变量
-e对转义字符做输出处理
条件判断
判断文件是否存在
-e
判断文件是否存在-d
判断文件是否存在,且是否是目录-f
判断文件是否存在,且是否是普通文件
[user@localhost Desktop]$ test -e b.sh
[user@localhost Desktop]$ echo $?
0
[user@localhost Desktop]$ [ -e b.sh ]
[user@localhost Desktop]$ echo $?
0
判断文件权限
-r
判断文件是否存在,且是否拥有读权限-w
判断文件是否存在,且是否拥有写权限-x
判断文件是否存在,且是否拥有执行权限- 所有者所属组其他人只要有一个有该权限就返回0
整数之间的比较
int1 -eq int2
判断int1
和int2
是否相等int1 -ne int2
判断int1
和int2
是否不等int1 -gt int2
判断int1
是否大于int2
int1 -ge int2
判断int1
是否大于等于int2
int1 -lt int2
判断int1
是否小于int2
int1 -le int2
判断int1
是否小于等于int2
字符串之间的比较
-z
判断字符串是否为空-n
判断字符串是否非空str1 ==str2
判断字符串1是否等于字符串2str1 !=str2
判断字符串1是否不等于字符串2
多重条件判断
判断1 -a 判断2
逻辑与判断1 -o 判断2
逻辑或!判断
逻辑非
if 条件
- 单分支if两种格式
if [ 条件判断 ]; then
程序
fi
if [ 条件判断 ]
then
程序
fi
- 双分支if格式
if [ 条件判断 ]
then
程序
else
程序
fi
- 多条件分支
if [ 条件判断1 ]
then
程序1
elif [ 条件判断2 ]
then
程序2
else
程序
fi
while 循环
while [ 条件判断式 ]
do
程序
done
例子,求合1-11
[user@localhost Desktop]$ cat while.sh
#! /bin/bash
read -p "hello"
i=0
sum=0
while [ "$i" -le 10 ]
do
echo "while is $i ,sum is $sum"
i=$(($i+1))
sum=$(($sum+$i))
done
echo "total sum is $sum"
[user@localhost Desktop]$ ./while.sh
hello
while is 0 ,sum is 0
while is 1 ,sum is 1
while is 2 ,sum is 3
while is 3 ,sum is 6
while is 4 ,sum is 10
while is 5 ,sum is 15
while is 6 ,sum is 21
while is 7 ,sum is 28
while is 8 ,sum is 36
while is 9 ,sum is 45
while is 10 ,sum is 55
total sum is 66
for 循环
for 变量1 in 值1 值2 值3
do
程序
done
例子
[user@localhost Desktop]$ ./for.sh
num is 3
num is 2
num is 6
num is 7
[user@localhost Desktop]$ cat for.sh
#!/bin/bash
for i in 3 2 6 7
do
echo "num is $i"
done
[user@localhost Desktop]$ ./for2.sh
sum is 1
sum is 3
sum is 6
sum is 10
sum is 15
sum is 21
sum is 28
sum is 36
sum is 45
sum is 55
[user@localhost Desktop]$ cat for2.sh
#!/bin/bash
sum=0
for ((i=1;i<=10;i=i+1))
do
sum=$(($i+$sum))
echo "sum is $sum"
done