part-1

#!/bin/bash
:<<FTP
#test [ 1 -eq 2] #条件测试
x="abc" #不允许有空格
y="abc"

[ "$x" != "$y" ] #字符相等判断
echo $?

#test
test "$x" ="$y"
echo $?

test -n $x #判断字符是为空
echo $?

x=1.5 #数字条件测试
y=8
#数值判断符
[ "$x" -eq "$y" ] #<=
echo $? #执行判断的必须为相同的数据类型

test "$x" -gt "$y" #>=
echo $?

test 1 -eq 1 #=
echo $?

#文件测试

FTP

echo "对/home下的document文件测试"

test -a "/home/document" #判断文件是否存在
echo $? #返回状态布尔值
mkdir -p /home/document
test -a "/home/document" #再次判断文件是否存在
echo $?

cd /home/
touch 1
test -x 1 #测试文件是否可执行
echo $?
echo "sorry you document can't execute"
chmod 777 1
test -x 1 #再次测试文件是否可执行
[ -x 1 ]
echo $?

cd /home
chmod 333 1 #去除1的x "r",保留x 的 "w" 权限
[ -r 1 -o -w 1 ] #-a -w 1 ] #逻辑运算符-a,-o,!
echo $? #判断1是否可读并且可写

#条件判断

if [ -a /etc/sysconfig/selinux ];then #if...then单分支
echo "你的linux系统加固了selinux安全子层"
fi

#echo "shell脚本" > ./test
if:;then echo "active";fi

#&&代替if测试
test "$(w)"="root" && (you login user not is superadmin;exit 1)

echo "please input your number:" #if..then..else双分支
read number #接受用户输入
if [ "$number" -gt 10 ];then
echo "your numbe gater than ten"
else
echo "your number letter than ten"
fi

part-2

#!/bin/bash #嵌套双分支
echo "input your socre:"
read socre
if [ -z "$score" ]; then
echo "your score is null,please input aegin"
else
if [ "$score" -lt 100 -a "$score" -gt 0];then
echo "your score is right,you can continue excute"
else
if [ "$score" = 90 ];then
echo "Your score level is A"
else
if [ "$score"=70];then
echo "Your score level is B"
if [ "$score"=60];
echo "Your score level is C"
else
echo "Your score level is D"

fi
fi
fi
#statements
fi

echo "please input your score" #多分支
read score
if [ -z "$score" ];then #判断分数是否为空值
echo "your score is null,please input aegin"
else
if [ "$z" -ge 100 -a "$z" -le 0 ];then #判断分数是否在[0,100]之间
echo "your score not between 0 and 100,please agin"
read score #重新读取用户输入
else
if[ "$z" -le 60 ]; then
echo "your score is D"
elif[ "$z" -le 70 ];then
echo "your score is C"
elif[ "$z" -le 80 ];then
echo "your score is B"
else
echo "your score is A"
fi

fi
fi

part-3

#!/bin/bash
if [ -e "$1" ];then #判断文件是否存在
echo "$1 file have"
exit 1 #文件不存在状态码
else
touch "$1"
echo "$1 file created" #不存在创建
exit 0 #文件存在状态码
fi
#case..in循环分支语句
echo "please input your char"
read char
case $char in #接受变量
[[:upper:]]) #判断条件1
echo "your char is letter";; #结束
[[:lower:]])
echo "your char is lower";;
[0-9])
echo "your char is number";;
*) #默认
echo "your char is other";;
esac #终止循环$

wile getopt ":pq:"y: #while循环
do
case $y in
"p")
echo "$y";;
"q")
echo "$x $y";;
"::")
echo "$x";;
"?")
echo "$x
"*")
echo "z";;";;
esac
echo "w"
done
#算数运算符
result=`expr 3 / 2`
echo "$result" #expr:算数计算符
#result=`expr \(1 * 2\)\ + 1`
#echo "$result"

result=$(( 10 + 90 / 2 )) #$(()):算数运算符
echo "$result"

#$[]算数运算符
result=$[ 5*2 ]
echo "$result"

n=1 #let算数运算符
let n=n+1
echo "$n"

result=`expr 4 += 6`
echo "$result"

part-4

#!/bin/bash

result=$((4 += 6)) #复合算数运算符
echo "$result"

