Shell 练习题

1.创建一个脚本,当执行脚本执行时,脚本可以如下显示:
(1).你目前的身份是:
(2).你目前所在的目录是:
2.创建一个脚本,该脚本可以根据你输入的日期计算出你的生日还有多少天
3.创建一个脚本,执行脚本后让用户输入一个数字,程序可以由1+2+3····一直加到shi用户输入的数字为止。
4.创建一个脚本,脚本作用如下:
(1).先查看/tmp/nebula这个名称是否存在
(2).如不存在则创建一个文件,建立后退出
(3).若存在则判断该名称是否是否是文件,若为文件则将其删除后创建一个同名目录,之后退出。
(4)如果存在且为目录则删除。
5.创建一个脚本,将/etc/passwd中以:为分隔符的第一个域取出,并在取出后的每一行中都以“The 1 account is ”root“ ”来表示,其中1表示行数。
代码:
1.解析:此题主要考察对基础命令的熟悉,当前用户身份用whoami当前目录用pwd;

#!/bin/bash
#Penman:liuyao
#Time:20190123
echo “Your current status is”: whoami #用户当前身份 用命令whoami
echo “Your current directory is”: pwd #当前所在目录 用命令pwd
运行结果:
[root@localhost ~]# sh s1.sh
Your current status is: root
Your current directory is: /root

2.解析:此题主要考点对date命令参数的了解,用到了-d %j参数;本题小编也没有做好你有完善的答案可以给发哈

#!/bin/bash
#Penman:liuyao
#Tmie:20190123
#代码基本能实现现在距离你生日天数,存在一些问题1,现在年份 2,生日过了加的365天 3.未判断输入月日是否正确 还有问题尚未想到
#输入的日期要为4位数
read -p “Please enter your Birthday :” Birthday
birthday=date -d 2019$Birthday +%j
Nowday=date +%j
#判断生日时间与现在,过了加365
if (( $ birthday>=$ Nowday ));then
Birthday=$ (($ birthday-$ Nowday))
echo -e “It’s still your birthday has:\033[5m$ Birthday\033[0m day”
else
Birthday=$ (($ birthday-$ Nowday+365))
echo -e “It’s atill your birthday has:\033[5m$ Birthday\033[0m day”
fi
运行结果:
[root@localhost ~]# sh s2.sh
Please enter your Birthday :0122
It’s atill your birthday has:361 day
[root@localhost ~]# sh s2.sh
Please enter your Birthday :0127
It’s still your birthday has:1 day
[root@localhost ~]# sh s2.sh
Please enter your Birthday :0126
It’s still your birthday has:0 day

3.解析:此题用到了for循环和变量的引用;

#!/bin/bash
#Penman:liuyao
#Time:20190124
read -p “Please enter a number :” sum #将输入的数值存入sum
sum1=0
for ((i=1;i<=$ sum;i++)) #利用for循环来遍历i的值小于等于输入的sum值
do
sum1=$ (($ sum1+$ i)) #累加0到sum值赋予sum1
done
echo “The sum of the number you enter is :”$sum1"" #输出结果
运行结果:
[root@localhost ~]# sh s3.sh
Please enter a number :10
The sum of the number you enter is :55

4.解析:此题主要考察了if条件判断,不常用的-e判断filename是否存在,认真审题逻辑顺序;

#!/bin/bash
#Penman:liuyao
#Time:20190124
if [ -e /tmp/nebula ];then #-e 如果 filename存在,则为真
#若存在且为文件删除文件创建同名目录
if [ -f /tmp/nebula ];then
rm -rf /tmp/nebula
mkdir -p /tmp/nebula
#添加输入语句判断是否成功
echo “The file was deleted and set up directory was successfully”
#若存在为目录删除目录
elif [ -d /tmp/nebula ];then
rm -rf /tmp/nebula
echo “directory was deleted”
fi
else
#若名称不存在,则直接创建文件
touch /tmp/nebula
echo “The file was created successfully”
fi
运行结果:
[root@localhost ~]# sh s4.sh
The file was created successfully
[root@localhost ~]# sh s4.sh
The file was deleted and set up directory was successfully
[root@localhost ~]# sh s4.sh
directory was deleted

5.解析:此题考察了用awk截取信息 -F分隔符;

#!/bin/bash
#Penman:liuyao
#Time:20190125
#用cat -n 显示要的内容和行号 利用两次awk截取
cat -n /etc/passwd | awk -F":" ‘{print $1 }’| awk ‘{ print "The “$1” account is “$2"”}’
运行结果:
[root@localhost ~]# sh s5.sh
The 1 account is root
The 2 account is bin
The 3 account is daemon
The 4 account is adm
The 5 account is lp
The 6 account is sync
The 7 account is shutdown
The 8 account is halt

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值