Running commands in a script as another user using a here doc - bash

I want to be able to switch users in the middle of a script. Here is one attempt:
su - User << EOF
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
EOF
My goal is to execute the code between the EOF delimiters as if I actually logged in as User.
The middle line is supposed to install Homebrew. If I log in as User and run the middle line on its own, it installs fine. But running the full script above gives me problems:
-e:5: unknown regexp options - lcal
-e:6: unknown regexp options - lcal
-e:8: unknown regexp options - Cach
-e:9: syntax error, unexpected tLABEL
BREW_REPO = https://github.com/Homebrew/brew.freeze
^
-e:9: unknown regexp options - gthb
-e:10: syntax error, unexpected tLABEL
CORE_TAP_REPO = https://github.com/Homebrew/homebrew-core.freeze
^
-e:10: unknown regexp options - gthb
-e:32: syntax error, unexpected end-of-input, expecting keyword_end
-bash: line 34: end: command not found
-bash: line 36: def: command not found
-bash: line 37: escape: command not found
-bash: line 38: end: command not found
-bash: line 40: syntax error near unexpected token `('
-bash: line 40: ` def escape(n)'
I've tried a could different commands instead of just the Homebrew install but have problems most of the time. What is the difference between passing a command to 'su' as I am trying to do and actually running the command as that user?

What is happening is, the embedded $(...) command gets executed before the here-document is passed to su. That is, the actual script that is passed to su is something more like this:
/usr/bin/ruby -e "#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/Homebrew/brew/tarball/master anywhere you like or
# change the value of HOMEBREW_PREFIX.
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
...
And so on. In other words, the output of $(...) got inserted into the here-document.
To avoid that, you need to escape the $:
su - User << EOF
/usr/bin/ruby -e "\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
EOF
Alternatively, you can tell the shell to treat the entire here-document literally without any interpolation, by enclosing the starting EOF within double-quotes:
su - User << "EOF"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
EOF

Related

Process substitution command in . ebextensions

