shell脚本习题

本文包含了一系列的Shell脚本编程题目,涉及用户问候、数值运算、目录操作、文件管理、权限设置、日期时间处理、乘法表、猜数字游戏、账号创建、字符串操作等。通过这些练习,可以提升对Shell脚本的掌握和应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shell脚本习题


1.写一个脚本,要求如下:设定变量Fa的值为/etc/passwd依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么。结果输出如下:

Hello,root,your UID is 0.统计当前系统一个有多少个用户并输出

#!/bin/bash
Fa=/etc/passwd
sum=$(cat $Fa|wc -l)
while read A;do
       username=$(echo $A|awk -F: '{print$1}')
       user_uid=$(echo $A|awk -F: '{print$3}')
echo   hello,$username,your UID is $user_uid
done < $Fa
echo "当前有$sum用户

2.写一个脚本,传递两个整数给此脚本,让脚本分别计算并显示这两个整数的和,差,积,商

#!/bin/bash
A=7
B=6
echo "$[$A+$B]"
echo "$[$A-$B]"
echo "$[$A*$B]"
sum=$(echo  "scale=2;$A/$B"|bc)
echo $sum

3.写一个脚本,要求如下:创建目录/tmp/scripts切换至此目录中复制/etc/pam.d目录至当前目录,并重命名为test将当前目录的test及其里面的文件和子目录的属主改为redhat将test及其子目录中的文件的其它用户的权限改为没有任何权限

#!/bin/bash
mkdir /tmp/scripts
cd /tmp/scripts
cp -rf /etc/pam.d ./test
useradd redhat
chown -R redhat ./test
chmod -R o-rwx ./test

4.写一个脚本,要求如下:显示当前系统日期和时间,而后创建目录/tmp/lstest切换工作目录至/tmp/lstest创建目录a1d,b56e,6test创建空文件xy,x2y,732列出当前目录下以a,x或者6开头的文件或目录列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录

#!/bin/bash
date
mkdir /tmp/lstest
cd /tmp/lstest
mkdir a1d,b56e,6test
touch xy,x2y,732
ls [a,x,6]*
ls | egrep -i "^[a-z] [0-9]"

5. 99乘法表脚本

//echo -n 不换行输出
//echo -e 处理特殊字符

#!/bin/bash
for i in $(seq 9);do
for s in $(seq 9);do
   [ $i -ge $s ] && echo -n "[$i*$s]=$(( $i * $s ))"
done
echo
done
for ((p=1;p<=9;p++));do
for ((q=1;q<=p;q++));do

[ $q -le $p ] || [ $q -le $p ] && echo -n [$p*$q]=$(($p*$q))

done
echo
done

2、写一个猜数字的游戏脚本

#!/bin/bash

read -p "请输入一个数字:" sum

if [ $sum -ge 0 -a $sum -le 100 ];then
   echo "输入数字正确!"

else
   echo "输入数字错误!" 

fi

请输入数字,正确数字888,输入数字不能超过三次

#!/bin/bash

num=888
for ((i=1;i>=1;i++));do

  read -p "Please speak number:" sum
  if [ $i -gt 3 ];then
    echo "您输入次数过多!"
  break
  fi

  echo $sum | egrep '^[0-9]+$' &>/dev/null
  if [ $? -ne 0 ];then
    echo "请输入数字!"
    continue
  fi

  if [ $num -eq $sum ];then
     echo "You are right!"
     break

  elif [ $sum -gt $num ];then
     echo "Bigger"

  elif [ $sum -lt $num ];then
     echo "Samller"
  fi

done
#!/bin/bash

a=90
for ((i=1;i>=1;i++));do

read -p "请输入一个数字:" sum
if [ $i -gt 3 ];then
   echo "您输入次数过多!"
   break
fi

   echo "$sum"|egrep '^[0-9]+$'&>/dev/null
if [ $? -ne 0 ];then
   echo "请您输入数字!"
   continue
fi

if [ $sum -lt $a ];then
   echo "输入数字偏小!"

elif [ $sum -gt $a ];then
   echo "输入数字偏大!"

else
   echo "猜对了!真棒鸭!"
break
fi
done                                               

猜数字99

#!/bin/bash

a=99
while : ;do
    read -p "请输入一个数字:" sum
    echo "$sum"|egrep '^[0-9]+$'&>/dev/null
    if [ $? -ne 0 ];then
    echo "请重新输入数字!"
    continue
    fi
    if [ $sum -lt $a ];then
      echo "猜小了!"
    elif [ $sum -gt $a ];then
      echo "猜大了!"
    else
      echo "猜对了!"
      break
    fi

done

打印随机数:echo $RANDOM
随机打印两位数:echo $[RANDOM %100]

3、写一个猜年龄的游戏脚本

#!/bin/bash

read -p "请输入年龄:" i

while [ "$i" != 16 ];do
if [ $i -gt 16 ];then
echo "输入年龄偏大"
read -p "请再次输入年龄" i
else
echo "输入年龄偏小"
read -p "请再次输入年龄" i

fi
done
echo "对!永远16岁!"      

让用户输入N个数字,输出最大值和最小值

[root@lwq ~]# vim test.sh 
#!/bin/bash

max=$1
min=$2
for i in $*;do
    if [ $max -lt $i ];then
       max=$i
    fi
    if [ $min -gt $i ];then
        min=$i
    fi

done
echo "The max number is: " $max
echo "The min number is: " $min
[root@lwq ~]# bash test.sh 
The max number is: 
The min number is: 
[root@lwq ~]# bash test.sh 1 3 9 20 100 2 5 200 8
The max number is:  200
The min number is:  1

打印1-100间的数字,但是50-70不打印

#!/bin/bash

for i in $(seq 100);do
    [ $i -lt 50 -o $i -gt 70 ] && echo $i
done

1.使用for循环在/runtime目录下通过随机10个字符加固定字符串runtime批量创建10个html文件,结果类似qnvuxvicni_runtime.htmlcut -c 以字符为单位进行分割

#!/bin/bash

mkdir runtime
dir=/root/opt/runtime
[ ! -d $dir ] && mkdir -p $dir
for i in `seq 10`;do
touch $dir `echo $RANDOM|md5sum|cut -c 1-10`_runtime.html

done

2.将以上文件名中的runtime全部改成wangqing(用for循环实现),并且html改成大写

#!/bin/bash

mkdir runtime
dir=/root/opt/runtime
[ ! -d $dir ] && mkdir -p $dir
for i in `seq 10`;do
touch $dir `echo $RANDOM|md5sum|cut -c 1-10`_wangqing.HTML

done

3.批量创建10个系统帐号runtime01-runtime10并设置密码(密码为随机8位字符串)

head -c 显示文件字节

#!/bin/bash

for i in `seq 01 10`;do
   useradd runtime$i
   password=`head -c 500 /dev/urandom | md5sum |head -c 8`
   echo $password | passwd --stdin runtime$i

done

4.打印I am runtime teacher welcome to runtime training class.中字母长度不大于6的单词

//wc -L 打印最长行长度

#!/bin/bash

for i in I am runtime teacher welcome to runtime training class;do
if [ `echo $i|wc -L` -le 6 ];then
echo $i
fi

done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值