shell脚本的几个实例

下面是编写的几个shell脚本的实例,已经在linux测试过

#1、模拟linux登录shell
#!/bin/sh
echo  "login:";
echo -n "name:";
read name;
echo -n "password:";
read passwd;
if [ $name = "ren" -a $passwd = "0207" ]
then
    echo "the host and password is right!";
else 
    echo "input is error!";
fi

#2、比较两个数大小
#!/bin/sh
echo "please input 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、寻找/home/目录下是否存在该文件
#!/bin/sh
echo -n "please input filename:";
read filename;
if test -e /home/$filename
then
    echo "the file is exit";
else
    echo "the file isnot exit";
fi


for num in 1 2 3
do
    # echo $num;
done

#5、命令行输入
#!/bin/bash
echo "please enter a user:";
read user;
u_running=$(whoami);
if test $user = $u_running
then echo "the user is running";
else echo "the user is not running";
fi

#6、删除当前目录下大小为0的文件
#知识点:awk是linux下强大的文本分析工具,按行读入,默认空格每行切片#!/bin/bash
for filename in `ls`#这里是~下面的那个符号,不要错了哦
do
    if test -d $filename
    then b=0;
    else
        a=$(ls -l $filename | awk '{print $5}');
        echo $a;
        if test $a -eq 0
        then rm $filename;
        fi
    fi
done

#7、测试IP地址
#!/bin/bash
for i in 61 2 3 4 92 
do
    echo "the number of $i computer is";
    ping -c 1 192.168.142.$i;
done

#8、给函数传递参数
#!/bin/bash
p_num()
{
    num=$1;
    echo $num;
}
for n in $*
do
    p_num $n 4;
done

你可能感兴趣的:(脚本语言汇总)