#!/bin/sh
START=$1
END=$2
FILENAME=$3
ERROR="[PINCH ERROR]"
if [ $# -lt 3 ]; then
echo "$ERROR Need three arguments: Filename Start-line End-line"
exit 1
fi
if [ ! -f "$FILENAME" ]; then
echo -e "$ERROR File does not exist. \n\t$FILENAME"
exit 1
fi
if [ "$START" -gt "$END" ]; then
echo -e "$END $START"
exit 1
fi
if [ "$START" -lt 0 ]; then
echo -e "$ERROR Start line is less than 0."
exit 1
fi
if [ "$END" -lt 0 ]; then
echo -e "$ERROR End line is less than 0."
exit 1
fi
NUMOFLINES=$(wc -l < "$FILENAME")
ENDDIFF=$(( NUMOFLINES - END ))
if [ "$START" -lt "$ENDDIFF" ]; then
< "$FILENAME" head -n $END | tail -n +$START
else
< "$FILENAME" tail -n +$START | head -n $(( END-START+1 ))
fi
exit 0
The Problem I have is that if start is greater than end (exemple ./file 10 7) I need to print the the lines from start arg back to end arg (so basically line 10 9 8 7). Don't know how to do that :
if [ "$START" -gt "$END" ]; then
echo -e "$END $START"
exit 1
fi
Aucun commentaire:
Enregistrer un commentaire