唯米7s手机怎么样(Linux怎么使用ss命令查看系统的socket状态)
唯米7s手机怎么样,Linux怎么使用ss命令查看系统的socket状态? ss是Socketstatistics的缩写。顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netsta...
2023-04-15
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

[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
以上内容就是为大家推荐的linux编写shell脚本程序(linux脚本编写教程)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题
内容来源于网络仅供参考版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。
本文标题:linux编写shell脚本程序(linux脚本编写教程)
本文地址:https://www.xiaoyin02.com/shcs/97059.html
相关文章
唯米7s手机怎么样,Linux怎么使用ss命令查看系统的socket状态? ss是Socketstatistics的缩写。顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netsta...
2023-04-15
手机表格怎么做表格,苹果手机上怎么编写word文档和excel表格? 可以在iPhone手机上面下载个WPS Office来编写word文档和excel文档,具体操作步骤如下:; 1、首...
2023-03-24
手机怎么做app,如何在手机上编写c程序? 1、我们在手机上下载C语言学习APP并打开。 2、此时将会弹出此页面,我们点击编程选项卡。 3、此时,我们在这...
2023-03-21
手机怎么编写程序,华为手机一直出现很抱歉? 这种情况一般都是由于程序不兼容或者程序本身存在BUG发生的。升级应用程序或者下个其他类似的软件应该...
2023-03-18
手机怎么编写简历,用什么简历App最好? 简历制作模板这方面的软件和平台太多了!简单给您推荐几个吧! 一、五百丁简历 这个是大家求职常用的一款简...
2023-03-16
热点文章
2021年独生子女补贴新政策是真的吗(独生子女证有有效期吗)
2021年国庆节阅兵仪式几点开始几点结束(2021年国庆节还有阅兵吗)
鼠目寸光一点红是什么生肖动物(鼠目寸光一点红)指什么生肖,紧密
k0到k9的玩法大全(强制gc的玩法和注意事项)
入土为安是什么生肖《入土为安》打一个生肖动物,词语解释
浙江12月底全面停工是真的吗(浙江什么时候放假停工)
如何做t(t怎么把p做哭)
北京口碑最差的三甲医院(北京301医院最擅长什么)