批量建立用户脚本:

批量建立用户脚本:
#指定了要使用的解释器,这里是 Bash
#!/bin/bash

for i in {1..100}
do
    username="std$(printf "%03d" $i)"
    password="CentOS"

    # 创建用户
    sudo useradd -m -s /bin/bash $username
    
    # 设置默认登录口令
    echo "$username:$password" | sudo chpasswd
    
    # 第一次登录时要求更改口令
    sudo chage -d 0 $username
done

运行脚本:
bash create_users.sh

文件扩展名更名脚本:
#!/bin/bash

# 检查命令行参数的数量是否等于2
if [ "$#" -ne 2 ]; then
# $# 是用于获取传递给脚本的参数个数的特殊变量。
# -ne 是不等于的比较运算符。 
#  如果参数数量不正确,输出使用说明。$0 表示脚本自身的名字
    echo "Usage: $0 <old_extension> <new_extension>"
    exit 1
fi

old_extension="$1"
new_extension="$2"

for file in *."$old_extension"
do
    mv "$file" "${file%.$old_extension}.$new_extension"
done


运行脚本(假设脚本名为rename_files.sh):
bash rename_files.sh old_extension new_extension
(old_extension new_extension应该是具体的扩展名例如:
bash txt md)


3. 计算数字参数最大值脚本:
#!/bin/bash

max=0

#  循环遍历传递给脚本的所有参数。

#"$@" 表示所有的命令行参数。
for num in "$@"
do
# 检查当前参数是否大于 max。 -gt 是大于的比较运算符
    if [ $num -gt $max ]; then
        max=$num
    fi
done

# echo "最大值是: $max": 在循环结束后,输出存储在 max 变量中的最大值
echo "最大值是: $max"


运行脚本(假设脚本名为max_value.sh):
bash max_value.sh 5 2 8 3 7

请注意,在实际运行这些脚本之前,你可能需要给脚本文件添加执行权限,例如:
chmod +x create_users.sh rename_files.sh max_value.sh

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值