Adding key to ssh agent when shell is opened - shell

Why isn't this code working in my .bash_profile?
echo $SSH_AUTH_SOCK
if [ -z "$SSH_AUTH_SOCK" ]; then
ssh-add
fi
My identity is not getting added when shell is opened. The variable $SSH_AUTH_SOCK is being echoed when the shell opens:
/private/tmp/com.apple.launchd.UKHCb6uhHB/Listeners
So if the variable is defined, why isn't the ssh-add statement being evaluated?
If I run ssh-add manually from the command line, or place it on it's own separate line of my .bash_profile, it successfully runs and adds my identity to the ssh agent.
Additionally, making thing simpler and just trying to echo a value doesn't work, for example, I do not see test being echoed after placing this in the file:
echo $SSH_AUTH_SOCK
if [ -z "$SSH_AUTH_SOCK" ]; then
echo test
fi

Whoops, I was using the wrong operator. Should be -n, not -z.

Related

Setting environment path variable in bashrc not testing at command line

I'm trying to understand setting variables in .bashrc and .bash-profile.
If I add something to my .bashrc, like this
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export MMMM_HOME=/appl #added
#. ~/inits.bsh #added
. /appl/etc/mmmm.env #added
How can I check that it's set properly? I tried opening a new putty session, I tried typing bash, I tried at the command line [-z "$MMMM_HOME"] && echo "empty" || echo "Not empty" #-z: command not found...
I tried which MMMM_HOME and which $MMMM_HOME at the command line.
I googled it. I'm not sure about this...does anyone have any info? Thanks!
Two spaces are missing in your test. Here's the command with the right bash syntax:
[ -z "$MMMM_HOME" ] && echo "empty" || echo "Not empty"

Alias command incorrectly executing

I am attempting to load my git aliases from a gist on github. For some reason, the command executes find, but when I attempt to execute any of the aliases, they are either incorrectly mapped — e.g., gsts -> git stash instead of gsts -> git status — or they are not mapped at all.
#!/bin/bash
update_git_aliases(){
GIST_URL='https://gist.githubusercontent.com/Moyoka22/ec605b0b52fee6d6d30d5f72822938f4/raw/git-aliases'
RESPONSE="$(wget --no-cache -qO- ${GIST_URL})"
if [ ${?} -ne 0 ]
then
echo 'Download failed. Exiting.'
return 1
fi
echo ${RESPONSE} > ${1}
chmod +x ${1}
}
DOWNLOAD_FAILED=0
ALIAS_FILE="${HOME}/.config/git-aliases"
if [ ! -f ${ALIAS_FILE} ]
then
echo "Git aliases not found! Downloading..."
update_git_aliases ${ALIAS_FILE}
DOWNLOAD_FAILED=${?}
fi
if [ ${DOWNLOAD_FAILED} -ne 0 ]
then
echo "Downloading aliases failed."
exit 1
fi
cat ${ALIAS_FILE} | bash
I assume you want the aliases on your interactive shell, so remove the last cat as it's useless and once ${HOME}/.config/git-aliases is created
$ source "${HOME}/.config/git-aliases"
The alias commands must be run in your current shell process to have an effect.
cat ${ALIAS_FILE} | bash executes the alias commands in ALIAS_FILE in a new child process, not your shell, and not the program's shell.
source runs the commands in the current shell. You need to source the file from your current shell, not from the program. You can do this after the file is updated. In order to make this permanent, you will need to add source "${HOME}/.config/git-aliases" to your shell config.
What many programs like this do is print out the necessary commands at the end.
echo "$ALIAS_FILE updated"
echo "Make sure `source $ALIAS_FILE` is in your $HOME/.bash_profile"
echo "Run `source $ALIAS_FILE` to use it in your current shell"

Maven script shows that Java command is not executable

After installation on Ubuntu, mvn -version prints:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
I detected that the problem is in
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly" >&2
echo "This environment variable is needed to run this program" >&2
echo "NB: JAVA_HOME should point to a JDK not a JRE" >&2
exit 1
fi
When I remove quotes from "$JAVACMD" it works perfectly
I saw that other scripts use commands with in quotes and I doubt that Maven released this script with errors. So, what seems to be the problem? Why my script won't work in original version? Type of script is #!/bin/sh but I tested this condition separately in new file with bash script. The result is the same. When I ask with quotes is command executable, result is false. When I ask without quotes is true
Edit:
I put directly that case in new bash file. The result is interesting
#!/bin/bash
JAVACMD1="$JAVA_HOME/bin/java"
JAVACMD2="/usr/lib/jvm/java-8-openjdk-amd64/bin/java"
echo $JAVACMD1
echo $JAVACMD2
if [ ! -x "$JAVACMD1" ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x $JAVACMD1 ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x "$JAVACMD2" ]
then
echo "NOT"
else
echo "YES"
fi
Result:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
NOT
YES
YES
The problem was in env variable JAVA_HOME. It started with one blank, but that I didn't see in echo command (I suppose it trims the string). In command prompt I also used echo, but when I listed all env variables I saw it

script file not found when using source

I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.

Bash redirecting stdin as a standard input

In my script, sometimes I need a script to be passed to SSH, and sometimes let the user interact.
I tried this trick:
if [ "" != "$SSH_SCRIPT" ] ; then
...
else
SSH_SCRIPT="&0";
fi
And then:
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_#$HOST_ "$COMMAND_SSH" <$SSH_SCRIPT;
Which I assumed would work because cat <&0 works.
But when I use a variable after <, it works differently - it complains that file &0 was not found. Looks like <&n is a syntax construct. Reproducible this way:
$ FOO='&0';
$ cat <$FOO
bash: &0: No such file or directory
Is there a way to parametrize redirection by file descriptor number?
Enclosing the whole command to bash -c "..." isn't probably a good idea because it makes the whole thing more complicated.
Uploading the command to the remote server and running it there is also a bit overkill.
So far I have resorted to:
if [ "" == "$SSH_SCRIPT" ] ; then
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_#$HOST_ "$COMMAND_SSH";
else
ssh $SSH_IGNORE_STDIN $SSH_TTY $noKeyCheck -i $AWS_KEY $USER_#$HOST_ "$COMMAND_SSH" <$SSH_SCRIPT;
fi
bash will recognize /dev/stdin as the argument to a redirection whether or not your file system exposes such a file.
if [ -z "$SSH_SCRIPT" ]; then
SSH_SCRIPT=/dev/stdin
fi
ssh ... < "$SSH_SCRIPT"

Resources