EC2 command line tools - bash

I am trying to setup up Amazon's EC2 service and am having some trouble.
I have downloaded the Amazon EC2 API Tools, which I've put in a folder ~/.ec2 along with my .cert and .pemfiles.
When I attempt any command from ~/.ec2/bin I get an error /Users/zmjones/.ec2/bin/ec2-cmd: /usr/bin/env: bad interpreter: Operation not permitted. I asked about this in "/usr/bin/env bad interpreter".
Now that I have removed the DOS line-endings using variants of this mv /users/zmjones/.ec2/bin/ec2-add-keypair /users/zmjones/.ec2/bin/ec2-add-keypair.bak
tr -d '\r' < /users/zmjones/.ec2/bin/ec2-add-keypair.bak > /users/zmjones/.ec2/bin/ec2-add-keypair, I've tried to execute some of the commands in ~/.ec2/bin and have been unable to get it to work.
I've gotten a permission denied error, which when I then use sudo, tells me that the environment variable EC2_HOME doesn't exist, while echo $EC2_HOME indicates that it does. Here is my ~/.bash_profile.
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:$PATH
PATH=/opt/local/sbin:/usr/local/bin:/usr/x11/bin:/usr/texbin:$PATH
PATH=/usr/local/texlive/2011/bin/x86_64-darwin:$PATH
PATH=/Library/Frameworks/EPD64.framework/Versions/Current/bin:$PATH
EC2_HOME=~/.ec2
PATH=$EC2_HOME/bin:$PATH
EC2_PRIVATE_KEY=`ls $EC2_HOME/pk-*.pem`
EC2_CERT=`ls $EC2_HOME/cert-*.pem`
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home/
export PATH

You're getting a "permission denied" error because the execute (+x) bit is not set on your modified script.
Do not use sudo to fix this.
Simply set the +x bit:
chmod +x /users/zmjones/.ec2/bin/ec2-add-keypair
(and the same for your other scripts with fixed line endings).
The reason sudo didn't work is that by default it starts with a clean environment, without your EC2_HOME and other environment variables.
The reason you don't want to use sudo to fix the problem anyway, is that running with sudo runs the whole program as root, which has the possibility of doing bad things on your system. At the very least, it might create files or subdirectories in your local directory that are owned by root, which you then have to take extra steps to modify/delete later. At worst, a bug in the program could wipe out your whole system, or important parts of it.

If ls -al# shows com.apple.quarantine extended attrib, the files won't be executed. You'll need
xattr -d com.apple.quarantine filename

Related

ssh does not find /usr/local/bin path

If I log in to my remote Mac via ssh -p22 jenkins#192.168.2.220 and type docker, it finds the executable because it also finds the path /usr/local/bin if I check with echo $PATH. But if I do the same in a heredoc inside a file setup-mac.sh like
#!/bin/bash
ssh jenkins#192.168.2.220 '/bin/bash -s' << 'EOF'
"echo $PATH"
"bash run-docker.sh"
EOF
which I execute via shell and bash setup-mac.sh it does not find /usr/local/bin in PATH and consequently does not run docker, because the command is unknown.
On the remote Mac, there is a file run-docker.sh which is a bash file that calls docker commands, and it works if called locally.
To solve this issue, I've enabled PermitUserEnvironment on the mac in sshd_config, but this did not work. Though I only restarted ssh service and not the whole machine. Meanwhile I've changed all docker commands on the remote run-docker.sh script to an alias ${DOCKER} and I initialize it at the beginning of the script to DOCKER=/usr/local/bin/docker, but this is only a workaround.
I guess that the problem is occurring because /usr/local/bin is being added to the PATH by the 'jenkins' user's personal initialization file (~/.bashrc). That is run only by interactive shells, and the shell run by ssh ... '/bin/bash -s' ... is not interactive.
You could try forcing the shell to be interactive by invoking it with /bin/bash -i -s, but that is likely to cause other problems. (The shell may try and fail to set up job control. The value of PS1 may appear in outputs. ...)
In general, you can't rely on the PATH being set correctly for programs. See Setting the PATH in Scripts - Scripting OS X for a thorough analysis of the problem. Correct way to use Linux commands in bash script is also relevant, but doesn't have much information.
A simple and reliable way to fix the problem permanently is to set the required PATH explicitly at the start of run-docker.sh. For example:
export PATH=/bin:/usr/bin:/usr/local/bin
You may need to add other directories to the path if run-docker.sh runs programs that are in other locations.
Another solution to the problem is to use full paths to commands in code. However, that makes the code more difficult to read, more difficult to maintain, and more difficult to test. Setting a safe PATH is usually a better option.

How can I determine where an error message printed during shell startup comes from?

