-8.1 C
New York
Monday, December 23, 2024

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 you might need to think about:

There are two points to your query: 1) is the PATH side wrt the situation 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 major side is re your PATH; i.e. the variable that defines the place the system appears to be like to seek out executable instructions you enter.

Let’s deal with 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 seek out the “command” you enter. It seems your script is positioned in a folder that’s not included in your PATH. This might clarify your error message:

“bash: snapshot_remove: command not discovered”

You will have 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 Choice 1: Transfer your script :

Open a Terminal, and enter echo $PATH on the immediate to see what it incorporates. Hopefully, it’s going to comprise the folder usr/native/bin. If it doesn’t, there could possibly be any variety of the explanation why (Apple might have been inconsistent??), however the difficulty is definitely remedied as proven under within the Addendum: How you can 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 well-liked location for locally-sourced executable recordsdata – reminiscent of your snapshot_remove script. This location being IAW the “Filesystem Hierarchy Commonplace”. Accordingly, you would place your script in that folder, and overlook the entire remainder of the enterprise related to updating your PATH setting variable.

Choice 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 may add that folder to your PATH variable as a Momentary change, or you possibly can add it as a Everlasting change:

Momentary 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 in case you use bash):

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

export PATH=/Customers/whoever:$PATH

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

Now let’s deal with the “script syntax” side:

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

discover appears an inexpensive alternative for this goal. I feel Andy’s reply gives affordable recommendations that may 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’d counsel one thing alongside this line (utilizing -type f, and -mindepth 1 collectively is maybe 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 advocate mving the recordsdata to a folder in /tmp – as an alternative of -delete (i.e. -execdir mv f /tmp/X). This might 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 had been working accurately in all instances.

And at last, I would advocate you learn this (good & affordable) clarification on how mtime is interpreted. I imagine this clarification 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: How you can Modify /and many others/paths

Open the file /and many others/paths in your favourite editor (aka /non-public/and many others/paths). I assume nano is your editor right here, however substitute no matter you employ instead of that. Additionally Observe: In case you do not need sudo arrange, you are able to do that by coming into the command sudo visudo in Terminal. When you’re ready 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