unix - shell prints line shell -
write script named print_lines.sh
uses head
, tail
print out specific set of lines file. script should take 3 arguments: line number start at, line number stop at, , file use. here's example run:
[user@localhost ~]$ print_lines.sh 7 10 /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
in example, script prints line 7 through 10 (inclusive) of /etc/passwd
file. script must error checking. specifically, need check following things:
- you right number of arguments (3).
- the file specified exists , normal file.
- the first line number specified less or equal last line number specified.
- the actual number of lines in file greater last line printed.
- if of conditions not true, should print appropriate error message user , stop. if met, you'll need bit of arithmetic , use head , tail print out lines requested.
this work not working good
filename=$1 firstline=$2 lastline=$3 i=0 exec <${filename} # redirect file our stdin while read ; # read each line reply variable i=$(( $i + 1 )) # maintain line count if [ "$i" -ge "${firstline}" ] ; if [ "$i" -gt "${lastline}" ] ; break else echo "${reply}" fi fi done
Comments
Post a Comment