Linux shell script

Shell

Shell是一种命令解释器

脚本执行的两种方式
  1. 赋予执行权限,直接执行
    1. chmod 755 hello.sh
    2. ./hello.sh 用直接路径或者间接路径
  2. 通过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对转义字符做输出处理

条件判断

判断文件是否存在

  1. -e判断文件是否存在
  2. -d判断文件是否存在,且是否是目录
  3. -f判断文件是否存在,且是否是普通文件
[user@localhost Desktop]$ test -e b.sh
[user@localhost Desktop]$ echo $?
0
[user@localhost Desktop]$ [ -e b.sh ]
[user@localhost Desktop]$ echo $?
0

判断文件权限

  1. -r判断文件是否存在,且是否拥有读权限
  2. -w判断文件是否存在,且是否拥有写权限
  3. -x判断文件是否存在,且是否拥有执行权限
  4. 所有者所属组其他人只要有一个有该权限就返回0

整数之间的比较

  1. int1 -eq int2判断int1int2是否相等
  2. int1 -ne int2判断int1int2是否不等
  3. int1 -gt int2判断int1是否大于int2
  4. int1 -ge int2判断int1是否大于等于int2
  5. int1 -lt int2判断int1是否小于int2
  6. int1 -le int2判断int1是否小于等于int2

字符串之间的比较

  1. -z判断字符串是否为空
  2. -n判断字符串是否非空
  3. str1 ==str2判断字符串1是否等于字符串2
  4. str1 !=str2判断字符串1是否不等于字符串2

多重条件判断

  1. 判断1 -a 判断2逻辑与
  2. 判断1 -o 判断2逻辑或
  3. !判断逻辑非
if 条件
  1. 单分支if两种格式
if [ 条件判断 ]; then
	程序
fi
if [ 条件判断 ]
	then
		程序
fi
  1. 双分支if格式
if [ 条件判断 ]
	then
		程序
	else
		程序
fi
  1. 多条件分支
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值