Automate a bash script - linux

Im trying to install Anaconda on newly created EC2 instance using a bash script. While installation, it first asks to press enter then read through agreement(which I can skip by pressing q), then type yes to accept it. Once it's done, I have to type yes again to add PATH to .bashrc. I am trying to automate this but I'm not sure how it should be done. I tried to use Yes command, but it didn't work. I found out I can use expect command but for this I have to install its package first and It also asks to press some key(which I cannot automate) so I have to find some other way. It would be great if someone can provide some solution.

As #Dusan Bajic suggested, I installed Anaconda silently, which doesn't ask for any user input. I simply executed the following commands:
wget https://repo.anaconda.com/archive/Anaconda2-5.1.0-Linux-x86_64.sh -O anaconda.sh
bash anaconda.sh -b
where b is: Batch mode with no PATH modifications to ~/.bashrc. Assumes that you agree to the license agreement. Does not edit the .bashrc or .bash_profile files.
And then I added the PATH manually to .bashrc.
echo 'export PATH=/home/ec2-user/anaconda2/bin:$PATH' >> ~/.bashrc

Related

.Deb package postinst file not appending data to ~/.bashrc

I have this very simple postinst file for a .Deb package:
#!/bin/sh
echo 'alias command_pandora="sudo /usr/local/bin/pandora"' >> ~/.bashrc
echo 'Pandora Storage Server Installation complete.'
When I run it I even get the 'Pandora Storage Server Installation complete.' message, but nothing is appended to ~/.bashrc; nevertheless when I run this command alone in the terminal:
echo 'alias command_pandora="sudo /usr/local/bin/pandora"' >> ~/.bashrc
It does work. I already tried modifying the file permisions for ~/.bashrc but still get the same result. I also tried running a separate script with the same content and running it and it also works, so it seems to be related to dpkg.
Why is the content not being appended?
The postinst script runs as root. Package installation is a system installation utility; it should absolutely not modify users' private files, including those of root.
Tangentially, defining an alias seems like the wrong solution to your problem. Generally, prefer functions or shell scripts over aliases.
If the tool requires privileged access through sudo, perhaps refactor it to run itself with sudo (maybe with a check to only do this when it is connected to a terminal, to prevent it from hanging when run unattended).
Or, simply, include /usr/bin/command_pandora in the package with the following contents:
#!/bin/sh
exec sudo /usr/local/bin/pandora "$#"
(Marginally, I suppose it could add something to /etc/skel/.bashrc but this will only create a new alias for users which are created after this change, or users whose .bashrc presciently run source /etc/skel/.bashrc. I don't think that's a good idea, either.)

Installing GHCi requires to change my path to use in terminal