:<<FTP #位运算符
x=let "2 >> 1 " #2的二进制向右移一位
echo "$x"
y=$[ "$x" ~= 1 ] #变量x与1异或
echo "y"
z=`expr 1 ^ 0` #1和0取反
echo $z
FTP
result=$[ 2 << 1 ]
echo "$result"
x=1
result=$[ x |= 2 ] #位复合运算符
echo "$result"

y=5 #自增自减运算符
result=$[ y+(++y) ]
echo "$result"
result=$((result++))
echo "$result"
result=$[ --result ]
echo "$result"

#法一:
((x=201)) #二,八,十六进制
echo "$x"
((y=021))
echo "$y"
((z=0x21))
echo "$z"
#法二
((x=2#201))
echo "$x"
((y=8#201))
echo "$y"
((z=16#201))
echo "$z"

part-5

#!/bin/bash

#for i in `ls .` #for..in循环
#do
# if echo "$i" | grep "a"
# then
# echo "$i"
# fi
#done

for i in [1..10] #1 2 3 5 8
do
echo "The Cureent number is $i"
done

total=0 #for循环
for i in {1..100..2} #1 2 3 5 8 #{start,end,步长}
do
let "total=total+i"
#echo "The Cureent number is $i"
done
echo "total is $total"

for days in {M T S T F S S} #列表值
do
echo "Today is $days"
done

for x in $(ls) #* #"ls" #循环显示命令
do
echo "$x"
done

echo "$*" #接受输入所有的参数
for arg in "$*"
do
echo "${arg}"
done

for arg in #不带列表的for
do
echo "${arg}"
done

x="a"
for ((;;)) #类c的死循环 #((i=0;i<=5;i++)) #类c的for
do
echo "$x"
done

array=(M,T,T,F,S,S) #处理数组
for i in ${array[*]}
do
echo "$i"
done

i=1
until [[ "$i" -gt 10 ]]; do #开始until循环,当i>10时退出until循环
let "square=i*i" #条件
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done

part-6

#!/bin/bash
i=1
until [[ "$i" -gt 10 ]]; do
#开始until循环,条件测试i>10时退出until循环
let "square=i*i" #let条件计算
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done #中断
i=1
while [[ "$i" -lt 10 ]]; do #while循环
let "square=i*i" #let条件计算
echo "$i*$i=$square"
let "i++" #自增计数器
#statements
done

echo "please input your number,must between 1 and 10,and 0 exits"
read number
while [[ "$number" != 0 ]]; do #while判断计数
#echo "you can input"
#read number #再次读取用户输入
if [ "$number" -lt 5 ];then #多分支判断准确输入
echo "sorry,your number lt 5"
read number
elif [ "$number" -gt 5 ];then
echo "sorry,your number gt 5"
read number
else
echo "Configuration,You are right!"
exit 0 #退出
fi
#statements

#statements
done

i=1
until [[ "$i" -ge 21 ]];do
userdel user$i #代入计数器i,执行创建用户
#echo "123" | passwd --stdin user$i >> /dev/null #设置密码
let "i++" #自增
#statements
done

while [[ i=0;i<9;i++ ]]; do
while [[ j=0;j<i;i++ ]]; do
if [[ j -gt 9 ]]; then
let "product=i*j"
printf "$i*$j=$product"
#statements
else
echo " "
echo " "
fi
#statements
done
echo
#statements
done

part-7

#!/bin/bash
for((i=0;i<10;i++));do #嵌套循环
for (( j=0;j<i;j++ )); do
let "product=i*j"
printf "$i*$j=$product"
if [[ $product -gt 10 ]]; then

echo " "
else
echo " "
#statements
fi
#statements
done
echo
done

转载于:https://www.cnblogs.com/activecode/p/9848309.html

【shell脚本学习-3】相关推荐

  1. Shell脚本(学习笔记1)

    shell脚本学习记录 为什么学习shell脚本? 在一些复杂的linux维护工作过程中,大量的重复性的输入和交互操作不但费时费力,而且容易出现错误:然而编写shell脚本程序,可以批量处理.自动化的 ...

  2. BASH命令和SHELL脚本学习

    BASH命令和SHELL脚本学习 转载于:https://www.cnblogs.com/huolong123/p/6228049.html

  3. shell 脚本学习及troubleshooting

    shell 脚本学习及troubleshooting Shell问题一: $ FILENAME="My Document" 含有空格的文件名 $ ls $FILENAME 列出来试 ...

  4. Shell脚本学习-阶段二

    文章目录-Shell脚本学习阶段二 前言 shell脚本实操2 1.获取随机字符串或数字 2.定义一个颜色输出字符串函数 3.批量创建用户 4.检查软件包是否安装 5.检查服务状态 6.检查主机存活状 ...

  5. linux shell脚本学习

    linux shell脚本学习笔记 文章目录 linux shell脚本学习笔记 一.脚本入门 1.我的第一个linux脚本 2.关于date命令的知识 二.shell脚本中的变量 1.为什么脚本需要 ...

  6. shell脚本学习笔记一

    又开始写学习笔记了,呵呵... 今年打算选择一门技术系统的学习一下.编程语言.虚拟机技术.数据库.大数据.云计算在这些种类中,我选择了shell编程.为什么选择shell呢?也没有什么具体的原因.以前 ...

  7. shell 脚本学习笔记

    shell 脚本学习总结: 文件表达式 -e filename 如果 filename存在,则为真 -s file exists and has a size greater than zero. 判 ...

  8. linux命令封装sh,shell脚本学习之调用脚本将文件打包zip的方法示例

    前言 本文主要给大家介绍的是关于调用脚本将文件打包zip的相关资料,分享出来供大家参考学习,下面来一起看看详细的介绍: 最近刚刚接触shell脚本,写了一点简单的练手.这里是用python调用脚本执行 ...

  9. Shell脚本学习-阶段三

    文章目录-Shell学习阶段三 前言 1.用shell脚本批量建立Linux用户 2. 编写shell脚本,将/usr/local/test目录下大于100k的文件转移到/tmp目录下: 3.通过ap ...

  10. Shell脚本学习指南(三)——文本处理工具

    文章目录 排序文本 行的排序 以字段的排序 文本块排序 sort的效率 sort的稳定性 sort小结 删除重复 重新格式化段落 计算行数.字数以及字符数 打印 打印技术的演化 其他打印软件 提取开头 ...

最新文章

  1. 全球人形机器人接连突破 拟人度越来越高
  2. android 锁屏
  3. Acwing第 40 场周赛【完结】
  4. 《陶哲轩实分析》部分勘误
  5. oracle 的一些基础查询
  6. P4451-[国家集训队]整数的lqp拆分【生成函数,特征方程】
  7. linux系统的wps办公软件,Linux学习-7:Linux环境下安装WPS办公软件
  8. Vue安装步骤及教程(详细)
  9. 大数据告诉你,上海二手房到底难不难卖?
  10. 分类算法 -- 决策树ID3算法
  11. 代码随想录第十四天 二叉树基础 LeetCode 144、145、94
  12. Effective C++读书笔记 摘自 pandawuwyj的专栏
  13. 用dango框架搭建博客网站
  14. python最少钞票_钞票最少张数
  15. 【论文阅读】DenseCLIP: Language-Guided Dense Prediction with Context-Aware Prompting
  16. 华为AR路由器镜像端口配置及取消配置
  17. Linux查看cc编译器版本,如何查看linux版本(转)
  18. Hive:命令行界面、数据类型、DDL数据定义(数据库及表操作/分区分桶)、DML数据操作(数据导入导出)
  19. 简述工业机器人示教再现的一般步骤_工业机器人示教与初识编程语言
  20. Python入门操作-时间序列分析的重要性

热门文章

  1. 面试(2)——StringBuffer StringBuilder String /==与equals
  2. html图片倒角,CSS实例:纯CSS打造斜角
  3. java调用ole ie_SWT中通过Automatioin的方式访问IE(升级版)
  4. Spring框架学习笔记08:基于Java配置方式使用Spring MVC
  5. 计算机英语讲课笔记(2020-6-23)
  6. 《天天数学》连载04:一月四日
  7. iphone退款申请教程_【揭秘】朋友圈卖的iOS退款、王者荣耀0元撸点券教程
  8. 2017.9.4 斜堆 失败总结
  9. 2017.5.14-15 CPU监控 思考记录
  10. 【英语学习】【Level 07】U03 Amazing wonders L2 A global city