1.模拟linnux登录shell

#/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "cht" -a $passwd = "abc" ];then
echo "the host and password is right!"
else echo "input is error!"
fi

2.比较两个数大小

#/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 < NO.2"

3.查找/root/目录下是否存在该文件

#/bin/bash
echo "enter a file name:"
read a
if test  -e /root/$a
then echo "the file is exist!"
else echo "the file is not exist!"
fi

4.for循环的使用

#/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
    echo "$num"
done

5.命令行输入

#/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi

6.删除当前目录下大小为0的文件

#/bin/bash
for filename in `ls`
do
    if test -d $filename
    then b=0

else   
       a=$(ls -l $filename | awk '{ print $5 }')
            if test $a -eq 0
             then rm $filename
             fi
        fi     
done

7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G

#/bin/bash
while line=`ls /export/um_lpp_source`
do
        if test $line=""
        then  echo "NULL"
             sleep 1
    else echo $line
                chfs -a size=3G /export/um_lpp_source
                 exit 0
        fi
done

8.测试IP地址

#/bin/bash
for i in  1 2 3 4 5 6 7 8 9
do
    echo "the number of $i computer is "
    ping -c 1 192.168.0.$i
done

9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件

#/bin/sh
a=2
while name="test.log"
do
        sleep 1
        b=$(ls -l $name | awk '{print $5}')
        if test $b -ge $a
        #then echo "OK"
    then `cp /opt/*.tar.gz .`
        exit 0
        fi
done

10.打印读取的内容,为下面的例子做准备

#/bin/bash
while read name
do
echo $name
done

11.从0.sh中读取内容并打印

#/bin/bash
while read line
do
    echo $line
done < 0.sh

12.读取a.c中的内容并做加1运算

#/bin/bash
test -e a.c
while read line
do
    a=$(($line+1))
done < a.c
echo $a

13.普通无参数函数

#/bin/bash
p ()
{
    echo "hello"
}
p

14.给函数传递参数

#/bin/bash

p_num ()
{
    num=$1
    echo $num
}
for n in $@
do
    p_num $n
done

15.创建文件夹

#/bin/bash
while :
do
    echo "please input file's name:"
    read a
    if test -e /root/$a
    then
         echo "the file is existing Please input new file name:"
    else
        mkdir $a
        echo "you aye sussesful!"
        break
    fi
done

16.获取本机IP地址

#/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

17.查找最大文件

#/bin/bash
a=0
for  name in *.*
do
     b=$(ls -l $name | awk '{print $5}')
    if test $b -ge $a
    then a=$b

namemax=$name
     fi
done
echo "the max file is $namemax"

18.查找当前网段内IP用户,重定向到ip.txt文件中

#/bin/bash
a=1
while :
do
    a=$(($a+1))
    if test $a -gt 255
    then break
    else
        echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        echo $ip >> ip.txt
    fi
done

19.打印当前用户

#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')

20.case语句练习

#!/bin/bash
clear
echo "enter a number from 1 to 5:"

read num
case $num in
    1) echo "you enter 1"
    ;;
    2) echo "you enter 2"
    ;;
    3) echo "you enter 3"
    ;;
    4) echo "you enter 4"
    ;;
    5) echo "you enter 5"
    ;;
    *) echo "error"
    ;;
esac

21.yes/no返回不同的结构

#!/bin/bash
clear
echo "enter [y/n]:"
read a
case $a in
    y|Y|Yes|YES) echo "you enter $a"
    ;;
    n|N|NO|no) echo "you enter $a"
    ;;
    *) echo "error"
    ;;
esac

22.杀进程

#/bin/bash
pid=`ps -ef | grep '进程相关内容' | grep -v 'grep' | awk '{ print $2}'`
if [ -n "$pid" ]; then
        kill -9 $pid
fi

23.内置命令的使用

#/bin/bash

clear
        echo "Hello, $USER"
        echo
       
        echo "Today 's date id `date`"

echo

echo "the user is :"
        who
        echo

echo "this is `uname -s`"
        echo

echo "that's all folks! "

24.

25.

#/bin/bash

26.打印无密码用户

#/bin/bash
echo "No Password User are :"
echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

27.

#/bin/bash

clear
        echo "Hello, $USER"
        echo
       
        echo "Today 's date id `date`"

echo

echo "the user is :"
        who
        echo

