Linux运维学习作业2-1-bash脚本编写

a)、自动删除test.txt文件脚本

命名脚本名称为auto_cp.sh,脚本的功能实现从/root/目录cp拷贝test.txt到/tmp目录,并且在/tmp目录创建一个目录abc,并且删除原/root/下test.txt。

程序:

#!/bin/bash
#拷贝/root/目录下test.txt文件,并删除
#管理员邮箱:xxxx@xx.com
#2019年3月18日

if [ ! -e /tmp/abc ];then
        mkdir -p /tmp/abc
        echo "创建/tmp/abc目录成功..."
else
        echo "/tmp/abc目录已经存在..."
fi

if [ -e /root/test.txt ];then
        cp -a /root/test.txt /tmp/abc/
        echo "将/root/test.txt复制到/tem/abc目录下..."
        rm -rf /root/test.txt
        echo "已删除/root/test.txt..."
else
        echo "/root/test.txt文件不存在..."
fi

 

执行结果:

执行前:
[root@localhost ~]# 
[root@localhost ~]# ls -l /tmp/
total 0
drwx------. 3 root root 17 Mar 17 20:36 systemd-private-b01f0f2022bf4d86851c2b2571a75449-vmtoolsd.service-3DOs3Q
[root@localhost ~]# 
[root@localhost ~]# ls -l /root/test.txt 
-rw-r--r--. 1 root root 0 Mar 18 04:31 /root/test.txt
[root@localhost ~]# 

执行时
[root@localhost ~]# ./auto_cp.sh 
创建/tmp/abc目录成功...
将/root/test.txt复制到/tem/abc目录下...
已删除/root/test.txt...
[root@localhost ~]# 

执行后
[root@localhost ~]# ls -l /tmp/abc/
total 0
-rw-r--r--. 1 root root 0 Mar 18 04:31 test.txt
[root@localhost ~]# 
[root@localhost ~]# ls -l /root/test.txt 
ls: cannot access /root/test.txt: No such file or directory
[root@localhost ~]#

b)、批量创建文件(xxxxxxxxxx_thinkmo.html)

使用for循环在/thinkmo目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串thinkmo

 

程序:

#!/bin/bash
#批量创建文件,格式"xxxxxxxxxx_thinkmo.html"
#管理员邮箱:xxx@xx.com
#2019年03月18日

function radchars()
{
        chars=""
        for i in $(seq 1 10)
        do
                char_rdom=$(printf "\\x$(printf "%x\n" $(expr 97 + $(expr $RANDOM % 26)))")
                chars=${chars}${char_rdom}
        done
}

dust="/thinkmo"
if [ ! -e $dust ];then
        echo "${dust}目录不存在,正在创建..."
        mkdir -p $dust
        echo "${dust}目录已创建..."
fi

for file_num in $(seq 1 10)
do
        radchars
        touch ${dust}/${chars}_thinkmo.html
done
执行结果:

执行前:
[root@localhost ~]# 
[root@localhost ~]# ls -l /thinkmo/
ls: cannot access /thinkmo/: No such file or directory
[root@localhost ~]# 

执行时:
[root@localhost ~]# 
[root@localhost ~]# ./Create_file.sh 
/thinkmo目录不存在,正在创建...
/thinkmo目录已创建...
已创建/thinkmo/dhkrmgpuak_thinkmo.html
已创建/thinkmo/uuebjkoomh_thinkmo.html
已创建/thinkmo/sgkqcwfufz_thinkmo.html
已创建/thinkmo/aekohoolub_thinkmo.html
已创建/thinkmo/flsiygfmbj_thinkmo.html
已创建/thinkmo/suriukzhjd_thinkmo.html
已创建/thinkmo/dioqdbkyht_thinkmo.html
已创建/thinkmo/vpmmfqpcnz_thinkmo.html
已创建/thinkmo/yvetkflzux_thinkmo.html
已创建/thinkmo/irzwntvatj_thinkmo.html
[root@localhost ~]# 
[root@localhost ~]# 

执行后:
[root@localhost ~]# ls -l /thinkmo/
total 0
-rw-r--r--. 1 root root 0 Mar 18 04:47 aekohoolub_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 dhkrmgpuak_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 dioqdbkyht_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 flsiygfmbj_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 irzwntvatj_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 sgkqcwfufz_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 suriukzhjd_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 uuebjkoomh_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 vpmmfqpcnz_thinkmo.html
-rw-r--r--. 1 root root 0 Mar 18 04:47 yvetkflzux_thinkmo.html
[root@localhost ~]#

c)、批量创建系统账号

批量创建10个系统帐号thinkmo01-thinkmo10并设置密码(密码为随机数,要求字符和数字等混合)。

账号文件:

[root@localhost ~]# cat /root/user.txt 
thinkmo01
thinkmo02
thinkmo03
thinkmo04
thinkmo05
thinkmo06
thinkmo07
thinkmo08
thinkmo09
thinkmo10
[root@localhost ~]# 
程序:

#!/bin/bash
#批量创建系统账号
#管理员邮箱:xxx@xx.com
#2019年03月18日

for username in $(cat /root/user.txt)
do
        chars=$(cat /dev/urandom | head -n 5 | md5sum | head -c 8)
        useradd $username -s /sbin/nologin -M
        echo "$chars" | passwd $username --stdin
        echo "$username的密码是:$chars" 
