Shell shortcuts to folders in Windows - windows

I'm learning Vagrant and Virtualbox, Now to add a line to my hosts file in a (windows)
echo "test" >> c:/windows/system32/drivers/etc/hosts
But i'm wondering if i can make a Shortcut to c:/windows/system32/drivers/etc/hosts that i can use in any shell from everywhere.

You might create an environment variable:
[System.Environment]::SetEnvironmentVariable('Youralias','c:/windows/system32/drivers/etc/hosts','Machine)
and then access it using
$env:YourAlias
from PowerShell... If you want to access it from CMD
%Youralias%
should work.

Easier solution using a .bash_profile file
In your home folder (windows) you can make a .bash_profile file and specify aliases.
Go to your home folder
cd ~
make a .bash_profile file and open it in vim or any other editor
touch .bash_profile && vim .bash_profile
add a new alias by adding the following line
alias testfolder="cd /c/testfolder"
save and close your editor.
Explanation someone?
For some reason it only works after i run the following command
source .bash_profile

Related

cd commands not working from .bash_profile on MobaXTerm

I'm on Windows 10, using MobaXterm. I have a persistent home directory, and I've created .bash_profile there. My .bash_profile contains:
echo "Loading bash profile."
cd "/drives"
When I open a new MobaXterm tab, I get:
Loading bash profile
: No such file or directory
But when I run:
cd "/drives"
from the command line, it changes directory to /drives.
I don't really need to go to /drives when a tab starts up, what I want to do is create alias for cd commands that go to long directory names without having to type them out every time, but no cd commands will work from .bash_profile, I always get 'No such file or directory'. Any ideas?
I contacted MobaXTerm support, and their reply was to convert the .bash_profile to unix format with this command:
dos2unix /home/mobaxterm/.bash_profile
Now the cd commands work as expected.

How to set up packer PATH on Mac Unix

The documentation of packer says: Packer Setup Documentation
on how to set the PATH : How to permanently set PATH in Unix
And I did add :
export PATH=$PATH:~/packer/
in my ~/.zshrc file
however, when I go to type packer on the terminal first time, the dir changes to be ~/packer and when I type packer again I get :
~ packer
➜ packer packer
zsh: command not found: packer
Does anyone have a better clue on how to set up packer's PATH on a Macintosh Unix system?
In OS X you would typically have any exports set in:
~/.bash_profile
If you already have a .bash_profile setup then it will override .profile. Since you're using zsh you might try putting the export in ~/.zprofile.
Please try adding your edits to the ~/.profile file instead of ~/.bashrc file.
To make the new path stick permanently you need to create a .bash_profile file in your home directory and set the path there.
Open terminal on Mac and write nano .bash_profile.
Create the .bash_profile file with a command line editor called nano,If it is already exists then it will open a text editor containing path.
Add the path you require like:
export PATH="/usr/local/node/bin:$PATH"
Save the file in nano by clicking Ctrl+O and confirming the name of the file as .bash_profile by hitting enter. And the Ctrl+X to exit nano.
Then for checking path is set or not just write echo $PATH in terminal. Your path would be there in it.

Local .bashrc is not read on startup under MobaXterm Home on Windows

I am using MobaXTerm (Home version 3.0). I have create a direcorty C:\MobaXterm_3.0\home and set in the local configuration (Settings --> Configuration) the home directory to this path. Under this path I have created a .bashrc file with read access for everybody.
However when starting up MobaXterm, the .bashrc file does not get executed. How can I fix this? The MobaXterm docu says that in the Home addition I cannot change the /etc/profile, but the standard /etc/profile does execute ~/.bashrc. So I am a bit at a loss.
ps: I am aware of the answer to "How does one define aliases for use within MobaXTerm local bash shell?" and have created the .bashrc file with the right permissions.
When you launch a local terminal in MobaXterm first time .bash_profile is executed which sets up the required configuration for the initial shell command prompt to work. The idea is to source the .bashrc from the .bash_profile.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Add the above lines to your .bash_profile.
Source: http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

.bashrc: Permission denied

I try to work with a project in vagrant.
I have made the command vagrant ssh, and connected to VM. Now I need to edit .bashrc file to set path to the source code. But first I couldn't find that file. So I googled and find that the way is call command ~/.bashrc. But doing this I get message, that I have no access to it:
[vagrant#nupic-vagrant:~]$ ~/.bashrc
-bash: /home/vagrant/.bashrc: Permission denied
So what to do now?
UPD.
I can't find the .bashrc file.
When I try to make command ls -a I get following:
[vagrant#nupic-vagrant:~]$ ls -a
. .bash_logout cleanup.sh sshd.sh .veewee_params
.. .bash_profile minimize.sh vagrant.sh .veewee_version
.bash_history .bashrc .ssh .vbox_version .zsh_profile
[vagrant#nupic-vagrant:~]$ locate .bashrc
/etc/skel/.bashrc
/home/vagrant/.bashrc
/var/chef/backup/etc/skel/.bashrc.chef-20130614181911
/var/chef/backup/home/vagrant/.bashrc.chef-20130614181912
[vagrant#nupic-vagrant:~]$
But only the place where I can find some of those files is the directory where cygwin is installed. Pls, see illustrations, they reflect relations between directories vagrant and cygwin.
.bashrc is not meant to be executed but sourced. Try this instead:
. ~/.bashrc
or, equivalently
source ~/.bashrc
See the reference about the . (aka source) builtin.
Note that if what you're looking for is to restart your Bash session after modifying your ~/.bashrc file, you might as well use:
exec bash
That will replace your current Bash session (thanks to exec) by a new session.
If you want to edit that file (or any file in generally), you can't edit it simply writing its name in terminal. You must to use a command to a text editor to do this. For example:
nano ~/.bashrc
or
gedit ~/.bashrc
And in general, for any type of file:
xdg-open ~/.bashrc
Writing only ~/.bashrc in terminal, this will try to execute that file, but .bashrc file is not meant to be an executable file. If you want to execute the code inside of it, you can source it like follow:
source ~/.bashrc
or simple:
. ~/.bashrc
If you can't access the file and your os is any linux distro or mac os x then either of these commands should work:
sudo nano .bashrc
chmod 777 .bashrc
it is worthless
The .bashrc file is in your user home directory (~/.bashrc or ~vagrant/.bashrc both resolve to the same path), inside the VM's filesystem. This file is invisible on the host machine, so you can't use any Windows editors to edit it directly.
You have two simple choices:
Learn how to use a console-based text editor. My favourite is vi (or vim), which takes 15 minutes to learn the basics and is much quicker for simple edits than anything else.
vi .bashrc
Copy .bashrc out to /vagrant (which is a shared directory) and edit it using your Windows editors. Make sure not to save it back with any extensions.
cp .bashrc /vagrant
... edit using your host machine ...
cp /vagrant/.bashrc .
I'd recommend getting to know the command-line based editors. Once you're working inside the VM, it's best to stay there as otherwise you might just get confused.
You (the vagrant user) are the owner of your home .bashrc so you do have permissions to edit it.
Once edited, you can execute it by typing
source .bashrc
I prefer to logout and in again (there may be more than one file executed on login).
Please find the step to fix bash restricted error on Linux servers.
If you are getting below restricted message while try to login to the server by using your credentials , then it might be an issue with lack of directory permissions in the server.
Because of this permission issue we were unable to navigate to required directories and getting error “bash: cd: restricted”
Fix : To release bash restriction error use the highlighted command in Linux server -bash -f
As same if wants to restrict the permission use the highlighted command - bash -r
Once you executed the bash -f command the restrictions will be released from the directories and we can able to proceed with patch scripts.

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