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:
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
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
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