免费培训学校网站源码,网站交互做的比较好的,wordpress自豪的采用,深圳网站美化1.编写一个Linux的shell编程#xff0c;计算输入两个年份之间的闰年之和。
#!/bin/bash# 获取用户输入的两个年份
echo 请输入两个年份#xff0c;用空格隔开:
read year1 year2# 检查输入的年份是否有效
if [[ ! $year1 ~ ^[0-9]$ ]] || [[ ! $year2 ~ ^[0-9]$…1.编写一个Linux的shell编程计算输入两个年份之间的闰年之和。
#!/bin/bash# 获取用户输入的两个年份
echo 请输入两个年份用空格隔开:
read year1 year2# 检查输入的年份是否有效
if [[ ! $year1 ~ ^[0-9]$ ]] || [[ ! $year2 ~ ^[0-9]$ ]]; thenecho 输入的年份格式不正确exit 1
fiif (( year1 year2 )); thenecho 第一个年份必须小于第二个年份exit 1
fi# 计算闰年之和
sum0
for((year$year1; year$year2; year)); doif (( year % 4 0 (year % 100 ! 0 || year % 400 0) )); then(( sum ))fi
done# 输出闰年之和
echo 从 $year1 年到 $year2 年之间的闰年之和是 $sum
2.编写一个linux编程要求用*号打印一个倒梯形。
#!/bin/bash# 获取倒梯形的行数
echo 请输入倒梯形的高度
read height# 创建一个与高度相同的二维数组
stars( )
for ((i0; iheight; i)); dostars[$i]
done# 在二维数组中填充星号
for ((i0; iheight; i)); dofor ((j0; jheight-i; j)); dostars[$i]*done
done# 打印二维数组
for ((i0; iheight; i)); doecho ${stars[$i]}
done3.编写一个linux的shell编程实现列出当前目录里的目录和文件的个数的功能。
#!/bin/bash# 获取当前目录的路径
current_dir$(pwd)# 使用 find 命令列出当前目录下的所有文件和目录
files_and_dirs$(find $current_dir -maxdepth 1 -type f -o -type d)# 将 find 命令的输出按换行符分割并保存到数组中
files_and_dirs_array( $files_and_dirs )# 将数组的长度作为文件和目录的总数
total_files_and_dirs${#files_and_dirs_array[]}# 分别计算文件和目录的数量
num_files0
num_dirs0
for file_or_dir in ${files_and_dirs_array[]}; doif [ -f $file_or_dir ]; then((num_files))elif [ -d $file_or_dir ]; then((num_dirs))fi
done# 打印文件和目录的数量
echo 当前目录 $current_dir 中有 $total_files_and_dirs 个文件和目录其中包括 $num_files 个文件和 $num_dirs 个目录。4.编写一个linux的shell编程实现打印九九乘法表的功能。
#!/bin/bash# 打印九九乘法表的标题
echo 九九乘法表# 使用 for 循环遍历 1 到 9
for i in $(seq 1 9); do# 使用嵌套的 for 循环遍历 1 到 9for j in $(seq 1 9); do# 计算 i 和 j 的乘积product$((i * j))# 格式化输出确保每列的宽度相同printf %2d x %-2d %-3d $i $j $productdone# 换行echo
done5.编写一个linux的shell编程实现在当前目录中添加test1test2, test3文件并把这三个文件分别剪切到dir1dir2dir3目录中去。
#!/bin/bash# 创建test1、test2和test3文件
touch test1 test2 test3# 创建dir1、dir2和dir3目录
mkdir dir1 dir2 dir3# 将test1文件剪切到dir1目录
mv test1 dir1# 将test2文件剪切到dir2目录
mv test2 dir2# 将test3文件剪切到dir3目录
mv test3 dir36.编写一个linux的shell编程制作一个计算器能进行加减乘除运算。
#!/bin/bash# 获取用户输入的两个数字和运算符
echo 请输入第一个数字
read num1echo 请输入第二个数字
read num2echo 请输入运算符、-、*、/
read operator# 根据运算符进行计算
case $operator in)result$((num1 num2));;-)result$((num1 - num2));;*)result$((num1 * num2));;/)if [ $num2 -eq 0 ]; thenecho 除数不能为0elseresult$((num1 / num2))fi;;*)echo 无效的运算符;;
esac# 打印计算结果
if [ -n $result ]; thenecho 计算结果为$result
fi7.编写一个linux的shell编程计算100以内的质数和。
#!/bin/bash# 初始化质数和的变量
sum0# 遍历100以内的数字
for ((i2; i100; i)); do# 假设i是质数is_prime1# 从2到i的平方根遍历for ((j2; j$(echo sqrt($i) | bc); j)); do# 如果i被j整除则i不是质数if (( i % j 0 )); thenis_prime0breakfidone# 如果i是质数则将i添加到质数和中if (( is_prime 1 )); thensum$((sum i))fi
done# 打印质数和
echo 100以内的质数和为$sum8.编写linux的shell编程把100以内能被3整除但不能被5整除的数列出来3个一行。
#!/bin/bash# Initialize a counter
i1# Iterate until i is greater than 100
while [ $i -le 100 ]
do# Check if i is divisible by 3 and not divisible by 5if [ $((i % 3)) -eq 0 ] [ $((i % 5)) -ne 0 ]then# Print i, followed by a space characterprintf %3d $i# Increment the counter by 3((i 3))# Check if the counter is greater than 100if [ $i -gt 100 ]then# Print a newline character to terminate the lineechofielse# Increment the counter by 1((i 1))fi
done 9.编写linux的shell编程制作一个电话本要有添加删除查询修改退出功能。
#!/bin/bash# 定义电话本数据结构
declare -A phonebook# 定义主菜单函数
function main_menu() {echo -----------------------------------------echo 电话本echo -----------------------------------------echo 1. 添加联系人echo 2. 删除联系人echo 3. 查询联系人echo 4. 修改联系人echo 5. 退出echo -----------------------------------------echo 请选择一个选项
}# 定义添加联系人函数
function add_contact() {echo 请输入联系人姓名read nameecho 请输入联系人电话号码read phone_numberphonebook[$name]$phone_numberecho 联系人已添加。
}# 定义删除联系人函数
function delete_contact() {echo 请输入要删除的联系人姓名read nameif [ -z ${phonebook[$name]} ]; thenecho 联系人不存在。elseunset phonebook[$name]echo 联系人已删除。fi
}# 定义查询联系人函数
function query_contact() {echo 请输入要查询的联系人姓名read nameif [ -z ${phonebook[$name]} ]; thenecho 联系人不存在。elseecho 联系人电话号码${phonebook[$name]}fi
}# 定义修改联系人函数
function modify_contact() {echo 请输入要修改的联系人姓名read nameif [ -z ${phonebook[$name]} ]; thenecho 联系人不存在。elseecho 请输入新的联系人电话号码read new_phone_numberphonebook[$name]$new_phone_numberecho 联系人已修改。fi
}# 主循环
while true; domain_menuread choicecase $choice in1)add_contact;;2)delete_contact;;3)query_contact;;4)modify_contact;;5)echo 退出电话本。exit 0;;*)echo 无效的选项。;;esac
done10.编写linux的shell编程制作一个石头剪刀布的游戏。
#!/bin/bash# 定义游戏手势
declare -a gestures(石头 剪刀 布)# 定义游戏规则
declare -A rules([石头][剪刀]石头赢[石头][布]布赢[剪刀][石头]石头赢[剪刀][布]剪刀赢[布][石头]布赢[布][剪刀]剪刀赢
)# 获取玩家手势
function get_player_gesture() {echo 请输入您的手势石头、剪刀或布read player_gesturewhile [[ ! ${gestures[]} ~ $player_gesture ]]; doecho 无效的手势请重新输入read player_gesturedone
}# 获取电脑手势
function get_computer_gesture() {computer_gesture${gestures[$RANDOM % 3]}
}# 判断胜负
function judge_winner() {if [[ $player_gesture $computer_gesture ]]; thenecho 平局elif [[ ${rules[$player_gesture][$computer_gesture]} $player_gesture赢 ]]; thenecho 玩家赢elseecho 电脑赢fi
}# 打印结果
function print_result() {echo 玩家手势$player_gestureecho 电脑手势$computer_gestureecho 胜负结果$(judge_winner)
}# 主循环
while true; doget_player_gestureget_computer_gestureprint_resultecho 是否继续游戏y/nread continue_gameif [[ $continue_game ! y ]]; thenbreakfi
done11.编写linux的shell编程打印一个五角星。
#!/bin/bash
#打印五角星
echo 输出五角星图案
for (( i11;i16;i1 )) #这是五角星的上面一个角
dolet c19-$i1for (( j11;$j1$c;j1 )) #这是空格do
echo -n done
let d2*$i1-1
for (( k11;$k1$d;k1 )) #这是“*”do
echo -n *
done
echodonefor (( i21;i25;i2 )) #这是五角星的中间两个角do
let e3*$i2-3for (( j21;$j2$e;j2 ))do
echo -n
done
let f42-6*$i2
for (( k21;$k2$f;k2 ))do
echo -n *
done
echodonefor (( i31;i33;i3 )) #这是中间与下部相接的部分do
let g12-$i3for (( j31;j3$g;j3 ))do
echo -n donelet h122*$i3
for (( k31;k3$h;k3 ))do
echo -n * done
echodonefor (( i41;i45;i4 )) #这是五角星的下面两个角do
let o10-$i4for (( j41;j4$o;j4 ))do
echo -n done
let p10-2*$i4
for (( k41;k4$p;k4 ))do
echo -n * done
let q6*$i4-3
for (( m41;m4$q;m4 ))do
echo -n done
for (( n41;n4$p;n4 ))do
echo -n *
done
echo
doneecho * * #盖上角
12.编写一个shell编程制作一个猜数字游戏。
#!/bin/bash# 生成随机数
random_number$((RANDOM % 100 1))# 获取用户猜测的数字
echo 请输入您猜测的数字
read guess# 判断用户猜测的数字是否正确
while [[ $guess ! $random_number ]]; doif [[ $guess -lt $random_number ]]; thenecho 您的猜测太小了请重新猜测elseecho 您的猜测太大了请重新猜测firead guess
done# 告诉用户猜对了
echo 恭喜您您猜对了13.编写linux的shell编程打印一个10以内的加法表。
#!/bin/bash# 打印加法表的标题
echo 10以内的加法表# 使用 for 循环遍历 1 到 10
for ((i1; i10; i)); do# 使用嵌套的 for 循环遍历 1 到 10for ((j1; j10; j)); do# 计算 i 和 j 的和sum$((i j))# 格式化输出确保每列的宽度相同printf %2d %-2d %-3d $i $j $sumdone# 换行echo
done14.编写linux的shell编程打印一个10以内的减法表。
#!/bin/bash# 打印减法表的标题
echo 10以内的减法表# 使用 for 循环遍历 1 到 10
for ((i1; i10; i)); do# 使用嵌套的 for 循环遍历 1 到 10for ((j1; j10; j)); do# 计算 i 和 j 的差difference$((i - j))# 格式化输出确保每列的宽度相同printf %2d - %-2d %-3d $i $j $differencedone# 换行echo
done15.编写linux的shell编程把输入的数字进行求和。
#!/bin/bash# 获取用户输入的数字
echo 请输入要求和的数字用空格分隔
read numbers# 将数字分割成数组
numbers_array( $numbers )# 初始化求和结果
sum0# 遍历数组中的每个数字并将其添加到求和结果中
for number in ${numbers_array[]}; dosum$((sum number))
done# 打印求和结果
echo 求和结果为$sum16.编写linux的shell编程打印一个等腰三角形。
#!/bin/bash# 获取用户输入的行数
echo 请输入等腰三角形的高度
read height# 打印等腰三角形的每一行
for ((i1; iheight; i)); do# 计算当前行的空格数和星号数num_spaces$((height - i))num_stars$((2 * i - 1))# 打印空格for ((j1; jnum_spaces; j)); doecho -n done# 打印星号for ((j1; jnum_stars; j)); doecho -n *done# 换行echo
done17.编写linux的shell编程求1-1000中偶数之和。
#!/bin/bash# 初始化偶数之和
even_sum0# 遍历1到1000
for ((i1; i1000; i)); do# 如果i是偶数则将其添加到偶数之和中if (( i % 2 0 )); theneven_sum$((even_sum i))fi
done# 打印偶数之和
echo 1-1000中偶数之和为$even_sum18.编写linux的shell编程要求输入一个数字输出一个*的菱形。
#!/bin/bash# 获取用户输入的行数
echo 请输入菱形的高度
read height# 打印菱形的每一行
for ((i1; iheight; i)); do# 计算当前行的空格数和星号数num_spaces$((height - i))num_stars$((2 * i - 1))# 打印空格for ((j1; jnum_spaces; j)); doecho -n done# 打印星号for ((j1; jnum_stars; j)); doecho -n *done# 换行echo# 打印菱形的下半部分for ((i$((height-1)); i1; i--)); do# 计算当前行的空格数和星号数num_spaces$((i))num_stars$((2 * i - 1))# 打印空格for ((j1; jnum_spaces; j)); doecho -n done# 打印星号for ((j1; jnum_stars; j)); doecho -n *done# 换行echodone19.编写linux的shell编程要求输入一个数字以数字输出一个直角三角形。
#!/bin/bash# 获取用户输入的行数
echo 请输入直角三角形的高度
read height# 打印直角三角形的每一行
for ((i1; iheight; i)); do# 打印i个数字for ((j1; ji; j)); doecho -n $jdone# 换行echo
done20.编写linux的shell编程输入一个年份判断是不是闰年。
#!/bin/bash# 获取用户输入的年份
echo 请输入年份
read year# 判断年份是否能被4整除
if (( year % 4 0 )); then# 判断年份是否能被100整除if (( year % 100 0 )); then# 判断年份是否能被400整除if (( year % 400 0 )); thenecho $year是闰年。elseecho $year不是闰年。fielseecho $year是闰年。fi
elseecho $year不是闰年。
fi21.编写一个linux的shell编程要求输入一个数字n并计算1~n的和如果输入的数字小于1则重新输入直到输入正确的数字为止。
#!/bin/bash# 循环获取用户输入的数字直到输入正确的数字
while true; doecho 请输入一个数字nread n# 判断n是否大于等于1if (( n 1 )); thenbreakelseecho 输入的数字必须大于等于1请重新输入。fi
done# 计算1到n的和
sum0
for ((i1; in; i)); dosum$((sum i))
done# 打印1到n的和
echo 1到$n的和为$sum22.编写linux的shell编程要求批量创建用户user_00,user_01,.....,user_99.
#!/bin/bash# 定义要创建的用户名的前缀
user_prefixuser_# 循环创建用户
for i in $(seq -f %02g 0 99); dousername${user_prefix}${i}useradd -m $username
done23.编写linux的shell编程求1-1000以内的奇数之和。
#!/bin/bash# 初始化奇数之和
odd_sum0# 遍历1到1000
for ((i1; i1000; i)); do# 如果i是奇数则将其添加到奇数之和中if (( i % 2 ! 0 )); thenodd_sum$((odd_sum i))fi
done# 打印奇数之和
echo 1-1000以内的奇数之和为$odd_sum兄弟们花了点时间整理了一下希望对兄弟有用提醒一点的是做练习的时候写代码就别把代码里面的中文全部写进去了写一点就好。哪里有问题的还请兄弟们指正