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

【shell脚本学习-3】的更多相关文章

  1. 笔记——shell脚本学习指南

    <shell脚本学习指南>机械工业出版 ISBN 987-7-111-25504-8 第2章 2.4 初级陷阱 1.当今的系统,对#!这一行的长度限制从63到1024个字符都有,尽量不要超 ...

  2. Shell 脚本学习资料搜集

    Shell文档 ChinaUnix上大神“網中人”总结的Shell十三问,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点.每一问都写得非常精彩.ChinaUnix ...

  3. 学习笔记之Shell脚本学习指南 & sed与awk & 正则表达式

    正则表达式_百度百科 http://baike.baidu.com/link?url=ybgDrN2WQQKN64_gu-diCqdeDqL8LQ-jiQ-ftzzPaNUa9CmgBRDNnyx50 ...

  4. 转 shell脚本学习指南

    shell脚本学习指南 以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bashshell 脚本的第一行,#!之后应该是什么?如果拿这个问题 ...

  5. Shell脚本学习 - 运算符

    继续shell脚本学习.上一篇是基本数据类型和语法的总结,这一篇是运算相关的操作. 运算符 bash不支持简单的数学计算,需要依赖其他命令实现. expr可以代为实现. # 表达式一般这么写 ` + ...

  6. shell脚本学习总结02--数组

    bash同时支持普通数组个关联数组,普通数组只能使用整数作为数组的索引,关联数组可以使用字符串作为数组的索引. 数组的定义方法: 在单行中使用一列值定义一个数组 [root@new ~]# array ...

  7. Shell脚本学习指南笔记

    Shell脚本学习指南 作者:Danbo 2015-8-3 脚本编程语言与编译型语言的差异 许多中型.大型的程序都是用编译型语言写的,例如:C.C+.Java等.这类程序只要从源代码(Source C ...

  8. shell脚本学习之6小时搞定(1)

    shell脚本学习之6小时搞定(1) 简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把 ...

  9. shell脚本学习心得

    近来主要捣鼓ubuntu,大多数项目中都用到了sh脚本作为启动脚本等,以前只是大概明白如何使用,今天需要自己修改并运行脚本就碰到了很多问题,所以决定静下心来学习一下shell脚本,学习了几个小时,现将 ...

  10. shell脚本学习(一)

    Shell脚本最常用于系统管理工作,或者用于结合现有的程序以完成小型.特定的工作. Shell的特点有: 1. 简单性 2. 可移植性 3. 开发容易 [什么是shell] 简单点理解,就是系统跟计算 ...

随机推荐

  1. .NET开源工作流RoadFlow-表单设计-标签(label)

    LABEL标签即在表单中添加一个文本标签: 字号:标签显示文字的大小. 颜色:标签显示文字的颜色. 样式:以粗体和斜体显示.

  2. ASP.NET 中对大文件上传的简单处理

    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...

  3. 大数据的正确用法你get到了吗?

    Azure 镜像市场已于2016年9月21日正式上线,在这个统一的集成平台中,客户可以轻松地浏览.搜索和选择一系列来自第三方的应用和解决方案,并可以将其快速一键部署到 Azure 实例当中. 在移动为 ...

  4. P2PSearcher云点播设置和使用技巧

    P2PSearcher是一款基于ED2K网络的资源搜索工具,资源丰富,小巧轻便.资源搜索结果最丰富.智能排序定位精准,立即找到想要的资源.新版可突破服务器封杀限制,一个能用的P2PSearcher.整 ...

  5. wamp的www目录更改为指定目录

    wamp的www目录更改为指定目录内容简介:主要过程: (1)修改httpd.conf文件 (2)修改新的www目录下的index.php文件 (3)修改manage.ini的278行和manage. ...

  6. startup ORA-00845: MEMORY_TARGET not supported on this system

    一台虚拟机跑多个实例时,由于/dev/shm空间不够导致如下报错> startupORA-00845: MEMORY_TARGET not supported on this system解决方 ...

  7. Perl Unicode全攻略

    Perl Unicode全攻略 耐心看完本文,相信你今后在unicode处理上不会再有什么问题. 本文内容适用于perl 5.8及其以上版本. perl internal form 在Perl看来, ...

  8. VS LNK2019 解决办法之一

    LNK2019: unresolved external symbol _main referenced in function __main 有人说这是因为静态动态引用引起的,但是!这些都没有解决我 ...

  9. 如何用python语言撸出图表系统

    公司指标图表化显示,解决目前跟踪技术指标数据的各种不方便:于是话不多说,撸起袖子就是干: 1.挖掘需求和罗列功能点: a.图表显示技术指标数据. b.根据服务名和系统名查询对应的图表. c.根据日期区 ...

  10. c++11之100行实现简单线程池

    代码从github上拷的,写了一些理解,如有错误请指正 Threadpool.h #ifndef THREAD_POOL_H #define THREAD_POOL_H #include <ve ...