linux shell怎么练习,一些linux shelll简单练习_无需整理

整理了几道Shell编程实例,针对新手!

1. 在/home目录中创建一百个目录,目录名称依次为a1……a100.

2.  编写一个脚本,自动将用户主目录下所有小于5KB的文件打包成XX.tar.gz.(提示:用ls,grep,find等命令,文件一般指普通文件)

3.写一个程序,可以将/et/passwd的第一列取出,而且每一列都以一行字符串“the 1 account is “root””来显示

4. 编写一个程序,他的作用是先查看一下/root/test/logical这个名称是否存在,若不存在,则创建一个文件。使用touch来创建,创建完成后离开;如果存在的话,判断该名称是否为文件,若为文件则将之删除后新建一个目录。文件名为loglical,之后离开;如果存在的话,而且该名称为目录,则删除此目录。

5.编写一个shell脚本,从键盘读入10个数,显示最大值和最小值。

6.编写一个脚本,打印任何数的乘法表。如输入3则打印

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

7.编写一个脚本,输入自己的生日时间(YYYYMMDD),计算还有多少天多少个小时是自己的生日。

#第一题

for i in `seq 100`; do mkdir /home/a$i; done

#第二题

find ~ -size -5 -type f -maxdepth 1|xargs tar zcvpf backup.tar.gz

#第三题

awk -F':' '{print "the 1 account is "$1}' /etc/passwd

#第四题

if [ ! -e "/root/test/logical" ]; then touch "hh";  elif [ -f "/root/test/logical" ];then rm /root/test/logical && mkdir logical&&exit;elif  [ -d "/root/test/logical" ];then rm /root/test/logical; fi

#第五题

=> cat hh.sh

#! /bin/bash

printf "Enter 10 number: "

read

biggest=$(echo "$REPLY" | tr ' ' '\n' | sort -rn | head -n1)

smallest=$(echo "$REPLY" |  tr ' ' '\n' | sort -rn | tail -n1)

echo "Biggest number:  $biggest"

echo "Smallest number:  $smallest"

--------------------------------------------------

=> sh hh.sh

Enter 10 number: 1 2 3 4 5 6 7 8 9 0

Biggest number:  9

Smallest number:  0

#第六题

awk -vstr='3' 'BEGIN{for(i=1;i<=str;i++){for(p=1;p<=i;p++)printf p"*"i"="p*i"\t";printf "\n"}}'

#第七题

=> sh hh.sh

Input your birthday(YYYYmmdd):19930302

There is : 325 days 8 hours.

=> cat hh.sh

read -p "Input your birthday(YYYYmmdd):" date1

m=`date --date="$date1" +%m`

d=`date --date="$date1" +%d`

date_now=`date +%s`

y=`date +%Y`

birth=`date --date="$y$m$d" +%s`

internal=$(($birth-$date_now))

if [ "$internal" -lt "0" ]; then

birth=`date --date="$(($y+1))$m$d" +%s`

internal=$(($birth-$date_now))

fi

awk -vinternal=$internal 'BEGIN{d=int(internal/60/60/24);h=int((internal-24*60*60*d)/3600);print "There is : "d" days "h" hours."}'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值