Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need borne shell to delete all but last two backups

Posted on 2012-04-11
3
Medium Priority
?
414 Views
Last Modified: 2012-04-12
I wrote this shell scrip whose intent is to delete all but the last two files that match a particular pattern. I am attempting to set the variable $backupcnt to the number of files that match the pattern in line 4.

The script fails on line 4  with: "line 4: backupcnt: command not found" and on the while loop test: " line 6: [: -ge: unary operator expected"


#!/bin/bash
set -v -x
cd /opt/backup
backupcnt = $(ls -t -1 | grep $1_`hostname` | wc -l)

while [ $backupcnt -ge 2 ]
do
        ls -t -1 | grep $1_`hostname` > temp.txt
        echo cat temp.txt
        cat temp.txt
        echo rm `cat temp.txt | tail -1`
        rm `cat temp.txt | tail -1`
        backupcnt = $(ls -t -1 | grep $1_`hostname` | wc -l)
done
0
Comment
Question by:Christopher Schene
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 37836100
backupcnt=$(ls -t -1 | grep $1_`hostname` | wc -l)
0
 

Author Closing Comment

by:Christopher Schene
ID: 37836217
Perfect! Works great! Thanks for the fast response.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37836389
You can simplfy the whole script to

#!/bin/bash
ls -t $1_$(hostname) | sed 'N;$!P;$!D;$d' | xargs rm

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question