How can I echo a shell command as a string - shell

I want place the following string into my .zshrc file using the command line
eval "$(docker exec -it <abc-123>)"
I've tried:
echo "eval "$(docker exec -it <abc-123>)"" >> .zshrc
and every other ` and ' combination
The result I want is to have my .zshrc file execute
eval "$(docker exec -it <abc-123>)"
much like it does for homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
I just want to be able to write to my .zshrc file using echo. How can I achieve this?

echo 'eval "$(docker exec -it <abc-123>)"' >> .zshrc
Will add
eval "$(docker exec -it <abc-123>)"
at the end of your .zshrc file

A here doc can print a string verbatim, without quoting issues:
cat <<"EOF" >> .zshrc
eval "$(docker exec -it <abc-123>)"
EOF
In this case, you could do echo 'eval "$(docker exec -it <abc-123>)"' >> .zshrc, provided <abc-123> doesn't contain a single quote (').

Related

how to echo $(command) into bash profile

I am trying to append eval "$(rbenv init -)" to my bash profile
(I am trying to follow this instruction)
# Load rbenv automatically by appending
# the following to ~/.bash_profile:
eval "$(rbenv init -)"
This is part of an automated process so I don't have access to a GUI or can't use an editor. but when I echo the command $(rbenv init -) gets executed and append bunch of stuff. How can I echo it as plain text?
this is what I have now
ec2-user#ip-172-31-46-129 ~ % cat /tmp/install-rbenv.sh
+-zsh:41> cat /tmp/install-rbenv.sh
#!/bin/bash
sudo -i -u buildkite-agent bash << EOF
echo "export RUBY_CONFIGURE_OPTS=\"--with-openssl-dir=$(brew --prefix openssl#1.1)\"" >> /Users/buildkite-agent/.bash_profile
echo 'eval "$(rbenv init -)"' >> /Users/buildkite-agent/.bash_profile
EOF
echo "Done"
running it
ec2-user#ip-172-31-46-129 ~ % . /tmp/install-rbenv.sh
+-zsh:42> . /tmp/install-rbenv.sh
+/tmp/install-rbenv.sh:6> brew --prefix openssl#1.1
+/tmp/install-rbenv.sh:6> rbenv init -
+/tmp/install-rbenv.sh:6> sudo -i -u buildkite-agent bash
+/tmp/install-rbenv.sh:10> echo Done
Done
checking the bash profile
ec2-user#ip-172-31-46-129 ~ % sudo su - buildkite-agent
+-zsh:43> sudo su - buildkite-agent
rbenv: no such command `sh-'
-bash: eval: line 31: syntax error near unexpected token `rehash'
-bash: eval: line 31: ` rehash|shell)'
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
ip-172-31-46-129:~ buildkite-agent$ cat ~/.bash_profile
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/opt/openssl#1.1"
eval "export PATH="/Users/ec2-user/.rbenv/shims:${PATH}"
export RBENV_SHELL=zsh
source /usr/local/Cellar/rbenv/1.1.2/libexec/../completions/rbenv.zsh
command rbenv rehash 2>/dev/null
rbenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(rbenv "sh-$command" "$#")";;
*)
command rbenv "$command" "$#";;
esac
}"
Apparently you don't want to use an editor to edit your .bash_profile file or you are afraid you will got trapped inside vi :-D
Put the text you want to append to the file in apostrophes and it will go verbatim to the file. The shell does not do any expansion in the arguments that are wrapped in apostrophes.
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
That's all.
Use apostrophes around the first EOF, i.e. 'EOF' instead of EOF. bash distinguishes between the two and won't evaluate stuff if you use the apostrophe version.
> bash << EOF > test.sh
echo '$(date)'
EOF
> cat test.sh
Mon 12 Apr 2021 08:08:53 PM CEST
> bash << 'EOF' > test.sh
echo '$(date)'
EOF
> cat test.sh
$(date)

Start docker from command line

Is there a command for starting docker from the command line? While this works, it is quite lame:
$ open /Applications/Docker.app/
$ docker returns help text about docker cli commands.
An alternative could be setting up an alias and place it inside ~/.bashrc
echo -e "\nopen-docker='open /Applications/Docker.app/'" >> ~/.bashrc
Ended up adding these alias to ~/.bash_profile or ~/.zshrc.
alias dock="open -a 'Docker'"
alias dock_start="open -a 'Docker'"
alias docker_start="open -a 'Docker'"