done
执行结果:

执行前:
[root@localhost ~]# 
[root@localhost ~]# cat /etc/passwd | tail -n 10
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
ftd001:x:1000:1000::/home/ftd001:/bin/bash
[root@localhost ~]# 

执行时:
[root@localhost ~]# ./Create_users.sh 
Changing password for user thinkmo01.
passwd: all authentication tokens updated successfully.
thinkmo01的密码是:c8ecbc6d
Changing password for user thinkmo02.
passwd: all authentication tokens updated successfully.
thinkmo02的密码是:796556a2
Changing password for user thinkmo03.
passwd: all authentication tokens updated successfully.
thinkmo03的密码是:179654d1
Changing password for user thinkmo04.
passwd: all authentication tokens updated successfully.
thinkmo04的密码是:8b3658f3
Changing password for user thinkmo05.
passwd: all authentication tokens updated successfully.
thinkmo05的密码是:a98ffc2a
Changing password for user thinkmo06.
passwd: all authentication tokens updated successfully.
thinkmo06的密码是:551c85bd
Changing password for user thinkmo07.
passwd: all authentication tokens updated successfully.
thinkmo07的密码是:5aa2b57d
Changing password for user thinkmo08.
passwd: all authentication tokens updated successfully.
thinkmo08的密码是:dc1831a3
Changing password for user thinkmo09.
passwd: all authentication tokens updated successfully.
thinkmo09的密码是:b7b9537c
Changing password for user thinkmo10.
passwd: all authentication tokens updated successfully.
thinkmo10的密码是:cb1c97b8
[root@localhost ~]# 
[root@localhost ~]# 

执行后
[root@localhost ~]# cat /etc/passwd | tail -n 10
thinkmo01:x:1001:1001::/home/thinkmo01:/sbin/nologin
thinkmo02:x:1002:1002::/home/thinkmo02:/sbin/nologin
thinkmo03:x:1003:1003::/home/thinkmo03:/sbin/nologin
thinkmo04:x:1004:1004::/home/thinkmo04:/sbin/nologin
thinkmo05:x:1005:1005::/home/thinkmo05:/sbin/nologin
thinkmo06:x:1006:1006::/home/thinkmo06:/sbin/nologin
thinkmo07:x:1007:1007::/home/thinkmo07:/sbin/nologin
thinkmo08:x:1008:1008::/home/thinkmo08:/sbin/nologin
thinkmo09:x:1009:1009::/home/thinkmo09:/sbin/nologin
thinkmo10:x:1010:1010::/home/thinkmo10:/sbin/nologin
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# su thinkmo10
This account is currently not available.
[root@localhost ~]# 

d)、猜数字游戏

# 脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了,

# 猜小了或猜大了,直至用户猜对脚本结束。

# RANDOM 为系统自带的系统变量,值为 0‐32767的随机数

# 使用取余算法将随机数变为 1‐100 的随机数

 

程序:

#!/bin/bash
#猜数字游戏
#管理员邮箱:xxx@xx.com
#2019年03月18日

gass_num=$(expr $RANDOM % 100)

while true
do
        read -p "请输入一个[0-99]的整数:" num

        if [ $num -gt $gass_num ];then
                echo "大了!!!"
        elif [ $num -lt $gass_num ];then
                echo "小了!!!"
        else
                echo "猜对了,但是没有奖励^_^!!!"
                echo "游戏退出..."
                exit 0
        fi
done
执行结果:

[root@localhost ~]# 
[root@localhost ~]# ./gass_sum.sh 
请输入一个[0-99]的整数:50
小了!!!
请输入一个[0-99]的整数:75
大了!!!
请输入一个[0-99]的整数:60
大了!!!
请输入一个[0-99]的整数:55
大了!!!
请输入一个[0-99]的整数:53
猜对了,但是没有奖励^_^!!!
游戏退出...
[root@localhost ~]# 
[root@localhost ~]#

e)、输入三个数并进行升序排序

程序:

#!/bin/bash
#功能:输入三个数,按升序排序(冒泡排序法)
#管理者邮箱:xxx@xx.com
#时间:2019年03月18日

dust=()
for i in $(seq 0 2)
do
        read -p "请输入第$(expr $i + 1)个数:" num
        dust[i]=$num
done

echo "..."
echo "原数列为:${dust[@]}"

for x in $(seq 1 $(expr ${#dust[@]} - 1))
do
        for y in $(seq $(expr $x + 1) ${#dust[@]})
        do
                if [ ${dust[$x-1]} -gt ${dust[$y-1]} ];then
                        dustxx=${dust[$x-1]}
                        dust[$x-1]=${dust[$y-1]}
                        dust[$y-1]=$dustxx
                fi
        done
done

echo "..."
echo "新数列为:${dust[@]}"
执行结果:

[root@localhost ~]# 
[root@localhost ~]# ./seq_num.sh 
请输入第1个数:56
请输入第2个数:12
请输入第3个数:78
...
原数列为:56 12 78
...
新数列为:12 56 78
[root@localhost ~]# 
[root@localhost ~]# ./seq_num.sh 
请输入第1个数:67
请输入第2个数:34
请输入第3个数:12
...
原数列为:67 34 12
...
新数列为:12 34 67
[root@localhost ~]#

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值