linux~脚本合集

1:检查当前用户是否是超级用户,如果是超级用户,则安装dhcp服务;
如果不是超级用户,则输出“当前用户权限不足”

	[ `id -u`="0" ] && {
           yum install dhcp.x86_64 -y
              systemctl start dhcpd
              systemctl status dhcpd
      }||{
              echo "当前用户不是超级用户"
      }

在这里插入图片描述

2:脚本编程要求如下:
a) :运行 scr ipt1.sh dog 输出结果为 cat
b) :运行 scr ipt1.sh cat 输出结果为 dog 除此以外都输出结果为 error

 [ "$1" = "dog" ] && {
        echo cat
  }|| {
  [ "$1" = "cat" ] && {
          echo dog
         }||{
         echo error
    }
  }

在这里插入图片描述

3:建立用户脚本要求如下:
a) :如果系统存在用户 luck,并且使用/bin/bash,则输出 user ok
b) :如果系统村存在用户 luck,则输出 user exist
c):如果系统不存在用户 luck,则运行 user add luck 命令来创建
d) :当在系统上运行命令 usercmd 时,将输出指令“echo hello;ls-l” 的结果,并
在重启后也生效。

 useradd luck &> /dev/null || {
         echo "user exist"
          [ `grep "^luck" /etc/passwd | cut -d ":" -f 7`="/bin/bash" ] &&{
                 echo user is ok 
          }
 }

  [ "`grep "usercmd" /etc/bashrc`" = "alias usercmd='echo hello;ls -l'" ] ||{
 
 echo "alias usercmd='echo hello;ls -l'" >> /etc/bashrc
 
 source /etc/bashrc
 }

在这里插入图片描述

4:输出时间倒计时:
A:固定时间倒计时:

for  time in {4..1}
do
        echo -n "TIME $time 秒 "
        echo -ne "\r"
        sleep 1
done

在这里插入图片描述
B:用户决定时间倒计时(shell中的运算):

read -p "请输入倒计时分秒数:"  m s
for ((A=60*m+s;A>0;A--))
do
        M=$[A/60]
        S=$[A%60]
        echo -n "TIME $M 分 $S 秒 "
        echo -ne "\r"
        sleep 1
done

在这里插入图片描述

5:检测可以pin通的主机,如果可以ping通则将ip地址存放在file文件中(if和case的基本用法):

if [ -e "/mnt/file" ]
then
        echo  "/mnt/file was exist!!!"
        echo "[Overwrite] [Backup] [skip]"
        read -p "what are you want to do:" action
case $action in
                s|S)
                ;;
                b|B)
                mv /mnt/file /mnt/file_back
                ;;
                o|O)
                rm -rf  /mnt/file
esac
fi
for ip in {1..10}
do
        ping -c1 -w1 172.25.254.$ip &>/dev/null &&{
        echo 172.25.254.$ip >> /mnt/file
}

在这里插入图片描述

6:判断文件类型:
需要注意的是,在判定一个文件是普通文件还是软链接时,因为if语句是顺序执行的,所有需要将判定软链接写在判定普通文件之前:
read -p " Plesae input the filename of what you want to detection: " filename

if      [ ! -e "$filename" ]
then
        echo "The file is not's exist!!! Please detection your input:"
elif    [ -L "$filename" ]
then
        echo "$filename is a software~"
elif    [ -d "$filename" ]
then
        echo "$filename is a common directory~"
elif    [ -f "$filename"  ]
then
        echo "$filename is a common file~"
fi

在这里插入图片描述

7:使用脚本创建/删除/备份文件
需要注意的是,在创建/删除/备份文件之前,首先要进行判断,-a/-d/-p参数以及filename参数是否输入;其次判断filename文件是否存在;

Check_File()     
{
        if [ -e "$1" ]
        then
                $2
                $3
        fi
}
if [ "$#" -lt "2" ]
then
        echo "Error:Please input option [-a|-d|-p] and following filename $0"
        exit 1
fi
if [ "$1" = "-a" ]
then
        Check_File $2 "echo $2 is exit!!" exit
        touch $2
elif [ "$1" = "-d" ]
then
        Check_File $2 "rm -rf $2" exit
        echo "$2 is not exist"
elif [ "$1" = "-p" ]
then
        Check_File $2 "cp -rp $2 /mnt" exit
        echo "$2 is not exist"
else
        echo "Please input -a|-d|-p following $0!!"
fi

在这里插入图片描述

8:交互式使用脚本远程获取信息文件(expect基本用法):
如:在可以连接的情况下,获取1~34号主机的/etc/passwd/文件的后三行文件信息(当然不止这么简单,类比的可以使用脚本批量远程在对方主机添加用户/安装程序等)
Connect(){
/usr/bin/expect <<EOF
spawn ssh root@$1 KaTeX parse error: Can't use function '\r' in math mode at position 47: … {send "yes\̲r̲";exp_continue …ip &>/dev/null &&{
echo “ i p + y e s &quot; e c h o &quot; ‘ C o n n e c t 172.25.254. ip + yes&quot; echo &quot;`Connect 172.25.254. ip+yes"echo"Connect172.25.254.ip tail -n 3 /etc/passwd >>/mnt/passwd`”
}||{
echo no
}
done
在这里插入图片描述

9:用户创建脚本:

if      [ $# -lt 2 ]
    then
        echo "Please input userlist and passlist after scrept!!!"
elif    [ ! -e "$1" -o ! -e "$2" ]
    then
        echo "userlist or passlist is not exist ,please inspect!!"
fi
userline=`awk 'EGIN{n=0}{n++}END{print n}' $1`
#echo $userline
passline=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
if [ $userline != $passline ]
     then
        echo "Please keep two files same lines to continue!!$0"
fi
for num in `seq 1 $userline`
do
#echo $num
        username=`sed -n ${num}p $1`
        password=`sed -n ${num}p $2`
#echo $username
#echo $password
        useradd $username &>/dev/null &&{
        echo $password | passwd --stdin $username &>/dev/null
        }||{
        echo  "$username is exist!!!"
}
done

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
10:用户删除:

if [ "$#" -lt 1 ]	
then
	echo "please input userlist and passlist after $0"
	exit 1
elif [ ! -e "$1" ]
then
	echo "用户文件为空,请检查文件!!"
fi
userline=`awk 'BEGIN{n=0}{n++}END{print n}' $1`

for NUM in `seq 1 $userline`
#for NUM in 1  $userline
do
	username=`sed -n ${NUM}p $1`
	id $username &> /dev/null &&{
	userdel -r $username &>/dev/null
}
done

在这里插入图片描述

11:简单计算器:

read -p "Please input first num~:" anum
read -p "Please input what you want to do~:"  choice
read -p "Please input second num~:" bnum
[ -z "$anum" -o -z "$choice" -o -z "$bnum" ] &&{
        echo "Please input right message"
        exit 1
}
bc <<EOF

$anum $choice $bnum
EOF

在这里插入图片描述

在这里插入图片描述

12:9*9乘法表:

for  ((i=1;i<=9;i++))
do
#       echo $i
        for ((c=1;c<=i;c++))
        do
        num=$(($i*$c))
        echo -ne "$i*$c=$num \t"
        done
        echo -e "\n"
done

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值