shell编程进阶

2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
read -p “Enter you choice yes|no:” Choice
Choice1=`echo $Choice | tr ‘[a-z]’ ‘[A-Z]’`
case $Choice1 in
Y|YES)
echo “you select yes.”
;;
N|NO)
echo “you select no.”
;;
*)
echo “you select others.”
;;
esac

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

用while实现
1、编写脚本,求100以内所有正奇数之和
第一种:for
11
#!/bin/bash
declare -i num=0
for i in `seq 1 2 100`
do
num+=i
# num=$[$num+$i]
done
echo “sum is $num”
执行结果:
[root@os01 /]# ./1.sh
sum is 2500
第二种:while
#!/bin/bash
#
declare -i i=1
declare -i sum=0
while [ $i -le 100 ]
do
sum+=i
let i+=2
done
echo “sum is $sum”
#执行结果
[root@os01 /]# ./2.sh
sum is 2500
第三种:for contniue
#!/bin/bash
declare -i i=1
declare -i sum=0
for i in `seq 1 100`
do
if [ $[$i%2] -eq 0 ]
then
continue
fi
sum+=i
done
echo “sum is $sum”
#执行结果
[root@os01 /]# ./3.sh
sum is 2500

第四种:until
#!/bin/bash
#
declare -i i=0
declare -i sum=0
until [ $i -eq 100 ]
do
let i++
if [ $[$i%2] -eq 0 ]
then
continue
fi
sum+=i
done
echo “sum is $sum”
[root@os01 /]# ./4.sh
sum is 2500
2、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
#!/bin/bash
read -p “please input the ip(eg:172.18.0.1): ” ip
network=`echo $ip |cut -d “.” -f1-3` #C类地址前三位为网络id
i=1 #ip一般从1开始
up=0
down=0
while [ $i -le 254 ];do #255作为网段内的广播ip,所以取1-254之内的循环
if ping -c1 -w1 $network.$i > /dev/null;then #ping一次一秒
echo $network.$i is up!
let up++ #统计开机主机数
else
echo $network.$i is down!
let down++ #统计关机主机数
fi
let i++
done
3、编写脚本,打印九九乘法表
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
mul=$[$i*$j]
echo -ne “$i*$j=$mul\t”
j=$[$j+1]
done
echo
i=$[$i+1]
done
4、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值
random.sh

cat /dev/null > /root/random.txt
declare -i num=1
while [ $num -le 10 ];do
echo $RANDOM | tee -a /root/random.txt
let num++
done
echo “min: “
sort -n /root/random.txt | head -1
echo “max: “
sort -n /root/random.txt | tail -1

============================================

addr.sh

i=10
a=$RANDOM
max=$a
min=$a
while [ $i -ge 1 ]
do
[ $max -lt $a ] && max=$a
[ $min -gt $a ] && min=$a
echo “$a”
a=$RANDOM
let i–
done
echo “最大值$max”
echo “最小值$min”

============================================

declare -i MAX=0
declare -i MIN=0
for I in {1..10};do
MYRAND=$RANDOM
[ $I -eq 1 ] && MIN=$RANDOM
if [ $I -le 9 ];then
echo -n “$MYRAND,”
else
echo “$MYRAND”
fi
[ $MYRAND -gt $MAX ] && MAX=$MYRAND
[ $MYRAND -lt $MIN ] && MIN=$MYRAND
done
echo $MAX,$MIN
5、编写脚本,实现打印国际象棋棋盘
#!/bin/bash
k=0
while [ $k -lt 4 ];do
l=0
while [ $l -lt 4 ];do
echo -ne “\033[41m \033[0m”
echo -ne “\033[43m \033[0m”
let l++
done
echo
l=0
while [ $l -lt 4 ];do
echo -ne “\033[41m \033[0m”
echo -ne “\033[43m \033[0m”
let l++
done
echo
l=0
while [ $l -lt 4 ];do
echo -ne “\033[43m \033[0m”
echo -ne “\033[41m \033[0m”
let l++
done
echo
l=0
while [ $l -lt 4 ];do
echo -ne “\033[43m \033[0m”
echo -ne “\033[41m \033[0m”
let l++
done
echo
let k++
done

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/87234

(2)
1589344251815893442518
上一篇 2017-09-16 11:56
下一篇 2017-09-16 13:13

相关推荐

  • VMware12 Centos系统安装VMware-tools过程

    1、点击VMware菜单虚拟机选中安装VMware-tools:           注:虚拟机桌面会生成VMwaretools DVD安装光盘 2、查看虚拟机设置ISO映像文件是否是VMwaretoolsDVD带Linux.iso映像的文件: 3、在centos系统中 open term…

    Linux干货 2016-07-26
  • sed行编辑器与vim编辑器

    一、sed行编辑器   1、简介:sed的本身也是一个管道命令,可以分析标准输入,还可以将数据进行替换、删除、新增、选取特定行的功能   2、工作原理:sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”( pattern space),接着用sed命令处理缓冲区中的内容…

    Linux干货 2016-08-11
  • N26-博客作业-week15

    1、总结sed和awk的详细用法 sed: 语法结构 sed [OPTION]…’script’ [input-file]…[action] -r:支持扩展正则表达式 -n:不输出模式空间中的内容至屏幕 -e script1 -e script2 -e script3:指定多脚本运行 -f /path/to/script_file:从指定的文件中读取…

    Linux干货 2017-07-14
  • 系统启动这块的一些实验及基本内容–下

    下面我来讲下grub,grub在编辑的时候可以进入一种模式就是单用户模式,就是当grub.conf文件未写入密码时,普通用户将直接忽略系统密码进入系统,所以这可以称为一个捷径,也可以成为一个漏洞,当然linux的前辈们不可能连这个都想不到,他们也有自己的办法,这就是我们grub的两层加密机制,在选定登陆界面之前可以设置一次,启动内核时也可以设置一次,密码也可…

    Linux干货 2016-09-13
  • 8月2日作业

    在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost ~]# groupadd g1 [root@localhost ~]# groupadd g2 [root@local…

    Linux干货 2016-08-05
  • inode、软硬链接区分、ln命令

    inode、软硬链接区分、ln命令 一、inode inode记录的是文件的属性及文件实际放在那块数据块中的。inode包含以下数据: 1. 该文件的可被访问的权限(read/write/excute) 2. 该文件的属主、属组(owner、group) 3. 该文件的大小 4. 该文件创建或者状态改变的时间(Ctim…

    Linux干货 2016-08-02