How to use .bashrc properly and run application? - bash

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'

Related

Creating executable file with pip install command on MacOS

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.

How can I call brew from an Applescript

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.

How do I use backticks to change directories?

I tried to change directories by using backticks in Ruby. I am on Ubuntu 12.04 box and this is the error I get:
irb(main):003:0> `cd /`
(irb):3: command not found: cd /
=> ""
I run the command directly via the terminal and I change directories just fine. It also works fine on my local Mac box, so I'm not sure what is going on.
When you run shell commands using the tick marks, they will be run in a child process that can not change the working directory of the parent.
If you link the cd command with another command, you will see it work, but only effecting the child process:
`cd / && ls`
To change the working directory of the parent process use the Ruby command Dir.chdir([string]):
Dir.chdir("/")
Relevant information can be found in this post:
"exec the cd command in a ruby script"

Error message: -bash: /etc/profile.d/rvm.sh: No such file or directory

After installing Ruby successfully on my mac, every time I start the terminal, I get this message:
Last login: Sun May 19 00:47:06 on ttys000
-bash: /etc/profile.d/rvm.sh: No such file or directory
What does it mean? Do I need to fix this? if so how?
Every time you start Terminal or a new tab in Terminal, you begin a bash shell session. You can set some commands to run every time you start a bash session. Typical use cases include setting some aliases (e.g. gst for git status), environment variables, and running other bash scripts. The place to put these initial bash commands is usually in ~/.bashrc or ~/.bash_profile. Start by checking those files to see if you have any reference to the file /etc/profile.d/rvm.sh. Also, check whether said file actually exists on your machine. If it doesn't, you can probably safely delete the corresponding line from your bashrc or bash_profile. If it does exist, there's something else funny going on which you'll have to debug.
Also note, the way RVM works, it requires you to run a certain script every time. If you have RVM and plan on using it, does it work? If not, you might need to find where the appropriate RVM script lives on your machine and add something to your bashrc or bash_profile that runs the script. Normally when you install RVM it tells you what command to add and where. For example by ~/.bashrc file includes the following command:
[[ -s "/Users/amitgupta/.rvm/scripts/rvm" ]] && source "/Users/amitgupta/.rvm/scripts/rvm"
which checks that the rvm script exists, is a non-empty file, and then runs it.

Using ruby in UNIX shell

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.

Resources