I am doing a bash script to backup my database daily(on Mac OS X) and I want to save the newest 7 backup directories. Here is the problematic fragment of my script(for listing everything but the newest directories):
# Assigning a value to a variable named NUMBEROFFOLDERS. NUMBEROFFOLDERS=7 # Command that leaves the newest NUMBEROFFOLDERS and removes everything else. TOBEDELETED=`(ls -t|head -n $(($NUMBEROFFOLDERS));ls)|sort|uniq -u|sed -e 's,.*,"&",g'|xargs rm -rfv` # Executing the TOBEDELETED command. eval "$TOBEDELETED"
P.S. The command assigned in TOBEDELETED works when I enter it in a Terminal window, but when I run the script nothing happens.
EDIT: i) The script should list all directories in a specific destination twice differently; sort should parse them and only the newest directories will be taken off the list; sed should handle all spaces correctly and rm -rfv will delete all directories from the lists (directories to be deleted) with all their files.
ii) The script should leave the newest $NUMBEROFFOLDERS folders.
iii) I haven't received an error. I believe the issue is when I refer to the variable of the assigned command.