-gt option in UNIX

Dear All,

Trying to understand below portion of one existing script
RESTOREPOINTS = 3 which is being passed as run time parameter.
$BACKUPLIST is having following 3 file names for example:

2019-02-01T00_00_01.590462914Z
2019-02-01T03_00_01.622179359Z
2019-02-01T06_00_01.635216799Z
 COUNT=$(echo "${BACKUPLIST}" | wc -l)
 if [ "$COUNT" -gt "$RESTOREPOINTS" ]
       then
              START=$(echo "${BACKUPLIST}" | sed -n 1p)
              END=$(echo "${BACKUPLIST}" | sed -n $((1+COUNT-RESTOREPOINTS))p)

does

 if [ "$COUNT" -gt "$RESTOREPOINTS" ]

means if Count = Restorepoints ? what does that -gt option represents ?

START variable I am able to understand which mean it will print the 1st line of the $BACKUPLIST

I dont understand below line

END=$(echo "${BACKUPLIST}" | sed -n $((1+COUNT-RESTOREPOINTS))p)

can some one clarify what will be value for sed in this statement?

The -gt can be read as "is greater than".

1 Like

thank you Don Cragun :slight_smile: Now understood :slight_smile:

I'm afraid that entire script doesn't make too much sense. You may want to run it with the -x (xtrace=) option set to see how and where it fails.

First logical error: COUNT=$(echo "${BACKUPLIST}" | wc -l) . Guessing you want that file's line count, try

COUNT=$(<"${BACKUPLIST}"  wc -l)

Otherwise it will just count the file name string, most probably resulting in 1.

Same here START=$(echo "${BACKUPLIST}" | sed -n 1p) and here END=$(echo "${BACKUPLIST}" | sed -n $((1+COUNT-RESTOREPOINTS))p)
If what you're after is to repeat the restores from first to $RESTOREPOINTS th, try to replace by

START=$(sed -n 1p  "${BACKUPLIST}");
 END=$(sed -n "$((1+COUNT-RESTOREPOINTS))p" "${BACKUPLIST}")

The terminating fi is missing, but there might be code lines following.
Did you consider the head and tail *nix text tools?

Maybe this will be useful as a hint?

COUNT=($BACKUPLIST)
if [ ${#COUNT[@]} -gt "$RESTOREPOINTS" ]; then
    START=$COUNT
    END=${COUNT[@]: -1}

@nezabudka : interesting approach - but you might need to use the difference between $COUNT and $RESTOREPOINTS in lieu of the simple -1 .

1 Like

there is no need increment. Array index starts at 0

COUNT=($BACKUPLIST)
d=$((${#COUNT[@]} - "$RESTOREPOINTS"))
if [ $d -gt 0 ]; then
    START=$COUNT
    END=${COUNT[@]:$d:1}

I've been too smart here:)

${COUNT[$d]} == ${COUNT[@]:$d:1}
1 Like

numerical comparison.

Probably not. The way threads O/P showed the string in #1 the string contains filenames separated by newlines and the command results in the number of filenames then. I don't say this is a good idea but under these circumstancs it at least makes sense:

x='first line
second line
third line'
echo "$x" | wc -l
3

I hope this helps.

bakunin

1 Like

Oohhh. Yes, if you look at it from this aspect, the echo ing of the variable makes some sense; I was wrong. I interpreted $BACKUPLIST to point to a file listing the backups.
Thanks for pointing this out.

Wouldn't it make more sense to use a shell array (saving running many external commands) to achieve the same goal? Like

$ IFS=$'\n'
$ BCKARR=(${BACKUPLIST})
$ echo ${#BCKARR[@]}
3
$ if [ ${#BCKARR[@]} -gt $RESTOREPOINTS ]
>    then START=${BCKARR[0]}
>         END=${BCKARR[${#BCKARR[@]}-$RESTOREPOINTS]}
>    fi

Hmmm - I see nezabudka was on the same track.

1 Like