[![I'm trying to write Haskell code in a text editor then run it with GHCi in my terminal. I successfully installed GHCi (I think), and am trying to run a command to run some code I wrote, but I there is no command found for ghci or ghc. Do I need to change my terminal path to where I downloaded/installed GHCI? Also if I change the path will it permanently change my starting path in terminal? Here's the last thing my terminal says. I've also tried stack ghci.
EDIT
My error is that the commands are not found when I run them in terminal.
I installed everything by running this command in my terminal and continuing to type YES when prompted.
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
The contents of /Users/Jack/.ghcup/env is
export PATH="$HOME/.cabal/bin:/Users/Jack/.ghcup/bin:$PATH"
Note to others reading this: I recommended ~/.profile instead of ~/.bashrc because I know from the original question (before it was revised) that the OP is using a Mac (more info on bash on Macs).
Try running these three commands in order in your terminal:
echo 'export PATH="$HOME/.cabal/bin:/Users/Jack/.ghcup/bin:$PATH"' >> ~/.profile
. ~/.profile
ghci
What this will do is modify your PATH environment variable as necessary so that your shell can find the ghci program. It will also store this modification in your ~/.profile so your PATH will have the correct value next time you log in.
Also if I change the path will it permanently change my starting path in terminal?
The PATH environment variable is not related to the initial working directory of your terminal. So no, your terminal's "starting path" will not be changed.
Just write . ~/.ghcup/env in the ~/.zshrc file. Another thing is restarting the terminal.

How do I make a command in linux using a bash script?

I am trying to make a command for the terminal. I have a bash script prepared (la.sh) and I want to just be able to type la to run it. How am I able to get the code so that I can just type la?
I have tried putting it in the /bin folder however had no luck.
What can I do to fix this?
I am using the latest version of Manjaro Gnome.
Thanks a lot!!!
BTW, the script was literally just ls.
It was just a practice script.
Lets consider that your script is stored under /some/path/la.sh. In my opinion, you have several solutions to accomplish your goal:
Option 1:
Add the script to your user's path so you can directly call it.
echo "export PATH=$PATH:/some/path/" >> ~/.bashrc
Then you will be able to use in your terminal:
$ la.sh
Using this option you can call la.sh with any parameters if needed. If the requirement is to call simply la you can also rename the script or create a softlink:
mv /some/path/la.sh /some/path/la
or
ln -s /some/path/la.sh /some/path/la
Option 2:
Create an alias for the script.
echo "alias la='/some/path/la.sh'" >> ~.bashrc
Then you will be able to use in your terminal:
$ la
However, using this option you will not be able to pass arguments to your script (executing something similar to la param1 param2) unless you define a more complex alias (an alias using a function in the .bashrc, but I think this is out of the scope of the question).
IMPORTANT NOTE: Remember to reload the environment in your terminal (source .bashrc) or to close and open again the terminal EVERY TIME you make modifications to the .bashrc file. Otherwise, you will not be able to see any changes.
The file la.sh must be placed in your path. Then you can create an alias for it.
alias la="la.sh"
If you want to have a command be available under two different names (la.sh and la in your case), I recommend against using an alias: An alias defined in your .bashrc is only available in an interactive bash; If you run, say, a non-bash interactive shell, or writing a bash script, you can't use it.
The IMO most general way is to create a link. Since you said that you have already placed la.sh into bin, you can create the link in the same directory, i.e.
ln /bin/la /bin/la.sh # creates a hard link
or
ln -s /bin/la /bin/la.sh # creates a symbolic link
In your case, either one is fine. If you want to find out more about the differences between hard and symbolic link, look for instance here.
It worked with a mixture of everybody's answers.
All I had to do was go into the directory that la.sh was in.
Rename it to just la as a text file.
Run chmod 777 la to turn it into executable to anybody.
Add it to my path by using the command export PATH=$PATH:~/Directory/It/Was/In/
Thank you to all who contributed.

Setting path variables and running Ruby script

This is my first time working with a Ruby script, and, in order to run this script, I have to first cd into the root of the project, which is /usr/local/bin/youtube-multiple-dl and then execute the script as bin/youtube-multiple-dl.
I tried setting the PATH variable
echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile
in hopes that I can run this from anywhere on the machine without having to cd to the project's root, however, no luck with that so far.
System: Ubuntu 15.04 server
Script Repo
My current way of executing the script is:
root#box15990:~# cd /usr/local/bin/youtube-multiple-dl
root#box15990:/usr/local/bin/youtube-multiple-dl# bin/youtube-multiple-dl
Desired way of executing script:
root#box15990:~# youtube-multiple-dl
How can I properly set the enviroment path for this script in order to run from anywhere?
echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile
isn't how we set a PATH entry.
The PATH is a list of directories to be searched, not a list of files.
Typically, the PATH should contain something like:
/usr/local/bin:/usr/bin
somewhere in it.
If it doesn't, then you want to modify it using a text editor, such as nano, pico or vim using one of these commands:
nano ~/.bash_profile
pico ~/.bash_profile
vim ~/.bash_profile
You probably want one of the first two over vim as vim, while being extremely powerful and one of the most-used editors in the world, is also not overly intuitive if you're not used to it. You can use man nano or man pico to learn about the other too.
Once your in your file editor, scroll to the bottom and remove the line you added. Then find the /usr/bin section in your PATH and add /usr/local/bin: before it. : is the delimiter between directories. That change will tell the shell to look in /usr/local/bin before /usr/bin, so that any things you added to the /usr/local/bin directory will be found before the system-installed code, which is in /usr/bin.
It's possible that there isn't a PATH statement in the file. If you don't see one, simply add:
export PATH=/usr/local/bin:$PATH
After modifying your ~/.bash_profile, save the file and exit the editor, and then restart your shell. You can do that by exiting and re-opening a terminal window, or by running:
exec $SHELL
at the command-line.
At that point, running:
echo $PATH
should reflect the change to your path.
To confirm that the change is in effect, you can run:
which youtube-multiple.dl
and you should get back:
/usr/local/bin/youtube-multiple.dl
At that point you should be able to run:
youtube-multiple.dl -h
and get back a response showing the built-in help. This is because the shell will search the path, starting with the first defined directory, and continue until it exhausts the list, and will execute the first file matching that name.
Because of the difficulties you're having, I'd strongly recommend reading some tutorials about managing a *nix system. It's not overly hard to learn the basics, and having an understanding of how the shell finds files and executes them is essential for anyone programming a scripting language like Ruby, Python, Perl, etc. We're using the OS constantly, installing files for system and user's use, and doing so correctly and safely is very important for the security and stability of the machine.

Where to place zsh autocompletion script on Linux?

After installing cheat (command cheat sheets from the command line), I tried to enable the autocompletion using the provided zsh script. However, I don't seem to find the correct location for the script.
So far
I fetch the cheat.zsh ;
copy it to ~/.oh-my-zsh/custom/plugins/cheat/_cheat.zsh ;
add the cheat to the plugins array in my ~/.zshrc ;
reload my shell.
Auto-completion doesn't happen when typing cheat d<TAB>.
Question
So where to place zsh auto-completion script on Linux?
I got this to work by adding cheat.zsh to the ~/.oh-my-zsh/plugins directory. Zsh checks to autoload functions on FPATH, so try:
echo $FPATH
and then either add to FPATH or move the file into a folder on the path.
This actually does a much better job of explaining it:
https://unix.stackexchange.com/questions/33255/how-to-define-and-load-your-own-shell-function-in-zsh
Let me try to help here.
I was trying something similar and this is how I was able to get it worked.
Below solution has verified with oh-my-zsh on debian distro [ubuntu]
Problem
> Your zsh isnt giving proper completion suggestions say [conda]
> This is what you get when you type in # conda tab
Solution
Find the completion script
one great location is https://github.com/clarketm/zsh-completions/tree/master/src
Download the file to completions folder [~/.oh-my-zsh/completions]
wget https://raw.githubusercontent.com/clarketm/zsh-completions/master/src/_conda ~/.oh-my-zsh/completions
Make sure the completions folder is listed under $fpath
print -l $fpath
What if its not listed
It should have normaly added with .oh-my-zsh.sh
If not append below to ~/.oh-my-zsh/oh-my-zsh.sh
# add a function path
fpath=($ZSH/functions $ZSH/completions $fpath)
source .zshrc
source ~/.zshrc
Execute compinit this will build ~/.zcompdump file for the functions
compinit
Troubleshooting
Due to conflicts the suggestions might not be shown try the following
rm -f ~/.zcompdump; compinit
# we are clearing the function dump stored by zsh, its safe zsh will rebuilt it.
Try source .zshrc
source ~/.zshrc
Try loggin out and login
Check the mapping in ~/.zcompdump
vi ~/.zcompdump
search for conda
[/conda]
you should see as below
'conda' '_conda'
Hope someone will find it useful, if so Happy to Help

Resources