shell编程题

1.输入一个域名或IP检查域名或IP是否能访问

#!/bin/bash
#input a area or a IP
read -p "input a network:" ip
#ping ip
ping  -c 3 -W 1 $ip &> /dev/null  
#-c 3 count for ping
#-W 1 time out = 1s
#&> /dev/null remove the display of ping

#check ping
if [ $? -eq 0 ]
then
    echo "the network is ok!"
else
    echo "something wrong!"
fi

 2.I am oldboy linux,welcome to our training.显示这串字符中单词字符数大于6的单词

#!/bin/bash

# replace commas and periods with spaces
text=$(echo "I am oldboy linux,welcome to our training." | tr ',.' ' ')

# Iterate through each word in the processed text
for word in $text
do
    # Check if the length of the current word is greater than 6
    if [ ${#word} -gt 6 ]
    then
        # Print the word if its length is greater than 6
        echo $word
    fi
done

3.实现简单计算器


                                从命令行输入2个参数sh cal.sh 10 20
                                计算他们的+ - * / 结果显示出来

#!/bin/bash
x=$1
y=$2

awk -vn1=$x -vn2=$y 'BEGIN{print n1+n2}'
awk -vn1=$x -vn2=$y 'BEGIN{print n1-n2}'
awk -vn1=$x -vn2=$y 'BEGIN{print n1*n2}'
awk -vn1=$x -vn2=$y 'BEGIN{print n1/n2}'

4.检测本机当前用户是否为超级管理员,如果是管理员则使用yum安装vsftpd,如果不是,则提示您非管理员

method 1:

#!/bin/bash

# Check if the script is being executed by the root user
#UID=0 stand the root
[ $UID -eq 0 ] || {
    echo "You are not root. Exiting..."
    exit 1
}

# If the script is executed by the root user, install vsftpd
[ $UID = 0 ] && {
    echo "Installing vsftpd..."
    yum install vsftpd -y
}


meathod 2:

#!/bin/bash
name=`whoami`
[ "name"="root" ]||{
        echo "you are not root"
        exit 1
}

[ "name"="root" ]&&{
        yum install vsftpd -y
}

5.设计一个shell程序, 在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名,为如下形式yymmdd_ etc,yy为年,mm为月,dd为日。Shel程序fileback存放在/usr/bin目录下。

#!/bin/bash

# Define backup directory and current date
dir=/root/bak
time=$(date +%y%m%d)

# Create the backup directory if it doesn't exist
[ -d $dir ] || mkdir -p $dir

# Use the tar command to archive the /etc/ directory into a tar.gz compressed file named with the current date
tar -zcvf ${dir}/${time}_etc.tar.gz /etc/

定时任务部分:

[root@serverc ~]# crontab -e
00 00 01 * * /bin/bash test.sh &> /dev/null
#每个月的第一天午夜时执行,并将输出重定向到/dev/null以丢弃任何输出

6.用户输入1个字符,判断这个字符是数字,字母(大写),小写,还是其他

#!/bin/bash


case "$1" in
    [a-z])
        echo "Lowercase letter."
        ;;
    [A-Z])
        echo "Uppercase letter."
        ;;
    [0-9])
        echo "Number."
        ;;
    *)
        echo "Other type."
        ;;
esac

7.使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:


 

# Specify the target directory
dir="oldboy/"

# Check if the directory exists, create it if not
[ -d "$dir" ] || mkdir -p "$dir"

# Loop to create 10 files with random names in the target directory
for i in {1..10}; do
    # Generate a random string containing alphanumeric characters
    name=$(tr -cd 'a-zA-Z0-9' </dev/urandom | head -c10)

    # Create a file with the generated random name in the target directory
    touch "${dir}${name}_oldboy.html"
done

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值