http://sh.k7p.work /index.php,shell编程进阶

3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

read -p “Please input the filepath: ” f

if [ ! -e $f ];then

echo “the file is not exited,please input the right filepath” && exit

elif [ -f $f ];then

echo “the file is regular file”

elif [ -d $f ];then

echo “the file is directory file”

elif [ -l $f ];then

echo “the file is link file”

else

echo “the file is other type”

fi

4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数

read -p “please input : ” number

if [[ “$number” =~ ^[0-9]+$ ]];then

echo zhengshu

else

echo “not zhengshu”

fi

~

for

1、判断/var/目录下所有文件的类型

for type in /var/* ;do

if [ -h $type -o -L $type ];then

echo “the $type is a link file”

elif [ -f $type ];then

echo “the $type is a reguler file”

elif [ -d $type ];then

echo “the $type is a dir file”

elif [ -b $type ];then

echo “the $type is a block file”

elif [ -c $type ];then

echo “the $type is a character file”

elif [ -p $type ];then

echo “the $type is a pipe file”

else

echo “the $type is other file”

fi

done

wait

2、添加10个用户user1-user10,密码为8位随机字符

for uid in `seq 1 6`;do

grep “^user$uid\>” /etc/passwd &>/dev/null

if [ $? -eq 0 ];then

echo the user$uid is exited

else

useradd user”$uid”

passwd=`tr -dc ‘a-zA-Z0-9’ > /app/user.log

echo “$passwd” | passwd –stdin user”$uid” &> /dev/null && echo user”$uid” is created Suc

cessfully!!!

fi

done

3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start

k=`ls /etc/rc.d/rc3.d | egrep -i “^k.*”`

s=`ls /etc/rc.d/rc3.d | egrep -i “^s.*”`

for fk in $k;do

echo -e “$fk\tstop”

done

for fs in $s;do

echo -e “$fs\tstart”

done

4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和

read -p “please input number: ” n

if [[ “$n” =~ ^[0-9]+$ ]];then

sum=0

for n in `seq $n`;do

let sum=sum+$n

done

echo “the sumnumber is $sum”

else

echo “please input right number!”

fi

5、计算100以内所有能被3整除的整数之和

sum=0

m=3

for n in `seq 100`;do

let a=n%m

if [ $a -eq 0 ];then

let sum=sum+n

fi

done

echo $sum

6、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

#!/bin/bash

read -p “Please input IP: ” ip

if [[ “$ip” =~ ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3} ]];then

a=`echo $ip | cut -d. -f1-3`

for b in {0..254};do

{

if ping -c1 -W1 $a.$b &> /dev/null;then

echo $a.$b is up!

fi

}&

done

else

echo please input the right IP!

fi

wait

7、打印九九乘法表

for a in `seq 9`;do

for b in `seq $a`;do

echo -ne “$b*$a=$[$b*$a]\t”

done

echo

done

8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

#!/bin/bash

if [ ! -d /testdir ];then

mkdir /testdir/ &> /dev/null

fi

for n in {1..10};do

for a in `cat /dev/urandom |tr -dc “a-zA-Z”|head -c 8`;do

touch /testdir/$n$a.html

done

echo $n$a.html is already created!

done

9、打印等腰三角形

#!/bin/bash

read -p “请输出层数:” L

if [[ “$L” =~ ^[0-9]+$ ]];then

for k in `seq $L`;do

for a in `seq $[$L-$k]`;do

echo -n ” “

done

for b in `seq $[$k*2-1]`;do

echo -en “\033[3$Yan;5m?\033[0m”

done

echo

done

else

echo Please input the number!

fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值