Making bash script continue after exec $SHELL

I'm making a bash script that would install rbenv and ruby.
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install $rubyVersion
rbenv global $rubyVersion
But when the exec $SHELL is called the bash process is replaced by new bash process and the script stops (of course).
How can I make the script to continue?
It appears that you're trying to achieve multiple objectives by modifying the .bashrc file then calling exec $SHELL. Neither of those actions will modify the shell-in-which-this-script-is-running. To modify the current shell, you want to "source" the .bashrc file. Use the "dot notation" instead of calling exec $SHELL:
. ~/.bashrc
Good luck with this one!
replace exec $SHELL lines with "$SHELL" lines or completely remove those lines

Execute script to inside another bash script

On my server I try run:
#!/bin/bash
PATH="/SANCFS/stats/scripts/"
for (( i=6;i<=8;i++ ));
do
echo "Running $i"
exec "/SANCFS/stats/scripts/load_cdrs.sh --debug --config /SANCFS/stats/scripts/iquall-mm4-cdr.cfg --date '2018-10-0"$i"' >> /home/stats/201810/load_cdrsIMRMM4-0"$i".ok 2>>/home/stats/201810/load_cdrsIMRMM4-0"$i".err"
done
And the result is:
cannot execute: No such file or directory
Your help, how edit/modify to run successfully ?
Here's an easier way to reproduce your problem:
$ exec "echo "hello world""
bash: exec: echo hello: not found
Running a command in bash does not require adding exec or quotes:
$ echo "hello world"
hello world
Additionally, you are using $i in single quotes in one case, and you're overwriting the shell search path PATH for seemingly no reason. Applied to your example:
#!/bin/bash
for (( i=6;i<=8;i++ ));
do
echo "Running $i"
/SANCFS/stats/scripts/load_cdrs.sh --debug --config /SANCFS/stats/scripts/iquall-mm4-cdr.cfg --date "2018-10-0$i" >> /home/stats/201810/load_cdrsIMRMM4-0"$i".ok 2>>/home/stats/201810/load_cdrsIMRMM4-0"$i".err
done
Don't use exec. That replaces the current process with the process that runs the specified command, so you won't repeat the loop. Just execute the command normally.
And the argument to exec shouldn't be all inside a single quoted string. Maybe you're confusing it with eval?
#!/bin/bash
PATH="/SANCFS/stats/scripts/"
for (( i=6;i<=8;i++ ));
do
echo "Running $i"
/SANCFS/stats/scripts/load_cdrs.sh --debug --config /SANCFS/stats/scripts/iquall-mm4-cdr.cfg --date 2018-10-0"$i" >> /home/stats/201810/load_cdrsIMRMM4-0"$i".ok 2>>/home/stats/201810/load_cdrsIMRMM4-0"$i".err
done
You could replace exec with dot ( . )
If you try the 5 options, you should see the different options
$ exec /bin/bash
$ /bin/bash
$ . /bin/bash
$ ./bin/bash
$ /bin/bash /bin/bash

Echo variable using sudo bash -c 'echo $myVariable' - bash script

I want to echo a string into the /etc/hosts file. The string is stored in a variable called $myString.
When I run the following code the echo is empty:
finalString="Hello\nWorld"
sudo bash -c 'echo -e "$finalString"'
What am I doing wrong?
You're not exporting the variable into the environment so that it can be picked up by subprocesses.
You haven't told sudo to preserve the environment.
\
finalString="Hello\nWorld"
export finalString
sudo -E bash -c 'echo -e "$finalString"'
Alternatively, you can have the current shell substitute instead:
finalString="Hello\nWorld"
sudo bash -c 'echo -e "'"$finalString"'"'
You can do this:
bash -c "echo -e '$finalString'"
i.e using double quote to pass argument to the subshell, thus the variable ($finalString) is expanded (by the current shell) as expected.
Though I would recommend not using the -e flag with echo. Instead you can just do:
finalString="Hello
World"

Resources