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" 
fi

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

shell脚本简单实例的更多相关文章

  1. shell脚本简单例子

    eg: Expect: 1.用环境变量RANDOM随机生成一个100以内的随机数 2.read读取当前输入 3.当前输入对比随机生成的数 4.当两个数相等时跳出苏循环,并计数(比较n次结果才相等) # ...

  2. Linux Shell脚本简单语法汇总(Deepin下运行)

    整理自: https://www.runoob.com/?s=shell Shell 脚本(shell script),是一种为 shell 编写的脚本程序. 业界所说的 shell 通常都是指 sh ...

  3. shell脚本小实例

    本文收集了一堆的shell脚本技巧,我说过,我写博客主要是作一些学习笔记,方便自己查阅,所以,我会搞出这么一篇文章,也没有什么不可理解的.关于这些技巧的出处,诶,我也忘了,可能来自theunixsch ...

  4. shell脚本编写实例

    实际案例 1.判断接收参数个数大于1 [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.. ...

  5. SHELL脚本简单的赋值与递增

    Count=`expr $Count + 1`;#可以在各种shell执行,其他类C的写法只能在指定的bash版本执行; 赋值不能带$, 带$相当于字符串常量了;执行脚本参考如下 #!/bin/sh ...

  6. linux100day(day6)--shell脚本简单逻辑

    if语句: if条件语句的使用格式: 1.单分支语句 if 条件;then 执行语句 fi 2.双分支语句 if 条件;then 执行语句1 else 执行语句2 fi 3.多分支语句 if 条件;t ...

  7. shell脚本简单切割字符串

    我们有这样一个字符串: info='abcd;efgh' 现在想获取abcd和efgh,我们可以简单地用cut工具来获取: fstr=`` sstr=`` 这里主要是用了cut工具的-d和-f参数: ...

  8. shell脚本简单密码加密

    #!/bin/sh #输入密码 echo "请输入原密码:" read resultFirst firstPWD=$resultFirst echo "请再次输入原密码: ...

  9. 什么是Shell?Shell脚本基础知识详细介绍

    这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...

随机推荐

  1. 9.11 Django视图 view和路由

    2018-9-11 16:34:16 2018-9-11 19:00:24 越努力,.越幸运! Django框架参考: https://www.cnblogs.com/liwenzhou/p/8296 ...

  2. logstash实战input插件syslog

    vim /etc/logstash/conf.d/syslog.conf input{ syslog{ type => "system-syslog" port => ...

  3. org.springframework.dao.InvalidDataAccessApiUsageException

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read ...

  4. OpenCV学习笔记之CXCORE篇

    转自blog.csdn.net/bbzz2/article/details/50764209

  5. pycurl实例详解

    Pycurl是Python的libcurl接口.liburl是客户端的URL传输库,它支持FTP,FTPS,HTTP,HTTPS,TELNET,LDAP等诸多协议,同时支持HTTP认证,代理,FTP上 ...

  6. Ubuntu:如何显示系统托盘图标(systray)

    1. 问题说明 Ubuntu 11版本开始,默认关闭了托盘图标的显示,需要手动执行命令或额外工具配置,添加到白名单.Ubuntu 13.04更彻底,默认配置根本没有托盘图标,除了java和wine等几 ...

  7. Gym - 101628F Find the Inn dijkstra,读边时计算新权值

    题意: 给n个点m条边及每条边所花费的时间,经过给定的p个点时会停留k秒,要求在t秒内从1号点走到n号点,若可以走到输出最短时间,若不行输出-1.. 题解:读取边时,将每个点停留的时间加到以其为终点的 ...

  8. 通过qemu复现路由器漏洞

    目录 简介 环境搭建 固件解包 复现漏洞,IDA调试 参考资料 简介 qemu和vmware一样,一种虚拟机软件,只不过qemu能够虚拟的平台更加丰富一些.能够虚拟很多嵌入式平台的设备. 工作上需要向 ...

  9. 优云软件助阵GOPS·2017全球运维大会北京站

    GOPS· 2017全球运维大会北京站于2017年7月28日-29日在北京隆重举办,汇聚国内一线运维专家和诸多运维同仁达800余名.作为长期致力于企业级高端运维市场软件开发和咨询服务的优云软件受邀参与 ...

  10. jquery closest & parent比较

    .closest() .parents() 从当前元素开始 从父元素开始 沿 DOM 树向上遍历,直到找到已应用选择器的一个匹配为止. 沿 DOM 树向上遍历,直到文档的根元素为止,将每个祖先元素添加 ...