shell练习

1、要求:把一个文本文档的前5行中包含字母的行删除掉,同时把6到10行中的全部字母删除掉。
#! /bin/bash
nu=wc -l 2.txt | awk '{print $1}'
for i in seq 1 5
do
l=sed -n "$i"p 2.txt
if echo $l |grep -vq ‘[a-zA-Z]’
then
echo l f i d o n e f o r i i n ‘ s e q 610 ‘ d o l = ‘ s e d − n " l fi done for i in `seq 6 10` do l=`sed -n " lfidoneforiinseq610dol=sedn"i"p 2.txtecho $i | sed 's/[a-zA-Z]//g' done for i inseq 11 n u ‘ d o s e d − n " nu` do sed -n " nudosedn"i"p 2.txt
done

2、要求:使用传参的方法写个脚本,实现加减乘除的功能。
#!/bin/bash
if [ $# -ne 2 ];then
echo “Usage:basename $0 parameter1 parameter2”
echo “The operations of addition, subtraction, multiplication, and division of two positive integers.”
exit 1
fi

if [ ! -z echo $1 | sed 's@[[:digit:]]@@g' ] || [ ! -z echo $2 | sed 's@[[:digit:]]@@g' ];then
echo “Argument must be positive integer.”
exit 2
fi

SUM=$[$1+$2]
printf “%-10s %-10s\n” SUM: “$1 + $2 = $SUM”

if [ $1 -ge 2 ] ; t h e n S U B T R A C T = 2 ];then SUBTRACT= 2];thenSUBTRACT=[$1-$2]
printf “%-10s %-10s\n” Subtract: "$1 - $2 = S U B T R A C T " e l s e S U B T R A C T = SUBTRACT" else SUBTRACT= SUBTRACT"elseSUBTRACT=[$2-$1]
printf “%-10s %-10s\n” Subtract: “$2 - $1 = $SUBTRACT”
fi

MULTIPLY=$[$1*$2]
printf “%-10s %-10s\n” Multiply: “$1 * $2 = $MULTIPLY”

if [ $1 -ge $2 ];then
DIVIDE=echo "scale=2;$1/$2" | bc
printf “%-10s %-10s\n” Subtract: “$1 / $2 = $DIVIDE”
else
DIVIDE=echo "scale=2;$2/$1" | bc
printf “%-10s %-10s\n” Subtract: “$2 / $1 = $DIVIDE”
fi

你可能感兴趣的:(shell练习)