I'm trying to install Netdata in my aws beanstalk instances. I created a config file in my .ebextensions folder
container_commands:
00install:
command: "bash <(curl -Ss https://my-netdata.io/kickstart.sh) --dont-wait"
ignoreErrors: true
When the command gets ran on deploy beanstalk logs this error.
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `bash <(curl -Ss https://my-netdata.io/kickstart.sh) --dont-wait'
I had no idea what <() meant so I looked it up and saw it was process substitution. From what I understood process substitution can be rewriting using plain pipes.
For example
more <( ls /usr/bin )
Could be
ls /usr/bin | more
In my command I'm also passing in flags so I was having issues gettin the piped version of the command working.
NOTE: The root problem is beanstalk telling me its confused about the parenthesis. My solution was just transforming the command to use regular pipes. However, if anyone knows just how I write this command on the beanstalk config to get it working that would be awesome.

command not found: shopt during anaconda installation process [duplicate]

I have an Amazon EC2 Machine running Ubuntu 10.04.
The default user, ubuntu's .bashrc seems to be behaving properly, but if I ssh or su to the second user, mikey, it doesn't display properly until I run bash:
Ex 1.) Changing user with su
mikey#home ~$ ssh ubuntu#EC2
ubuntu#EC2:~$
ubuntu#EC2:~$ su mikey
$
$ bash
mikey#EC2: $
Ex 2.) SSH-ing in directly as the user
mikey#home ~$ ssh mikey#EC2
/home/mikey/.bashrc: 13: shopt: not found
/home/mikey/.bashrc: 21: shopt: not found
/home/mikey/.bashrc: 99: shopt: not found
/etc/bash_completion: 33: [[: not found
/etc/bash_completion: 39: [[: not found
/etc/bash_completion: 52: Bad substitution
\[\e]0;\u#\h: \w\a\]\u#\h:\w$
\[\e]0;\u#\h: \w\a\]\u#\h:\w$ bash
mikey#EC2:~$
I've tried playing around with ~/.profile and ~/.bash_login to include
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
but so far, nothing has seemed to do the trick.
any pointers would be greatly appreciated. thanks!
If, in fact, your shell isn't bash, you can try to change it like so:
usermod -s /bin/bash mikey
If /bin/bash is the location of bash on that system.
I think your default shell is dash or sh and not bash in this case. echo $SHELL should show it, should it show /bin/sh, it might be a link, so check that ls -l /bin/sh doesn't link to some other shell.
Can't you use chsh to change shell? instead of hacking /etc/passwd?
In order to fix this on the permanent basis, change /etc/passwd
From:
kwilson:x:3042:3042::/home/jjson:/bin/sh
Change it to:
kwilson:x:3042:3042::/home/jjson:/bin/bash

Bash script errors with setting into variable to be executed script paths

I have the following script:
https://gist.github.com/pc-magas/37ec5e668c12017eb32aa308a0cb8f35
And on the execution I have these error messages:
./scripts/install2.sh: line 84: /home/pcmagas/Kwdikas/Docker/ellakcy/scripts/../start.sh: Permission denied
./scripts/install2.sh: line 87: /home/pcmagas/Kwdikas/Docker/ellakcy/scripts/../stop.sh: Permission denied
./scripts/install2.sh: line 89: ${STARTUP_SCRIPT_PATH}: ambiguous redirect
chmod: missing operand after 'u+x'
Try 'chmod --help' for more information.
Startup script generated
./scripts/install2.sh: line 94: ${STOP_SCRIPT_PATH}: ambiguous redirect
chmod: missing operand after 'u+x'
Try 'chmod --help' for more information.
The source of the errors are these lines of script:
touch "${SCRIPT_PATH}/start.sh"
STARTUP_SCRIPT_PATH= "${SCRIPT_PATH}/start.sh"
touch "${SCRIPT_PATH}/stop.sh"
STOP_SCRIPT_PATH= "${SCRIPT_PATH}/stop.sh"
What I want/try to do is my script by given some parameters to auto-generate scripts in order to start and stop some docker container services.
But somehow the plan backfires to me right now.
May I have some help please.
You need sudo to get permission to write the script files, and there were spaces between the variable name and value; there must not be any spaces around the = signs:
sudo touch "${SCRIPT_PATH}/start.sh"
STARTUP_SCRIPT_PATH="${SCRIPT_PATH}/start.sh" # Note no spaces ..PATH="${..
sudo touch "${SCRIPT_PATH}/stop.sh"
STOP_SCRIPT_PATH="${SCRIPT_PATH}/stop.sh" # Again here.

Confusing Error message when using Brew

Every time when I run a brew command, it will give me several error lines at the beginning. I can't understand what's going on here. Can anyone tell me how I can get rid of the error?
brew
/bin/sh: __rvm_ruby_string_find: line 8: syntax error near unexpected token `('
/bin/sh: __rvm_ruby_string_find: line 8: ` ruby-+([1-9]) | ruby-+([1-9]).+([0-9]) | ruby-1.+([1-9]).+([0-9]) | jruby-[19]*)'
/bin/sh: error importing function definition for `__rvm_ruby_string_find'
/bin/sh: __rvm_project_ruby_env_load_parse_file: line 9: syntax error near unexpected token `('
/bin/sh: __rvm_project_ruby_env_load_parse_file: line 9: ` __rvm_read_lines __variables <( { cat "$1"; echo ""; } | __rvm_sed "${__sed_commands[#]}" )'
/bin/sh: error importing function definition for `__rvm_project_ruby_env_load_parse_file'
/bin/sh: __rvm_remove_without_gems: line 2: syntax error near unexpected token `('
/bin/sh: __rvm_remove_without_gems: line 2: ` __rvm_read_lines __gems_to_remove <('
/bin/sh: error importing function definition for `__rvm_remove_without_gems'
Now I found if I run an illegal command for ruby, it gives me a same error message. So is it because my version problem?
To my mind, you need to install ruby first as the errors you're receiving are coming from BASH (it is trying to parse the script as bash but doesn't succeed). Not sure how you managed to install Homebrew without ruby.
I've had this error due to RVM being source'ed during bash profile setup.
It seems the RVM script cannot handle being sourced multiple times into the same shell.
This was being triggered by commands like ssh-agent, which spawn a new sub-shell, or simply running bash again.
My solution was to wrap the RVM source'ing in my .bashrc with a check for the $rvm_version variable:
if [ -z "$rvm_version" ]; then
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
fi

Error installing Homebrew: /etc/sudoers: syntax error near line 46

I am trying to install Homebrew (using the code snippet right off their site):
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I am getting:
>>> /etc/sudoers: syntax error near line 46 <<<
sudo: parse error in /etc/sudoers near line 46
sudo: no valid sudoers sources found, quitting
Not great with terminal commands and lingo. Can anyone help?
Your /etc/sudoers file seems to be borked.
Never open sudoer file with a normal editor. always use visudo
just type
sudo visudo
this will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.
if you make an error in sudoer file, you will lose sudo access, so always use visudo

Resources