I'm trying to create a script to check if Homebrew is installed on any given mac and in case it is if it has a particular formula installed. I have the part that checks if brew is installed, but when I try to run brew list to see what packages are installed I get "Command not found" even though I can run the command in the Terminal fine. I'm using:
do shell script "brew list"
Is there any other way to run brew commands in Applescript?
It's best to call the command directly, otherwise AppleScript might not find the correct path returning command not found — To do that you'll want to see where the brew command is located in Terminal:
$ which brew
/usr/local/bin/brew
Based off of this you should be able to do:
do shell script "/usr/local/bin/brew list"
If you have multiple arguments after a command use the -c option:
do shell script "/usr/local/bin/brew -c list <package>"
If the -c option is present, then commands are read from
string. If there are arguments after the string, they are
assigned to the positional parameters, starting with $0.
If you wanted a complete way to figure out the path you might be able to do something such as:
set brewPath to do shell script "/usr/bin/which brew | awk '{print $0}'" as string
set brewList to do shell script "" & brewPath & " list" as string
*note: I haven't tested this, so it might require some adjustment.
Related
I'm both new here and with MacOS and coding.
I know in windows I can create bash file to have an executable command in cmd. I've researched and discovered I can use similar file with MacOS (shell files) but I'm struggling to understand how to make one. I've tried different route:
As first thing I've tried creating a file in txt editor with this code:
#!/bin/bash
pip3 install pgzero
echo Installing Pygame Zero
Using later 'chmod 700 Filename' in terminal. It did not worked
I then tried with Apple Scrip, with a code like:
tell application "Terminal"
activate
do script "pip3 install pgzero"
do script "echo Installing Pygame Zero"
end tell
and it kinda worked, but it wasn't an executable
Then I tried with another approach found on google:
echo '#!/bin/bash
pip3 install pgzero
echo Installing PyGame Zero'> ~/Desktop/PygameInstaller.command
chmod 740> ~/Desktop/PygameInstaller.command
and it still didn't worked D:
Can someone land a bit of help? I'm starting feeling lost q,q
Thank you in advance!
In terms of what you want in your shell script, your first attempt is probably close to what you want but the echo should precede the pip3 command. I guess if you change the verb from Installing to Installed, then you could leave it where it is.
$ cat <<EOF > ~/Desktop/PygameInstaller.command
#!/bin/bash
echo Installing Pygame Zero
pip3 install pgzero
EOF
$ chmod 700 ~/Desktop/PygameInstaller.command
The permissions you assign to the script and the script's location depend on who you want to grant execution. If it's only you, then your desktop and 700 should be fine.
Now, if you want to execute the script from a command line like what you would see if you opened an instance of Terminal.app, then you have options.
If you want to fully specify the command, then you would type this (showing prompt which you would not type):
$ ~/Desktop/PygameInstaller.command
If you want to specify only the name of the script, then you would type this after adding ~/Desktop to your PATH:
$ PATH="$HOME/Desktop:$PATH"
$ PygameInstaller.command
If you prefer to type only PygameInstaller, then don't put the code in a file named PygameInstaller.command. Instead, you put the code in a file called simply PygameInstaller.
If you need the script to be executable by everyone, then put it in /usr/local/bin because most people will either have that in their PATH or have no political problem doing so. But you'll have to use the sudo command to elevate your privileges to accomplish that task.
If, however, you want to have that script be treated like any other app that you can launch with a double-click, then you have significantly more work to do.
I'm trying to create an alias for an application for easy access rather than going to the directory and running it.
alias cpanel-run='"$(cd /home/ian/projects/electron/cpanel-linux-x64/)" "$(cpanel)"'
but it only displays
bash: ./cpanel: No such file or directory
Command '' not found, but can be installed with:
sudo apt install bpfcc-tools # version 0.8.0-4, or
sudo apt install mailutils-mh # version 1:3.5-2build1
sudo apt install mmh # version 0.4-2
sudo apt install nmh # version 1.7.1-4
Do you actually need to be in the same directory as the executable? If not, just do this:
alias cpanel-run='/home/ian/projects/electron/cpanel-linux-x64/cpanel'
If you do need to be in the same directory, use this instead:
alias cpanel-run='cd /home/ian/projects/electron/cpanel-linux-x64/ && ./cpanel'
(The && tells the shell to run the first (cd) command, and then run the second only if that succeeds.)
The reason your original version didn't work is that you're using $() inappropriately. What $() does is run its contents as a subprocess collect the output, and use that as part of the command line. So, your version runs the cd command, which successfully changes to the directory, but since it runs as a subprocess it has no effect on your shell or any other process. It also produces no output. Then the other $() tries to run cpanel (is it actually ./cpanel?) in a different subprocess, fails because it's not there (producing the first error message), and also produces no output. Then, based on the (empty) output from those two subprocesses, the shell tries to run the command "" "", which fails because the empty string is not a valid command.
Please refer below, I used to like this, and if you need to run open a terminal and just type kibana or elasticsearch whatever the alias name.
Please note you have to put these lines bottom of the .bashrc file
alias kibana='cd /home/bhanuka/Apps/ELK/kibana-7.5.2-linux-x86_64/bin/ && ./kibana'
alias elasticsearch='cd /home/bhanuka/Apps/ELK/elasticsearch-7.5.2-linux-x86_64/elasticsearch-7.5.2/bin/ && ./elasticsearch'
alias logstash='cd /home/bhanuka/Apps/ELK/logstash-7.5.2/bin/ && ./logstash'
alias filebeat='cd /home/bhanuka/Apps/ELK/filebeat-7.5.2-linux-x86_64/ && ./filebeat -e'
I was trying to set up a platform that used pip and I realized I had to install it using 'sudo easy_install'. I think easy_install is a python-enabled command that was set up in terminal after I installed Python.
How does it work that after you install something like Python, a command like easy_install is automatically activated with-in terminal that can call what easy_install does? Is there some sort of active list of commands that is updated for all terminal executions by programs that are installed?
Thanks.
All the places that the terminal will search for programs are in the PATH variable. To see which directories are in your path variable you can run the following in your terminal: echo $PATH. Note that paths are colon separated on most UNIX-based systems. As such, all programs contained in directories that are in the PATH variable are ones that can be run without specifying a relative/absolute path to them.
I've created a ruby script that sets up a new mac.
Among other things it creates a .bash_profile, .gitconfig and configures various system settings such as displaying the full POSIX path as the Finder window title (super useful).
Mostly I'm running commands in backticks such as `defaults write com.apple.finder _FXShowPosixPathInTitle -bool true` the aforementioned full POSIX path as the Finder window title trick.
All this works just fine.
What I want to do is have this ruby script run the Homebrew installer too. The bash command for this is :
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
However this doesn't work when called using backticks.
So my question is how do I run another ruby script (which the Homebrew installer is) from within a ruby script?
And more specifically how would I kick off the web based interactive Homebrew installer (well you have to press return at least once) from within a ruby script and for it's output to show in the terminal?
I know that I could rewrite this all as bash script but I'd really rather keep it all within ruby.
Let's decompose what $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" actually does:
download, via curl, the homebrew install ruby file. Since the command is surrounded by $(), it executes the command and passes the output to ruby.
execute the script via Ruby. The -e flag instructs Ruby to execute the script from the command line instead of loading a specified file.
Since we know that it's a ruby script, we can just do the following:
using Net::HTTP or some other ruby library, download, the homebrew install file.
eval() or otherwise execute the homebrew ruby script.
Of course, eval() is dangerous, especially with untrusted input, but you're already essentially running eval on the script anyways with the install command provided.
In script form that would be:
require 'net/http'
homebrew_uri = URI('https://raw.githubusercontent.com/Homebrew/install/master/install')
homebrew_script = Net::HTTP.get(homebrew_uri)
eval(homebrew_script)
I have this program in ruby. I won't explain the whole process, but it gave me in the end a string.
I'd like to use this string in my shell. For now, I can generate it with ruby mysoft.rb
I'd like to use the result's string in a command, for exemple, when I commit with git, with something like this
git commit -m "$generated_string"
I would that the file was install on the computer and be usable by him. With a single command, he could have the generated string, from any directory, like a "normal" command like "ls" for exemple.
I have no idea how to do that ? Should I do a Gem ? Or something else. I'm new in Ruby, so, pretty confused.
Thanks a lot.
You are looking for shell command substitution; the syntax depends on which shell you are using. For example, if using bash or csh:
$ git commit -m `ruby mysoft.rb`
The following syntax does the same thing but in bash only:
$ git commit -m $(ruby mysoft.rb)
Change the first line of your program to this:
#!/usr/bin/env ruby
This line tells the shell that ruby should be used to execute this script by default.
Before you can run the script however you need to add the executable bit to the file:
> chmod gou+x mysoft.rb
Now you can type on the command line directly:
> ./mysoft.rb
And ruby will run your program.
If you want to make the command globally available on your machine, for example with the name mysoft, then you need to do this:
> sudo cp mysoft.rb /usr/bin/mysoft
This will install the program in the bin directory of the system. After this whenever you type mysoft anywhere on the machine, your program will run.