Shell 经典面试例题

1.shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。

编写脚本:

#!/bin/bash

FILE="/tmp/size.log"

if -f "$FILE" ; then

echo "文件存在,显示文件内容:"

cat "$FILE"

else

echo "文件不存在,创建文件并写入创建时间。"

echo "创建时间:(date)" \> "FILE"

fi

执行脚本结果:

root@localhost \~# vim 1.sh

root@localhost \~# chmod +x 1.sh

root@localhost \~# ./1.sh

文件不存在,创建文件并写入创建时间。

2.写一个 shel1 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。

编写脚本:

#!/bin/bash

USER_PREFIX="user"

USER_COUNT=20

for ((i=1; i<=USER_COUNT; i++)); do

USERNAME="{USER_PREFIX}(printf "%02d" $i)"

PASSWORD="user$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 5)"

sudo useradd $USERNAME

echo -e "PASSWORD\\nPASSWORD" | sudo passwd $USERNAME

echo "用户 USERNAME 添加成功,密码为:PASSWORD"

done

执行脚本结果:

Changing password for user user01.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user01 添加成功,密码为:useraPHte

Changing password for user user02.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user02 添加成功,密码为:userclBkI

Changing password for user user03.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user03 添加成功,密码为:user5B8Yg

Changing password for user user04.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user04 添加成功,密码为:userCZmXl

Changing password for user user05.

New password: BAD PASSWORD: The password contains the user name in some form

Retype new password: passwd: all authentication tokens updated successfully.

用户 user05 添加成功,密码为:user0k5xR

Changing password for user user06.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user06 添加成功,密码为:userGaAG1

Changing password for user user07.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user07 添加成功,密码为:userZefFN

Changing password for user user08.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user08 添加成功,密码为:userord6b

Changing password for user user09.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user09 添加成功,密码为:userLitYu

Changing password for user user10.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user10 添加成功,密码为:userj37Vx

Changing password for user user11.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user11 添加成功,密码为:userv23j9

Changing password for user user12.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user12 添加成功,密码为:useruKikd

Changing password for user user13.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user13 添加成功,密码为:userxH9hs

Changing password for user user14.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user14 添加成功,密码为:userQyMIF

Changing password for user user15.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user15 添加成功,密码为:userMSIyp

Changing password for user user16.

New password: BAD PASSWORD: The password contains less than 3 character classes

Retype new password: passwd: all authentication tokens updated successfully.

用户 user16 添加成功,密码为:userJcDYt

Changing password for user user17.

New password: BAD PASSWORD: The password contains the user name in some form

Retype new password: passwd: all authentication tokens updated successfully.

用户 user17 添加成功,密码为:userh1Vsr

Changing password for user user18.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user18 添加成功,密码为:user7Zgho

Changing password for user user19.

New password: BAD PASSWORD: The password contains the user name in some form

Retype new password: passwd: all authentication tokens updated successfully.

用户 user19 添加成功,密码为:usery1vKl

Changing password for user user20.

New password: Retype new password: passwd: all authentication tokens updated successfully.

用户 user20 添加成功,密码为:userMeXQ3

3.编写个shel 脚本将/usr/local 日录下大于10M的文件转移到/tmp目录下

编写脚本:、

#!/bin/bash

SOURCE_DIR="/usr/local"

TARGET_DIR="/tmp"

if ! -d "$TARGET_DIR" ; then

mkdir -p "$TARGET_DIR"

fi

find "SOURCE_DIR" -type f -size +10M -exec mv {} "TARGET_DIR" \;

echo "大于 10M 的文件已转移到 $TARGET_DIR"

执行脚本结果:

root@localhost \~# vim 3.sh

root@localhost \~# chmod +x 3.sh

root@localhost \~# ./3.sh

大于 10M 的文件已转移到 /tmp

以上为三个题目的解答!

相关推荐
码农学院3 小时前
基于运维监控体系的网络品牌推广方案:从架构设计到技术实现
运维·网络
爱写代码的森9 小时前
鸿蒙三方库 | harmony-utils之ImageUtil图片保存到本地详解
服务器·华为·harmonyos·鸿蒙·huawei
大耳朵-小飞象12 小时前
电力安全运维的智能密码:BACS如何破解设备全生命周期管理难题,让电网安全“看得见、管得住”?
运维·安全·智慧城市·能耗系统·楼宇智控·未来生活
极客侃科技12 小时前
制造企业 MES/APS 选型:SAP PP/DS 集成、ERP-MES 边界划分与一体化架构要点
运维·架构·制造
HLC++13 小时前
Linux的进程间通信
android·linux·服务器
华清远见IT开放实验室14 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
白露与泡影14 小时前
Arthas 实战指南:从方法耗时定位到 JVM 变量热修改
服务器·jvm·c#
神奇霸王龙15 小时前
Claude Code屠榜:MiMo与Grok紧追Codex
服务器·网络·人工智能·gpt·ai·ai编程
Web极客码15 小时前
WordPress SEO优化:提升网站排名的13个关键步骤
服务器·seo·wordpress
生活爱好者!15 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器