Linux--进程考核二--Shell编程

实验  Shell编程

  1. 在Linux环境下编写一个Shell脚本程序first,在屏幕输出“Hello GDUST!”。

示例:

#!/bin/sh
data="Hello GDUST"
echo $data
exit 0
~         
  1. 在Linux环境下编写一个Shell脚本程序max,该程序能接收用户从键盘输入的10个整数,并输出最大值。

#! /bin/bash
printf "Enter 10 number: "
read
biggest=$(echo "$REPLY" | tr ' ' '\n' | sort -rn | head -n1)
echo "Biggest number:  $biggest"
  1. 编写一个Shell程序menu,实现如下菜单界面:

 

#!/bin/sh

   echo "Menu"
   select YY in "Linux" "Android" "C++" "C" "NetworkSecurity" "OS"
   do
   echo "teacher is zg,he will teach $YY"
   break
   done
exit 0;

4在Linux环境下编写一个Shell脚本程序handgame,该程序为剪刀石头布小游戏,结果如下:

 

#!/usr/bin

#需求:用shell脚本写一个石头、剪刀、步的游戏

#定义函数ofen,存放经常使用到的输出语句
function often(){
        echo "你出的是:$person"
        echo "电脑出的是:$computer"
}
#用数字1、2、3分别模拟石头、剪刀、步
echo "输入1,2,3。1是石头,2是剪刀,3是布"
#1.提示用户出拳,并接出拳
read -p '请出拳[1(石头) | 2(剪刀) | 3(布)]:' person

#2.判断person是否为合法出拳,不是退出游戏
if [ $person -ne 1 -a $person -ne 2 -a $person -ne 3 ];then
        echo '请输入合法的猜拳!'
        exit
fi

#3.电脑出拳
computer=$[ $RANDOM % 3 + 1 ]

#4.比较person 和 computer的值,判断输赢
if [ $person -eq $computer ];then
        often
        echo "此局是平局"
elif [ $person -eq 1 -a $computer -eq 2 ] || [ $person -eq 2 -a $person -eq 3 ] || [ $person -eq 3 -a $computer -eq 1 ];then
        often
        echo "此局你赢了"
else
        often
        echo "此局你输了"
fi

此处代码转载于:(384条消息) 用shell写一个简单的石头、剪刀、步的猜拳游戏_shell脚本石头剪刀布游戏_家有千金行止由心的博客-CSDN博客

5、在Linux环境下编写一个猜数字的小游戏,游戏规则如下:首先程序随机生成一个64以内的整数,然后等待用户的输入

 

 

#!/bin/bash

# 生成随机整数
num=$(( $RANDOM % 64 + 1 ))
echo "The answer is $num"
echo "============================================="

# 输出游戏规则
echo "Welcome to the game! Guess a number between 1 and 64."

# 读取用户输入,直到猜中为止
while true
do
    read -p "Enter your guess: " guess

    # 判断是否输入了整数
    if [[ $guess =~ ^[0-9]+$ ]]; then
        # 判断是否猜中
        if [ $guess -eq $num ]; then
            echo "Guessed, you are clever!"
            exit 0
        elif [ $guess -gt $num ]; then
            echo "Too Large!"
        else
            echo "Too Small!"
        fi
    else
        echo "Please enter an integer!"
    fi
done

 

  1. 编写一个shell程序adduser,添加一个新组class,然后添加属于这个组的30个用户,用户名的形式为yhxx,并设置密码为yhxx。其中xx从01到30(前九个是01-09,后10-30)。

 

#!/bin/bash

# 创建新组class
sudo groupadd class

# 添加30个用户
for i in {1..30}
do
    # 用户名的形式为yhxx
    username="yh$(printf '%02d' "$i")"

    # 添加用户并设置密码为yhxx
    sudo useradd -m -p "$(openssl passwd -1 'yh'$i)" -g class "$username"
done

echo "Completed."

  1. 编写一个Shell程序countfile,能统计出当前目录中子目录、文件的数量。(使用ls -al命令时,不显示统计信息)

 

#!/bin/bash

# 统计子目录数量
subdir=$(find . -maxdepth 1 -type d | wc -l)

# 统计文件数量
file=$(find . -maxdepth 1 -type f | wc -l)

echo "Number of subdirectories: $((subdir - 1))"
echo "Number of files: $file"

  1. 编写一个Shell程序mulTable,输出九九乘法表。

 

 

#!/bin/bash

# 外层循环控制行数
for i in {1..9}
do
    # 内层循环控制列数
    for((j=1;j<=i;j++))
    do
        # 输出乘积
        echo -n "$i*$j=$(($i*$j)) "
    done

    # 换行
    echo ""
done
~           
  1. 编写shell脚本程序print。打印给定行数的*号。第一行打印一个。第二行打印两个,以此类推。系统提示“input a num:”,用户输入数字5。

 

 

#!/bin/bash

echo -n "input a num: "
read num

# 外层循环控制行数
for i in $(seq 1 $num)
do
    # 内层循环控制列数
    for j in $(seq 1 $i)
    do
        # 输出*
        echo -n "*"
    done

    # 换行
    echo ""
done

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值