linux编写shell脚本程序(linux脚本编写教程)

目录

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

linux编写shell脚本程序(linux脚本编写教程)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

linux编写shell脚本程序(linux脚本编写教程)
linux编写shell脚本程序(linux脚本编写教程)实验二

循环

循环执行介绍

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

重复运行次数

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

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

linux编写shell脚本程序(linux脚本编写教程)

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

秒鲨号所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈!本站将在三个工作日内改正。
(1)

大家都在看

品牌推广 在线咨询
返回顶部