Search and replace a specific line with bash - bash

I need add new plugins in my zshrc file using bash script, to do that I search the line which contains plugins = (sometext)
syntax
plugin_text=$(grep "^[^#;]" zshrc | grep -n "plugins=(.*)")
running directly in terminal I get:
$ grep "^[^;]" zshrc | grep -n "plugins=(.*)"
38:# Example format: plugins=(rails git textmate ruby lighthouse)
40:plugins=(git python pip)
40 is the correct line but when I execute my bash script I get:
$ ./config-minimal
3:plugins=(git python pip)
I need change 40 line inserting new plugins. Example:
before
plugins=(git python pip)
after
plugins=(git python pip zsh-autosuggestions zsh-syntax-highlighting)
How can I get this line and replace text with a easy way?
My script
function install_zsh {
# aptitude install zsh
# sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
# Install zsh highlighting
# cd ~/.oh-my-zsh/custom
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# Install zsh auto suggestions
# git clone git://github.com/zsh-users/zsh-autosuggestions
# TODO: Add options in plugins
cd ~
plugin_text=$(grep "^[^#;]" .zshrc | grep -n "plugins=(.*)")
new_plugins=${plugin_text/)/ zsh-autosuggestions zsh-syntax-highlighting)}
line_number=${plugin_text/:plugins*/ }
sed "$(line_number)s/plugin_text/new_plugins/" .zshrc
}