After uninstall Docker on Ubuntu, each time I start a bash shell window, it emits the following hint:
The program 'docker' is currently not installed. You can install it by typing:
sudo apt install docker.io
I have searched in .bashrc, .profile, .bash_profile, but didn't found any lines related to docker.
So, how can I get rid of those tips?
Run PS4=':${BASH_SOURCE}:$LINENO+' bash -x -l -i to log every command in your startup scripts, including which config file or script they came from. Search through that, and you'll find the individual command that's creating this error.
(Note that very new versions of bash ignore inherited values of PS4 when running as root for security reasons. But you're not using root as your primary account... right?)

Raspbian: Reset Bash environment variables

I was trying to get a crontab working on my Raspberry PI and I think I messed up my environment variables. I can execute a file from the GUI by right-clicking and choosing execute. However I cannot get the same file to run from command line. I can use ls to see the file (ChromeTab.sh), but when I type ChromeTab.sh, I get "bash: ChromeTab.sh: command not found".
I think I messed up my environment variables when I put this in the crontab.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I followed the examples in Script doesn't run via crontab but works fine standalone.
Any idea what I'm doing wrong?
UPDATE:
OK,
Let me clarify what efforts I took on my part BEFORE posting my question on stackoverflow before getting anymore downvotes.
First of all thanks S. Adam Nissley for your suggestions.
In answer to your steps listed above.
Running this from home path, or fully qualified path does Not work as stated.
Error: bash: ChromeTab.sh: command not found
./ChromeTab.sh
I have also ensured read/write and execute permissions on the file with
chmod +x ./ChromeTab.sh
Also, my bash script starts off with the following shebang
#!/bin/sh
So, what i'm trying to say is, regardless of using crontab or not the issue at hand is that I can not even execute the script from command line. This started happening after I was messing around with the environment variables in the crontab. I'm looking for a way to revert to the situation where I can at least run/execute bash commands from the terminal.
The only way I can effectively execute this script is (right-click execute) through the GUI.
Assuming you are in the same directory as your script, you should just be able to enter
./ChromeTab.sh
If it does not execute, make sure it is executable with the command
chmod +x ./ChromeTab.sh
Or
chmod 755 ./ChromeTab.sh
And if it still won't execute, make sure it has an appropriate hashbang on the very first line of the script like #!/bin/sh or #!/bin/bash
When you add it to your crontab, make sure it has the full path like
/home/pi/bin/ChromeTab.sh <br/>
EDIT: Default PATH and SHELL for Raspbian
You can check your PATH and SHELL environmental variables from the command line as follows:
echo $SHELL
echo $PATH
The default PATH for Rasbian is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
And the default SHELL is:
/bin/bash
So if you need to set those it is as simple as:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash
If you are having other issues with your environment, you may want to disable some of your local settings to see if the problem is in your profile. You can list all files with ls -a, which includes hidden files. Raspbian typically has a .bashrc and a .profile in each user's home directory. To disable them simple rename them:
mv .bashrc .bashrc_disabled
mv .profile .profile_disabled
If that solves the problem, you can inspect the files and make the necessary corrections before renaming them back to their original names.

Error message: -bash: /etc/profile.d/rvm.sh: No such file or directory

After installing Ruby successfully on my mac, every time I start the terminal, I get this message:
Last login: Sun May 19 00:47:06 on ttys000
-bash: /etc/profile.d/rvm.sh: No such file or directory
What does it mean? Do I need to fix this? if so how?
Every time you start Terminal or a new tab in Terminal, you begin a bash shell session. You can set some commands to run every time you start a bash session. Typical use cases include setting some aliases (e.g. gst for git status), environment variables, and running other bash scripts. The place to put these initial bash commands is usually in ~/.bashrc or ~/.bash_profile. Start by checking those files to see if you have any reference to the file /etc/profile.d/rvm.sh. Also, check whether said file actually exists on your machine. If it doesn't, you can probably safely delete the corresponding line from your bashrc or bash_profile. If it does exist, there's something else funny going on which you'll have to debug.
Also note, the way RVM works, it requires you to run a certain script every time. If you have RVM and plan on using it, does it work? If not, you might need to find where the appropriate RVM script lives on your machine and add something to your bashrc or bash_profile that runs the script. Normally when you install RVM it tells you what command to add and where. For example by ~/.bashrc file includes the following command:
[[ -s "/Users/amitgupta/.rvm/scripts/rvm" ]] && source "/Users/amitgupta/.rvm/scripts/rvm"
which checks that the rvm script exists, is a non-empty file, and then runs it.

terminal sudo command

I'm a complete novice at this (terminal in Mac leopard) and hoping to get an lifeline from the web as I've certainly hit a wall.
I want to run a script as a root in terminal. The script is saved as a text file with a .rtf extension. I've inserted into terminal:
sudo filename.rtf
then I get asked a password which I've typed in (admin password) and pressed enter; then it shows a prompt: sudo: Mac: command not found
I've tried it with the extension hidden but got the same result. Any pointers on what I'm doing wrong?
Thanks in advance
You need to first get the script out of the .rtf file and into a plain text file (open it up in TextEdit and select "Make Plain Text" from the format menu, then save it again as myscript.sh).
Now you can type
sudo sh myscript.sh
The "magic" sh letters there are because as another responder says, sudo will temporarily elevate you to superuser and run a program. In *nix environments, that would be anything with the executable bit set, meaning that someone's explicitly told the operating system that it's safe to run a file. In your case, your myscript.sh has not been "blessed" in this way, so to run it you need to feed it into a program that knows how to understand it. That program is sh, and it does have the executable bit set. Thinking of it as sudo (sh myscript.sh) might make it a bit clearer.
If you plan on running this script a lot, you might want to actually make it executable on its own. This amounts to putting special instructions inside the file that tell the operating system how the file should be run. If you stick #!/bin/sh (this is called a shebang line and tells the OS what to do with your file) on the first line of your script, and then type chmod u+x myscript.sh (this tells the OS that you, and only you, are allowed to execute your file), you'll be able to run the file directly with sudo myscript.sh.
sudo is used to execute commands as the root user of the machine.
when you type
sudo [somthing]
the shell grants temporary root privilges and then executes the given "somthing"
assume your script is in bash, you should have done
sudo sh filename.rtf
Also, it's better to save script as plain txt, with an sh extension, so you would execute
sudo sh myscript.sh
first set the script as executable:
chmod +x filename.rtf
Then you can run it like so:
sudo ./filename.rtf

Resources