#!/bin/bash
#Author:Luo
#date:2017-07-01
#version:2.0
#This script is used for adding a new user!
if [ "$#" -ne 1 ] ;then
echo "请输入一个用户名";exit
elif id $1 &> /dev/null;then
echo "该用户已存在"
else
useradd $1;echo "创建用户成功,用户信息如下:"
id $1
fi
#!/bin/bash
#Author:Luo
#date:2017-07-01
#version:2.0
#编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
read -p "请输入yes or no:" ans
if [[ "$ans" =~ ^[yY]$|^[yY][Ee]$|^[yY][Ee][Ss]$ ]]
then
echo "输入结果为yes"
elif [[ "$ans" =~ ^[nN]$|^[nN][Oo]$ ]]
then
echo "输入结果为no"
else
echo "只能输入yes or no"
fi
#!/bin/bash
#Author:Luo
#date:2017-07-01
#version:2.0
#编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,块设备,其它文件类型)
if [[ ! -a $1 ]];
then echo "文件不存在"
elif [ -h $1 ];
then echo "符号链接文件"
elif [ -b $1 ];
then echo "块设备文件"
elif [ -d $1 ];
then echo "目录文件"
elif [ -f $1 ];
then echo "普通文件"
else
echo "其他文件"
fi
#!/bin/bash
#Author:Luo
#date:2017-07-01
#version:2.0
#判断用户输入的参数是否为正整数
if [[ $# -ne 1 ]]
then
echo "请输入一个参数"
elif [[ $1 =~ ^[0]$ ]]
then
echo "不是正整数"
elif [[ $1 =~ ^[[:digit:]]+$ ]]
then
echo "是正整数"
else
echo "不是正整数"
fi
#!/bin/bash
#Author:Luo
#date:2017-07-01
#version:2.0
#This script is used for 输入一个目录名,判断该目录下所有文件的类型
if [ $# -ne 1 ]
then
echo "参数错误,请输入一个目录"
elif [[ ! -a $1 ]]
then
echo "目录不存在"
elif [[ ! -d $1 ]]
then
echo "输入的参数不是目录"
else
for filename in `ls $1`;do
file $1/$filename
unset filename
done
fi
#Author:Luo
#date:2017-07-01
#version:2.0
#添加10个用户user1-user10,密码为8位随机字符
for num in `seq 10`;do
useradd user$num; cat /dev/urandom |tr -dc a-zA-Z0-9|head -c 8|passwd user$num --stdin
done
#Author:Luo
#date:2017-07-01
#version:2.0
#/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为>文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
for filename in `ls /etc/rc.d/rc3.d`;do
if [[ "$filename" =~ ^K.*$ ]]
then echo "$filename stop"
elif [[ "$filename" =~ ^S.*$ ]]
then echo "$filename start"
else echo "$filename"
fi
done
unset filename
#Author:Luo
#date:2017-07-01
#version:2.0
#提示输入正整数n的值,计算1+2+…+n的总和
sum=0
if [[ $# -ne 1 ]]
then
echo "参数输入错误,请输入一个正整数"
elif [[ $1 =~ ^[0]$ ]]
then
echo "参数输入错误,不是正整数"
elif [[ $1 =~ ^[[:digit:]]+$ ]]
then
for num in `seq $1`;do
let sum+=$num
done
echo "sum=$sum"
else
echo "参数输入错误,不是正整数"
fi
#Author:Luo
#date:2017-07-01
#version:2.0
#计算100以内所有能被3整除的整数之和
sum=0
for num in `seq 100`;do
module=$[num%3]
if [[ $module -eq 0 ]];then
let sum+=num
fi
done
echo "sum=$sum"
unset sum
unset module
unset num
#Author:Luo
#date:2017-07-01
#version:2.0
#打印九九乘法表
for num1 in `seq 9`;do
for num2 in `seq $num1`;do
let product=$num2*$num1
echo -n "$num2*$num1=$product "
done
echo
done
#Author:Luo
#date:2017-07-01
#version:2.0
#打印等腰三角形
for lnum in `seq $1`;do
let space=$1-lnum
let star=2*lnum-1
for spacetimes in `seq "$space"`;do
echo -n " "
done
for startimes in `seq "$star"`;do
echo -n "*"
done
echo
done
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#猜字游戏
#date:2017-07-02
#version:2.0
let num=$RANDOM%10
guess=11
until [ "$guess" -eq "$num" ];do
read -p "猜一猜我的数字是多少?(输入1-10或者exit退出):" guess
[ $guess = exit ]&&exit
[[ ! $guess =~ ^[[:digit:]]+$ ]]&&echo "输入错误,请重新开始游戏"&&exit
if [ "$guess" -lt "$num" ];then
echo "猜小啦!"
elif [ "$guess" -gt "$num" ];then
echo "猜大啦!"
else
echo "猜对啦!"
fi
done
unset guess
unset num
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-02
#version:2.0
#用文件名做为参数,统计所有参数文件的总行数
sum=0
until [ -z "$1" ];do
line=`cat $1 |wc -l`
let sum+=$line
shift
done
echo sum=$sum
unset sum
unset line
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-02
#version:2.0
#用二个以上的数字为参数,显示其中的最大值和最小值
[ $# -lt 2 ]&&echo "usage:请输入两个以上数字参数"&&exit
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-02
#version:2.0
#用二个以上的数字为参数,显示其中的最大值和最小值
[ $# -lt 2 ]&&echo "usage:请输入两个以上数字参数"&&exit
if [[ ! $1 =~ ^-?[[:digit:]]+$ ]];then
echo "参数类型错误,只能输入数字";exit
elif [[ ! "$2" =~ ^-?[[:digit:]]+$ ]];then
echo "参数类型错误,只能输入数字";exit
elif [ $1 -gt $2 ];then
max=$1
min=$2
else
max=$2
min=$1
fi
until [ -z "$1" ];do
[[ ! "$1" =~ ^-?[[:digit:]]+$ ]]&&echo "参数类型错误,只能输入数字"&&exit
if [ $1 -gt $max ];then
max=$1
fi
if [ $1 -le $min ];then
min=$1
fi
shift
done
echo max=$max
echo min=$min
unset max
unset min
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-02
#version:2.0
#扫描/etc/passwd文件每一行,如发现GECOS字段为空,则填充用户名和单位电话为62985600,并提示>该用户的GECOS信息修改成功。
while read line;do
user=`echo $line|cut -d: -f1`
comment=`echo $line|cut -d: -f5`
[ -z "$comment" ] && usermod -c ""$user"6285600" $user &&echo "用户$user 注释信息>添加成功"
done</etc/passd
unset user
unset conmment
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-06
#version:2.0
#编写函数实现数字的加减乘除运算,例如输入1 + 2,,将得出正确结果
jisuan () {
[ $# -lt 3 ]&&echo "请输入正确的参数,like 1 + 2 / 3 + 3 "&&return
result=$1
until [ -z $2 ] ;do
let result=$result$2$3
shift
shift
done
echo $result
}
#!/bin/bash
#Author:Luo
#email:luo-maojian@foxmail.com
#date:2017-07-06
#斐波那契数列又称黄金分割数列,因数学家列昂纳多·斐波那契以兔子繁殖为例子而引入,
#故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……,
#斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2)
#利用函数,求n阶斐波那契数列
#version:2.0
F (){
if [ $1 -eq 0 ];then
f=0
echo $1
elif [ $1 -eq 1 ];then
f=1
echo $f
else
m=`F $[$1-1]`
n=`F $[$1-2]`
let f=m+n
echo $f
fi
}
Flist () {
for ((i=0;i < $1;i++));do
echo -n "`F $i` "
done
echo
}