脚本练习题

脚本练习

达到的效果  ./output.sh   5  file.txt  输出指定文件第5行

./output  5-10  file.txt 输出指定文件第5-10行

#!/bin/bash
###read the argument to output the file content.
if [ -n $1 ] && ! [[ $1 =~ [a-zA-Z] ]] && [ -f $2 ];then              #判断行的参数是否存在且为数字,打开的文件是否存在
    if [[ $1 =~ [0-9]+-[0-9]+ ]];then                                 #判断行参数是否符合 num-num的格式
         line_start=$[ `echo $1|cut -d\- -f1` + 1 ]                   #将参数中的首行赋给line_start
         line_end=`echo $1|cut -d\- -f2`                              #将参数中的末行赋给line_end
         if [[ $line_end -gt $line_start ]];then                      #判断行尾大于行首且行尾不大于文件总行
            if  [[ $line_end -le `cat $2|wc -l` ]];then
                cat $2|head -$line_end|tail -$line_start
            else
                echo "The line_end is out of file range."
                exit 2
            fi
         else 
            echo "The line_end can‘t greater than the line_start. "
            exit 3
         fi 
    else  
         if  [[ $1 -le `cat $2|wc -l` ]];then                         #判断指定行是否小于等于文件的总行数
            cat $2|head -$1|tail -1
         else 
            echo "The line is out of the file range"
            exit 4
         fi
    fi
else
    echo -e "Argument Error! \nThe usage is . /output line_start filename or ./output line_start-line_end filename\nExplame: ./output 5-10 /etc/passwd"
    exit 1
fi
时间: 2024-08-05 02:21:54

脚本练习题的相关文章

shell脚本练习题(更新中...)

练习题(这里贴的是自己写的代码, 网上给的题目代码我会附加在最下面) 1. 编写shell脚本,计算1-100的和: 1 #!/bin/bash 2 #caculate the sum of numbers from 1 to 100 3 4 sum=0 5 for i in `seq 1 100`; do 6 sum=$[$sum+$i] 7 done 8 echo $sum 2. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到

shell脚本练习题

求出数字1~100的累加和: 要求数字的累加,可以使用循环来完成,如下代码: #!/bin/bash sum=0 str="" #for i in {1..100} for ((i=1 ; i<=100 ; ++i)) do     str+="${i}+"     let sum+=i done echo "${str%+}=${sum}" 运行脚本: 另外一种可以用递归的方式: #!/bin/bash read num  sum=0 s

shell编程脚本练习题

1.使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为: [[email protected] oldboy]# sh /server/scripts/oldboy.sh [[email protected] oldboy]# ls coaolvajcq_oldboy.html  qnvuxvicni_oldboy.html  vioesjmcbu_oldboy.html gmkhrancxh_oldboy.html  tmd

day01脚本练习题

1.管理员用户登陆系统时,可以使用如下方式,开启并闭系统服务 服务名 start|restart|stop|status sshd start 答案: #tail /etc/bashrc export PATH=/etc/init.d:$PATH #source /etc/bashrc 2.每周日晚上23:30对数据库服务器webdb数据库做完整备份,把备份文件保存到/mysqlbak文件里,用系统日期做备份文件名 答案: #crontab -e 30 23 * * 7 /usr/bin/mys

day02脚本练习题

一.four.sh输出用户从健盘输入的任意2个数的四则运算结果 如果用户有一个数值没有输入,就终止脚本 #!/bin/bash read -p "请输入第一个数:"  num1 read -p "请输入第二个数:"  num2 [ -z $num1 ] && exit [ -z $num2 ] && exit sum=`expr $num1 + $num2` jian=`expr $num1 - $num2` cheng=`expr

day03脚本练习题

1.用while循环输出数字1到10(降序输出) 用while循环输出数字35到50(升序输出) #!/bin/bash j=10 i=35 while [ $j -ge 1 ] do echo$j let j-- done while [  50 -ge $i ] do echo$i let i++ done 2.批量添加15个系统用户要求如下 用户名的密码与用户名相同 用户名的前辍是user,用户名格式如下 user01 user02 ... user09 user10 ... user15

day04脚本练习题

1.在fun.sh脚本里定义函数sum_two(功能是能够计算任意两个数相加的和) #!/bin/bash #yi=$1 #er=$2 if [ -z $yi ] || [ -z $er ];then echo数字不能为空 if sum_two() { #sum=`expr $yi + $er` sum=`expr $1 + $2` #echo "$yi + $er = $sum" echo "$1 + $2 = $sum" } #sum_two $1 $2 sum

Linux学习—脚本练习题

shell脚本练习 1.编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满 #!/bin/bash #编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满 #************************************************************************************************

Shell学习之路和我发布过的Shell脚本博文

Shell学习之路 目录 Shell学习之路[第一篇]:别名,管道,用户配置文件,变量,read Shell学习之路[第二篇]:条件测试,运算符,选择结构,for循环结构 Shell学习之路[第三篇]:While循环,C-for循环,Until循环,case分支结构,流程控制语句 Shell学习之路[第四篇]:函数,数组,变量替换 Shell学习之路[第五篇]:多线程脚本 Shell学习之路[第六篇]:Trap信号捕捉命令介绍与Shell结合实战讲解 Shell学习之路[第七篇]:Linux下d