echo "this is `uname -s`"
        echo

echo "that's all folks! "
28.检查端口号是否已启动
#!/bin/bash
n=1
echo "检查xxx服务..."
while true
do
        if test $n -gt 20
        then
                echo "xxx服务启动失败"
                break
        fi
               
        sleep 5
        n=$(($n+1))
        port=`netstat -antp | grep "0.0.0.0:8080"`
        if [ ${#port} -gt 3 ]; then
                echo "xxx服务已经启动"
                break;
        fi
done

sehll 小脚本的编写{基础}的更多相关文章

  1. sehll 小脚本的应用

    1.模拟linnux登录shell #/bin/bash echo -n "login:" read name echo -n "password:" read ...

  2. Linux 脚本编写基础

    txt去重    http://man.linuxde.net/sort Linux 脚本编写基础 http://www.cnblogs.com/linn/archive/2007/03/05/664 ...

  3. linux 脚本编写基础(一)

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

  4. 自己动手编写IPv4地址包含关系测试的小脚本

    工作中需要对地址包含关系进行测试,现有ipaddress标准库和IPy无法满足,于是自己动手编写小脚本,主要实现== , in, <等专用功能,其他功能可以后续用到再补充,例如迭代打印网段内所有 ...

  5. shell 脚本编写基础

    在进行Linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要什么后缀名,例如可以写abc,smartzip这类名称,运行时只要键入 ./smartzip就能运行脚本了.. 每行 ...

  6. bash脚本编写基础

    bash脚本编程     命令的堆砌     脚本程序:解释器解析执行     shell:交互式接口,编程环境         shell:能够提供一些内部命令,并且能通过PATH环境变量找到外部命 ...

  7. 学习 shell脚本之前的基础知识

    转载自:http://www.92csz.com/study/linux/12.htm  学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...

  8. MySQL中Procedure事务编写基础笔记

    原文:MySQL中Procedure事务编写基础笔记 目录: 一.PROCEDURE: 二.CREATE PROCEDURE基本语法: 三.PROCEDURE小进阶   3.1.基本的DECLARE语 ...

  9. 学习shell脚本之前的基础知识

    日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...

随机推荐

  1. Java正则表达式过滤并消除非法字符

    package sd; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author 大汉 * */ ...

  2. spring boot 加载application配置文件

    这就要注意了

  3. 在区块链侧链上进行Dapp技术开发

    我在白皮书里提到过,asch使用的是不同于以太坊和比特币的侧链架构,dapp是运行在侧链上的,每套侧链对应一个dapp. 侧链的独立性 侧链架构的好处是代码和数据独立,不增加主链的负担,避免数据过度膨 ...

  4. NoSQL数据库常见分类

    1.列式数据库HBaseBigTable2.K-V数据库RedisCassandraLevelDBMemCacheEhcache3.文档数据库MongoDBCouchDB4.全文搜索引擎Elastic ...

  5. C#的托管与非托管大难点

    托管代码与非托管代码 众所周知,我们正常编程所用的高级语言,是无法被计算机识别的.需要先将高级语言翻译为机器语言,才能被机器理解和运行.在标准C/C++中,编译过程是这样的:源代码首先经过预处理器,对 ...

  6. 在队列中join()与task_done()的关联性

    1.基础解释: Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号 Queue.join() 实际上意味着等到队列为空,再执 ...

  7. python基础4 input()函数

    input()函数 赋值输出: name=input('请求输入你喜欢的电影名:')print(name+'是我最喜欢的电影!') 输入:大话西游 输出:大话西游是我最喜欢的电影! print('那么 ...

  8. CF1142C U2

    题目链接:洛谷 codeforces $y>x^2+bx+c$也就是$y-x^2>bx+c$ 左边是点,右边是直线. 维护上凸包. 虽然这么简单但就是做不出来. #include<c ...

  9. phpstudy2018 安装xdebug扩展

    第一步:查看PHP版本信息 第二步:到xdebug下载页去下载最新的版本(注意:要下载PHP对应版本) 第三步:把扩展php_xdebug-2.7.0alpha1-7.2-vc15-nts.dll放到 ...

  10. Postman代码测试工具如何用?

    1.  1)get请求,参数为map时, postman的传参 2)参数为基本数据类型的参数时 postman传参: 3)当参数在接口中动态获取时 postman传参: 2.  1)post请求,参数 ...