bash – Beginner making an attempt to write down a easy script to automate command


Andy’s reply is right AFAICS. I am including this reply to offer some alternate options chances are you’ll need to contemplate:

There are two facets to your query: 1) is the PATH side wrt the placement of your script (snapshot_remove), and a couple of) is the syntax of the script itself. As I perceive your query, and the error message you posted, it appears the main side is re your PATH; i.e. the variable that defines the place the system seems to search out executable instructions you enter.

Let’s handle the PATH side first:

The PATH setting variable is the important thing to a part of your query. PATH determines the place the system will look (in what folders) to search out the “command” you enter. It seems your script is positioned in a folder that’s not included in your PATH. This is able to clarify your error message:

“bash: snapshot_remove: command not discovered”

You’ve got two (2) choices to resolve this error:

  1. transfer your script to a folder that is in your PATH, or
  2. modify your PATH setting variable to incorporate the folder containing snapshot_remove

Let’s begin with Possibility 1: Transfer your script :

Open a Terminal, and enter echo $PATH on the immediate to see what it accommodates. Hopefully, it can include the folder usr/native/bin. If it doesn’t, there might be any variety of the explanation why (Apple might have been inconsistent??), however the challenge is definitely remedied as proven under within the Addendum: Methods to Modify /and many others/paths.

Historically, in a Unix-like system setup, the folder usr/native/bin (or maybe /usr/native) could be the most popular location for locally-sourced executable information – akin to your snapshot_remove script. This location being IAW the “Filesystem Hierarchy Normal”. Accordingly, you may place your script in that folder, and neglect the entire remainder of the enterprise related to updating your PATH setting variable.

Possibility 2: Modify your PATH setting variable

The choice to putting your script in /usr/native/bin is to switch the PATH variable in your setting. I’ll assume your script is in your Customers folder (Customers/whoeveryouare). You’ll be able to add that folder to your PATH variable as a Short-term change, or you may add it as a Everlasting change:

Short-term change:

Assuming you are utilizing zsh, do that from Terminal:

path+=/Customers/whoeveryouare

# OR...

path+=/Customers/whoever/myscripts

Everlasting change:

Open your favourite editor, and add the next to .zshrc (or .bashrc for those who use bash):

cd /Customers/whoever 
nano .zshrc
#
# add one line:

export PATH=/Customers/whoever:$PATH

NOTE: It has been reported that Apple doesn’t embody a ~/.zshrc file. If that is your case, simply create the file your self & modify it as described above.

Now let’s handle the “script syntax” side:

As I perceive your script, you’re utilizing discover to delete all information in a collection of folders that have been modified greater than 25 hours in the past. If I’ve acquired your goal right, then please learn on.

discover appears an affordable alternative for this goal. I feel Andy’s reply provides affordable solutions that can simplify your script. I am not very well-placed to deal with this side as I’ve not used Apple’s model of discover for a few years (I exploit MacPorts’ GNU model of discover). However I would recommend one thing alongside this line (utilizing -type f, and -mindepth 1 collectively is probably overcautious):

discover /residence/web2py/web2py_apps/snapshots/ -mindepth 1 -type f -mtime +25 -delete

I am ignoring your use of /residence, which AFAIK would not exist in mac OS.

I may additionally suggest mving the information to a folder in /tmp – as a substitute of -delete (i.e. -execdir mv f /tmp/X). This is able to provide you with an opportunity to rethink these deletions earlier than making them everlasting. In any case, I would substitute -delete with -print to confirm issues have been working appropriately in all circumstances.

And at last, I would suggest you learn this (good & affordable) rationalization on how mtime is interpreted. I consider this rationalization applies to the GNU model of discover, however I have no idea if it additionally applies to Apple’s model (i.e. /usr/bin/discover).



ADDENDUM: Methods to Modify /and many others/paths

Open the file /and many others/paths in your favourite editor (aka /personal/and many others/paths). I assume nano is your editor right here, however substitute no matter you employ rather than that. Additionally Word: In the event you shouldn’t have sudo arrange, you are able to do that by coming into the command sudo visudo in Terminal. When you’re in a position to make use of sudo:

sudo nano /and many others/paths
#
# add one line to the file:

/usr/native/bin

# save & exit the editor

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles