一篇文章教会你Linux编写Shell脚本

一篇文章教会你Linux编写Shell脚本目录 ShellShell 脚本的执行 Shell 脚本编写规范 Shell 中的变量变量的算术运算双小括号 数值运算命令的用法 let 运算命令的用法 expr 命令的用法 br 命令的用法 符号的运算示例 Shell 脚本的条件测试几种条件

欢迎大家来到IT世界,在知识的湖畔探索吧!

目录

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脚本



欢迎大家来到IT世界,在知识的湖畔探索吧!

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编写Shell脚本

实验二

循环

循环执行介绍

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

重复运行次数

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

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

一篇文章教会你Linux编写Shell脚本

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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/96012.html

(0)
上一篇 1天前
下一篇 1天前

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信