目录

shell

shell脚本的执行

shell脚本编写规范

shell 中的变量

变量的算术运算

双小括号 (()) 数值运算命令的用法

let 运算命令的用法

expr 命令的用法

br 命令的用法

$[] 符号的运算示例

shell脚本的条件测试

几种条件测试语句

文件测试操作符

字符串测试操作符

整数二元比较操作符

逻辑操作符

测试表达式 test 、[] 、[[]] 、 (()) 的区别

if 条件判断语句

case 条件判断语句

for循环语句

while循环语句

break、continue、exit 循环控制语句

shell脚本执行scrapy爬虫和python脚本

shell

shell是一个命令解释器,它的作用是解释执行用户输入的命令及程序等。 用户每输入一条命令,shell就执行一条。这种从键盘输入命令,就可以立即得到回应的对话方式,称为交互的方式。

当命令或程序语句不在命令行下执行,而是通过一个程序文件来执行时,该程序文件就被称为shell脚本。 在shell脚本里内置了很多命令、语句及循环控制,然后将这些命令一次性执行完毕,这种通过文件执行脚本的方式称为非交互的方式。 shell脚本语言很适合用于处理纯文本型的数据,而linux系统中几乎所有的配置文件、日志文件,以及绝大对数的启动文件都是纯文本类型的文件。

实验一

利用case语句编写脚本,满足下列要求

1.执行create时根据userfile和passfile建立用户

2.执行delete时根据userfile删除用户

1.编写脚本:

[root@localhost mnt]# vim user_ctrl.sh

#!/bin/bash

read -p “please input the operation (create or delete ): ” operation

//输入你要执行的动作

case $operation in

create) //第一种情况:create

read -p “please input the userfile : ” userfile //提示输入文件

[ -e $userfile ] || { //判断是否存在

echo “$userfile is not exist “

exit 1

}

read -p “please input the passwdfile : ” passfile

[ -e $passfile ] || {

echo “$passfile is not exist “

exit 1

}

userline=`awk ‘begin{n=0}{n++}end{print n}’ $userfile` //计算userfile文件行数

for line_num in `seq 1 $userline` //利用循环建立

do

username=`sed -n “${line_num}p” $userfile` //截取userfile文件第一行内容

password=`sed -n “${line_num}p” $passfile` //截取passfile文件第一行内容

useradd $username //建立用户

echo $password | passwd –stdin $username

done

;;

delete) //第二种情况:delete

read -p “please input the userfile : ” userfile

[ -e $userfile ] || {

echo “$userfile is not exist “

exit 1

}

userline=`awk ‘begin{n=0}{n++}end{print n}’ $userfile`

for line_num in `seq 1 $userline`

do

username=`sed -n “${line_num}p” $userfile`

userdel -r $username

done

;;

*) //第三种情况:其余各种情况

echo eorror!

;;

esac

2.执行:

[root@localhost mnt]# cat userfile

user1

user2

user3

[root@localhost mnt]# cat passfile

123

456

789

[root@localhost mnt]# sh user_ctrl.sh user

please input the operation (create or delete ): hello //输入错误动作

eorror!

[root@localhost mnt]# sh user_ctrl.sh user

please input the operation (create or delete ): create

please input the userfile : user //输入错误文件

user is not exist

[root@localhost mnt]# sh user_ctrl.sh user

please input the operation (create or delete ): create

please input the userfile : userfile

please input the passwdfile : passfile //建立用户

changing password for user user1.

passwd: all authentication tokens updated successfully.

changing password for user user2.

passwd: all authentication tokens updated successfully.

changing password for user user3.

passwd: all authentication tokens updated successfully.

[root@localhost mnt]# sh user_ctrl.sh user

please input the operation (create or delete ): delete //删除用户

please input the userfile : userfile

[root@localhost mnt]# id user1

id: user1: no such user

实验二

循环

循环执行介绍

将某代码段重复运行多次,通常有进入循环的条件和退出循环的条件

重复运行次数

  • 循环次数事先已知
  • 循环次数事先未知

常见的循环的命令:for, while, until

for循环

[root@centos7 ~]#help for

for: for name [in words … ] ; do commands; done

execute commands for each member in a list.

the `for’ loop executes a sequence of commands for each member in a

list of items. if `in words …;’ is not present, then `in “$@”‘ is

assumed. for each element in words, name is set to that element, and

the commands are executed.

exit status:

returns the status of the last command executed.

for ((: for (( exp1; exp2; exp3 )); do commands; done

arithmetic for loop.

equivalent to

(( exp1 ))

while (( exp2 )); do

commands

(( exp3 ))

done

exp1, exp2, and exp3 are arithmetic expressions. if any expression is

omitted, it behaves as if it evaluates to 1.

exit status:

returns the status of the last command executed.

格式1:

for name [in words … ] ; do commands; done for 变量名 in 列表;do 循环体 done for 变量名 in 列表 do 循环体 done

执行机制:
依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束

for循环列表生成方式:

直接给出列表

整数列表:

{start..end}

$(seq [start [step]] end)

返回列表的命令:

$(command)

使用glob,如:*.sh

变量引用,如:$@每个参数为独立字符串,$#传递给脚本的参数的个数,$*全部参数合为一个字符串

范例:面试题,计算1+2+3+…+100的结果

[root@centos8 ~]#sum=0;for i in {1..100};do let sum+=i;done ;echo sum=$sum

sum=5050

[root@centos8 ~]#seq -s+ 100|bc5050

5050

1

2

3

4

范例:

[root@centos8 ~]#cat /data/scripts/for_sum.sh

#!/bin/bash

sum=0

for i in $* ; do

let sum+=i

done

echo sum=$sum

[root@centos8 ~]#bash /data/scripts/for_sum.sh 1 2 3 4 5 6

sum=21