From 73e40bb9b64ef484acfd1717a4f6ef6196ac198f Mon Sep 17 00:00:00 2001 From: Amit Auddy Date: Tue, 7 Feb 2017 00:01:46 +0530 Subject: [PATCH] added sollution for exc02.sh --- exc02.sh | 5 ----- showline.sh | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) delete mode 100755 exc02.sh create mode 100644 showline.sh diff --git a/exc02.sh b/exc02.sh deleted file mode 100755 index 26b48eb..0000000 --- a/exc02.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# Write an script that accepts a file name and a number as its parameter, -# Then return the content of that line number in that file. - - diff --git a/showline.sh b/showline.sh new file mode 100644 index 0000000..fbf3b8c --- /dev/null +++ b/showline.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# Write an script that accepts a file name and a number as its parameter, +# Then return the content of that line number in that file. + +if [ $# -ne 2 ]; then + echo "Usage showline fileName lineNumber" + exit -1 +fi + +file=$1 +line=$2 + +if [ -e "$file" ]; then + totalLines=`wc -l $file | cut -d" " -f1` + if [ $line -le $totalLines ]; then + cat $file|sed -n "$line"p + else + echo "File exists but line does not exists!" + fi +else + echo "File does not exists!" +fi