You can handle it with a simple sed:
sed 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' .zshrc
Or (thx #123):
sed 's/\(^plugins=([^)]*\)/\1 zsh-autosuggestions zsh-syntax-highlighting/' .zshrc
Add -i flag to infile replacement.
function install_zsh {
# aptitude install zsh
# sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
# Install zsh highlighting
# cd ~/.oh-my-zsh/custom
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# Install zsh auto suggestions
# git clone git://github.com/zsh-users/zsh-autosuggestions
# TODO: Add options in plugins
sed -i.bak 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' ~/.zshrc
}

Related

How to get the second word of an output from a command in shell?

Hi I am trying to make a shell script.
sudo usermod -s $(whereis -b zsh) $(whoami)
$(whereis -b zsh) makes an error with zsh: command not found zsh:
The error seems to occur because the output of whereis -b zsh is zsh: /usr/bin/zsh /usr/lib/x86_64-linux-gnu/zsh /bin/zsh /etc/zsh /usr/share/zsh /home/linuxbrew/.linuxbrew/bin/zsh
Now I would like to use /usr/bin/zsh for the script as an output. Is there any way to get the second word from the output of whereis -b zsh?
how should the script look like to get what I need?
shell script is quite difficult than I thought. Thank you everyone in advance!
Better add quotes around commands expansion
sudo usermod -s "$(whereis zsh | cut -d ' ' -f2)" "$(whoami)"
Alternate method by getting zsh from the $PATH:
sudo usermod -s "$(command -v zsh)" "$(id -un)"
If you run it under bash:
Instead of parsing the output of whereis, use type:
sudo usermod -s "$(type -P zsh)" "$(whoami)"
Don't forget that type -P yields an empty string, if the program you are searching for is not in the PATH.
If it is not bash, you can also do a
sudo usermod -s "$(which zsh)" "$(whoami)"
Note that which issues an error message if the program can't be found, so if you need an empty output in this case you'll have to throw away stderr.
UPDATE: Thinking of it, IMO a better solution is the one suggested by Lea Gris: command -v is available on bash and POSIX shells, and yields empty output if the file can't be found.
You can do something like:
whereis -b zsh | awk '{print $2}'

Messed up terminal after installing Angular [altered my zsh $PATH?]

I just installed Angular and after the default installation (as set out here: https://angular.io/guide/setup-local). After running the standard commands my terminal would not recognise ng (so I could not use ng new my-app).
To solve this problem I did the following based on a stackoverflow answer:
echo -e "export PATH=$(npm prefix -g)/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
ng --version
(I had no knowledge of what PATH was when doing this, or what the command I was running did...)
This worked, and my ng --version returned a result and I was able to build and run an Angular app.
However when I opened a new tab in my terminal all my normal terminal 'styles' (I use zsh) had gone.
My friend and I tried to resolve the problem, which he felt might be related to my terminal settings (his 'Command (complete path)' read: /usr/local/bin/zsh, and mine was /bin/zsh). So we tried a few things but none have worked and it might be worse - this included resetting my $PATH based on another stackoverflow answer to /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin.
We also switched back and forth from /bin/zsh to /bin/bash to try and resolve things, but this did not do anything.
The current state of things is that I cannot run rails commands where I normally could (for example), also things like ll (which returns zsh: command not found: ll).
This is my .zsh_history file:
which zsh
which bash
cd code
cd
ls
lsa
ls -a
cat .aliases
vi .aliases
sudo v
sudo vi .aliases
npm install -g angular
npm list
npm install -g #angular/cli
cd code
ls
cd JoshInLisbon
cd ..
JoshInLisbon
ls
mkdir Angular
mkdir Rails
cd rails
ls
cd ..
open
open .
cd WALD
ls
ll
lll
ls -a
cat .git
.git
ls
ls -a
cd ..
..
JoshInLisbon
..
JoshInLisbon
ls
cd Angular\ Projects
ng new my-first-app
which ng
which angula
which angular
echo $path
npm -v
which node
node -v
npm install -g #angular/cli
ng --version
brew install node
node -v
brew postinstall node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
npm install -g n
sudo npm install -g n
sudo n stable
brew doctor
npm -v
which brew
which npm
brew -v
brew update
npm -v
npm install npm
npm -v
npm install -g npm
npm install -g #angular/cli
ng --version
npm ls --global --depth 0
npm install -g #angular/cli#latest
echo -e "export PATH=$(npm prefix -g)/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
ng --version
cat ~/.bashrc
echo $PATH
ng new my-app
ls
my-app
npm outdated
..
npm install --save core-js#^3
my-app
npm install --save core-js#^3
npm install --save-dev "jasmine-core#>=3.5"
ng serve --open
exit
ls
cd code
ls
codesign
code
ls
cd code
ls
cd JoshInLisbon
ls
cd an
cd Angular\ Projects
ls
cd my-app
code
ng
code
code --
ls
cd ..
cd taaalk
cd ..
cd taaalk_edge
echo $PROMPT
zsh --version
chsh -s
ls
cd code
ls
cd JoshInLisbon
chsh -s /bin/bash
ls
zsh --version
ls -a
cat .zshrc
cd .zshrc
echo $PATH
echo $PATH
vim
cat
ls -a
vim .zshrc
vim .bash_profile
~/.MacOSX/environment.plist
echo $PATH
plutil -lint ~/.MacOSX/environment.plist
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
ls
ls -a
cat .zshrc.backup
cd code
cd JoshInLisbon
cd Rails\ Projects
rails new thing
rails new thing
echo $PATH
..
ls ..
cd ..
cd ..
cd
ls -a
cat .zshrc.backup
ls -a
cat .zsh_history
vim .bash_profile
ls -a
vim .zcompdump-jmpb-5.7.1
vim zshrc.backup
cat .zshrc
.zsh_history
vim .zshrc
vim .shell.pre-oh-my-zsh
ll
ll -a
I can see my .zshrc file when I do ls -a, however it is empty.
This is my .bash_history
ls
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
history
which zch
which zsh
echo path
echo $PATH
cd code
cd JoshInLisbon/
rails new project
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
echo $PAtH
echo $PATH
rails new
npm -version
which zsh
cd ..
cd ~
ls
ls -a
cat .szhrc
cat .zshrc
cat .zshrc
ls -a
cat .bash_profile
echo $PATH
cat .zsh_history
cat .bashrc
export PATH=/Users/joshua/.npm-global/bin:./bin:./node_modules/.bin:/Users/joshua/.rbenv/shims:/Users/joshua/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/usr/local/sbin
echo $PATH
which ruby
which zsh
cd code/
cd JoshInLisbon/
cd Rails\ Projects/
rails new thing
ls
rm -R thing
ls
which brew
which zsh
cd ..
cd
ls
ls -a
cat .zshrc
cat .zshrc
cat .bash_profile
cat .oh-my-zsh/
cd .oh-my-zsh/
ls
cat .oh-my-zsh.sh
cd
ls
ls -a
cat .zshrc.backup
echo $ZSH
export ZSH="/Users/joshua/.oh-my-zsh"
echo $ZSH
which rails
ll
This might be my old path? export PATH=/Users/joshua/.npm-global/bin:./bin:./node_modules/.bin:/Users/joshua/.rbenv/shims:/Users/joshua/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/usr/local/sbin
Anyway, you are a hero if you made it this far and if you have any idea what I should do I would be very grateful.
This was resolved by moving my dotfiles (which contains all my zsh settings) back to the original location. The change was caused by the re-ordering of many folders.
I also restored my PATH back to the old one pasted at the bottom of the question.

'scl enable' not setting PATH with bash

I'm trying to use software collections on CentOS 6.8 server, but it won't set the environment variable PATH corectly if the command passed is "bash", but "tcsh" works... (however we don't use tcsh on this machine)
Example:
$ scl --list
devtoolset-4
python27
rh-java-common
rh-perl524
rh-python35
$ which python
/usr/bin/python
$ python --version
Python 2.6.6
$ scl enable python27 bash
$ which python
/usr/bin/python
$ python --version
Python 2.6.6
$ cat /opt/rh/python27/enable
export PATH=/opt/rh/python27/root/usr/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export MANPATH=/opt/rh/python27/root/usr/share/man:${MANPATH}
# For systemtap
export XDG_DATA_DIRS=/opt/rh/python27/root/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
# For pkg-config
export PKG_CONFIG_PATH=/opt/rh/python27/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
$ echo $PATH
/usr/share/gridengine/bin/lx26-amd64:/usr/lib64/qt-3.3/bin:/usr/NX/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/bin:/opt/maker/bin:/opt/tools/:/opt/tools/amos-3.1.0:/opt/mpich-install/bin:/opt/pssc/bin:/opt/torque/bin:/opt/torque/sbin
$ echo $LD_LIBRARY_PATH
/opt/rh/python27/root/usr/lib64
$ echo $MANPATH
/opt/rh/python27/root/usr/share/man::/opt/mpich-install/share/man:/opt/mpich-install/share/man
So why is MANPATH and LD_LIBRARY_PATH being set properly but not PATH? If i use tcsh it works as expected:
$ scl enable python27 tcsh
$ which python
/opt/rh/python27/root/usr/bin/python
$ python --version
Python 2.7.8
Thank you Dominic you were on to something. I originally checked ~/.bash* files as well as /etc/bash* and /etc/profile but after your comment, I found several scripts in /etc/profile.d/ that we being executed, and one of them set the PATH explicitly without appending. I added $PATH back in there and now scl enable is working as expected!

RVM installation fails

I followed the instructions from the RVM homepage (https://rvm.beginrescueend.com/rvm/install/)
I typed the following:
sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
the script executed and didn't seem to give any errors.
the same without sudo gave an error:
Warning: Failed to create the file
Warning: /usr/share/ruby-rvm/archives/wayneeseguin-rvm-stable.tgz: Permission
Warning: denied
0 792k 0 3908 0 0 2257 0 0:05:59 0:00:01 0:05:58 2257
curl: (23) Failed writing body (0 != 3908)
Could not download 'https://github.com/wayneeseguin/rvm/tarball/stable'.
curl returned status '23'.
I also put this in my ~/.bashrc
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
what I get is:
~$ type rvm | head -1
bash: type: rvm: not found
~$ source "/usr/local/rvm/scripts/rvm"
bash: /usr/local/rvm/scripts/rvm: No such file or directory
~$ source "$HOME/.rvm/scripts/rvm"
bash: /home/anonym/.rvm/scripts/rvm: No such file or directory
How can this be fixed?
PS
I'm using Ubuntu 11.10
Sudo problems. This worked for me to install rvm. Just do:
curl -L https://get.rvm.io | sudo bash -s stable --ruby
nano ~/.bashrc
at the bottom of the file add these line
unset rvm_path
unset GEM_HOME
Then run
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.3
rvm use 2.1.3 --default
ruby -v
Can you try this:
$ curl -s raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable
or
$ curl -s raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | sudo bash -s stable
depending on your permissions. That should help. It helped me! :)
I'm sure there's a more elegant way to fix this, but I ran into the same issue and was kinda in a hurry, so I went for a quick and dirty workaround:
$ curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer > foo.sh
$ chmod 755 foo.sh
Then edit line 162, and add -k to the curl command:
$ vim foo.sh
162 if curl -Lk https://github.com/${_repo}/rvm/tarball/${_branch} -o ${rvm_archives_path}/${_repo}-rvm-${_branch}.tgz
Then run the script:
$ ./foo.sh --branch stable
Like I said, not ideal, but it got me where I needed to be quickly.
If anyone has this problem in the future in ubuntu I was getting this error because of an old package that still had config stuff hanging around.
Try running
sudo apt-get --purge remove ruby-rvm
That should take care of the permission error and let you install RVM as a normal user under $HOME/.rvm
Put this in your .bashrc instead (without echo)
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
And restart your terminal.
You might have RVM installed under the wrong user. RVM won't let you install if it's installed under another user.
You can uninstall it from the other user with rvm implode.
Log over to the other user and RVM will install correctly!
https://rvm.io/rvm/security#ipv6-issues
You can forbid gpg's internal dirmngr from using IPv6 by add the following line to ~/.gnupg/dirmngr.conf:

installing rvm on lion

How to install RVM on lion, I am trying this command in terminal
curl -s https://rvm.beginrescueend.com/install/rvm
After this command executed it gives me a lot of text in terminal, the text is ending with this statement
chmod +x ./scripts/install
# Now we run the RVM installer.
./scripts/install ${flags[*]} --prefix "$rvm_prefix" --path "$rvm_path"
After it finish executing I tried rvm command but its giving me error command not found
Are you running only curl -s https://rvm.beginrescueend.com/install/rvm in terminal? This only displays the content of the script.
If you want to install it you need to run something like this(assuming you are using bash):
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Copied from:
http://ruby.about.com/od/rubyversionmanager/ss/Installing-Ruby-On-Snow-Leopard-With-Rvm_6.htm
Now that the RVM scripts are installed, you need to load them every
time your start a new Bash session. This is achieved by adding a line
to the end of your ~/.bash_profile file. You can add this line with
your favorite text editor, or by using the cat command. Just hit
Ctrl-D after you've typed the line and it will write that line to the
file and return you to the bash prompt.
$ cat >>~/.bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

Resources