Add a single Bash command - bash

I do not have su access and I have a perl executable in ~/et directory which is called exiftool.
I need to add that executable to bash commands (so that I can type exiftool instead of ~/et/exiftool).
The problem is that ~/et contains other files that are not executable (so I cannot use export PATH=$PATH:$HOME/et). Is there any alternative?

You could use an alias:
alias exiftool=~/et/exiftool
Or you can symlink it elsewhere and add that directory to your path:
mkdir -p ~/bin
ln -s ~/et/exiftool ~/bin
PATH=$HOME/bin:$PATH

I don't understand why having files that are not executable in the directory prevents you from adding the directory to your PATH anyway?
As an alternative, though, you can use an alias.
alias exiftool=$HOME/et/exiftool
You can place this in your .bashrc to have it always available.

softlink to a folder in PATH
alias exiftool='~/et/exiftool'

Related

Change directory in bash script in windows

How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.

How can I use my C program's binary file after as a filter?

I have written a program that reads in input from a file or stdin, sorts in and writes it to an output file or stdout. I want to be able to use the binary like all of the filter-like programs (grep, tr, sed).
For example: I want to be able to type in something like, "cat myfile.txt | myfilter --action sort > myoutputfile" instead of cat myfile.txt | ./myfilter --action sort > myoutputfile.
It looks like you're using some kind of *nix system. So what you need to do is to put the binary in a directory that exists in your PATH environment variable. On my system it looks like this:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
So either put the binary in one of those, or add an extra directory to PATH
Assuming a Linux system (might work on BSD or Mac too) you edit the file ~/.bashrc and add the line export PATH="<your/custom/path>:$PATH". Note that editing .bashrc this file will only do this for the current user.
If you want the particular executable available for all users, then instead of creating a bin directory in your home directory, add it in /usr. For example mkdir /usr/mybin and edit /etc/profile instead of ~/.bashrc. My file has these lines:
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH
Add the line PATH="/usr/mybin:$PATH" so you get:
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
PATH="/usr/mybin:$PATH
export PATH
With this method you will make /usr/mybin appear in all users paths. Make sure that the directory and executable has correct permissions. Use the command chmod 755 /usr/mybin and chmod 755 /usr/mybin/filter. An alternative is to put the file in a directory that is already in the path, like /usr/bin. But you still need to make sure it has 755 permissions.
From the sound of it, you're on a linux machine, so I will use that for my answer
First, you will want make a new folder:
$ mkdir ~/bin
After that, you will need to add the new folder to your path:
$ nano ~/.bashrc
# Add the following to the end of your ~/.bashrc file while using nano
# or your text editor of choice:
export PATH="/home/$USER/bin:$PATH"
The next part can differ depending on how you want to do it. Since its your own personal binary, it might just be easier to add the binary into the folder. Otherwise, you could create a symlink to the file.
To create a symlink:
$ ln -sf ~/path/to/filter ~/bin/filter
To move the file into the bin folder
$ mv ~/path/to/filter ~/bin
Finally, after you've chosen the method (and the executable is somehow accessible from the ~/bin/ folder, run the following command:
Please note, source is only to refresh your current terminal session, you should not have to do this again
$ source ~/.bashrc

export, bash command not found

I have used export before but I don't know why when I set the variable PATH to any directory this time, ls, awk commands are not found but no problem with pwd, cd
export PATH="/Users/carolW/Desktop"
ls
-sh: ls: command not found
Use:
export PATH=/Users/carolW/Desktop:$PATH
You're removing all the normal directories from your path, so it only looks in your Desktop folder for everything. You just want to add your directory, not replace the entire path with it.
most probably because pwd and cd are built shell command
(you can test:
which pwd
which ls
)
However, ls are such are tools you can find in /bin directory or such, and those paths are defined in your variable PATH.
If you clear the variable PATH, most likely you won't find your tool anymore.
You may use export PATH=$PATH:"/Users/carolW/Desktop"
So that you concatenate your path to the already existing paths

Switching to an aliased directory

Just wanted to know if it is possible to switch to an aliased directory using shell script.
To switch to a directory there is cd command. But i have aliased the directory and wanted to know as how to switch to the aliased directory.
Could someone please help me?
An alias is meant to be a shortcut for a command.
You can't cd to an aliased directory.
But if you have a variable referencing that directory, you can. e.g.
dev="/path/to/my/dev"
cd $dev
Or set up an alias to cd to the directory:
alias dev='cd /path/to/my/dev'

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Resources