I'm trying to use the following function definition in Bash for setting session-wise git-environment variables. If I source this, my Bash blocks, I can only kil it with CTRL+D and then I got a unusable shell back (no commands possible). I guess, something with the read is blocking. The original version even had a "while true; do" in it, but that was even worse wreaking havoc the CPU.
So, two questions arise:
How can I reach the goal of having this function working properly?
Why does the problem pop up when only defining the function, but not calling it?
function git() {
echo "Running BETTER git..."
if [ -z "$GIT_COMMITTER_NAME" ]; then
echo -n "Git User: "
read -e UNAME
if [ "$UNAME" == "user1" ] -o [ "$UNAME" == "user2" ]; then
echo "Hello $UNAME, you will be set as committer for this git session"
else
echo "Invalid User"
fi
GIT_COMMITTER_NAME=$UNAME
export GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL=$UNAME#company.com
export GIT_COMMITTER_EMAIL
GIT_AUTHOR_NAME=$GIT_COMMITTER_NAME
export GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL=$GIT_COMMITTER_EMAIL
export GIT_AUTHOR_EMAIL
fi
echo " using git user: $GIT_AUTHOR_NAME / $GIT_AUTHOR_EMAIL"
/usr/bin/git "$#"
}
Just asking the 2nd question has brought myself to the problem's root: Why is just the definition of the function causing troubles? Because git actually is called in my environment:
Changing
export PS1=$Color_Off'$(git branch &>/dev/null;\
into
export PS1=$Color_Off'$(/usr/bin/git branch &>/dev/null;\
is solving the issue.
$PS1 is evaluated on every command in the bash, so on every prompt, a git command is issued, thus calling my defined function.
EDIT: I also had to replace all occurrences of "git " with "/usr/bin/git " in my /etc/bash_completion.d/git-prompt. Now I can alias "git" with that function and it works properly.
Related
I want to call a program when any SSH user logs in that prints a welcome message. I did this by editing the /etc/ssh/sshrc file:
#!/bin/bash
ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
echo $USER logged in from $ip
For simplicity, I replaced the program call with a simple echo command in the example
The problem is, I learned SCP is sensitive to any script that prints to stdout in .bashrc or, apparently, sshrc. My SCP commands failed silently. This was confirmed here: https://stackoverflow.com/a/12442753/2887850
Lots of solutions offered quick ways to check if the user is in an interactive terminal:
if [[ $- != *i* ]]; then return; fi link
Fails becase [ is not linked
case $- in *i* link
Fails because in is not recognized?
Use tty program (same as above)
tty gave me a bizarre error code when executed from sshrc
While all of those solutions could work in a normal BASH environment, none of them work in the sshrc file. I believe that is because PATH (and I suspect a few other things) aren't actually available when executing from sshrc, despite specifying BASH with a shebang. I'm not really sure why this is the case, but this link is what tipped me off to the fact that sshrc is running in a limited environment.
So the question becomes: is there a way to detect interactive terminal in the limited environment that sshrc executes in?
Use test to check $SSH_TTY (final solution in this link):
test -z $SSH_TTY || echo $USER logged in from $ip
So, I'm guessing this may be a bug, or maybe I've botched something up, I dunno...
I have a script that was always working but I've been tasked to make it work using the new WSL2. I've snipped out the first block of code as it's giving me issues right off the bat. As you'll see below, I'm simply trying to determine if a variable has been set or not. This works in my Linux VM and also in Cygwin, however, it doesn't work in WSL2. Here is the code:
#!/bin/bash
echo $test_var
set -e
if [ ! -d "$test_var" ]; then
echo Please set test_var.
exit
fi
When I run this in any of the working systems I get the output of the variable.
In WSL2 I get the output of the variable followed by Please set test_var. And it stops the script due to the set -e as it's supposed to do thinking the var isn't set.
Any help would be appreciated.
Thanks
If your intention is to check if a directory exists (whose name apparently needs to be set as an environment variable in $test_var), if that directory exists relative to the current directory where the script is executed, you want something like:
#!/bin/bash
echo "$test_var"
set -e
if [ ! -d "$test_var" ]; then
echo "cannot find directory $test_var"
exit
fi
Note that here I have only changed your message, and your problem might possibly be explained by the fact that you do not have such a directory under WSL2.
If, on the other hand, you want to check if some environment variable (whatever it represents) is set, you want the -z option, something like:
#!/bin/bash
echo "$test_var"
set -e
if [ -z "$test_var" ]; then
echo "Please set test_var."
exit
fi
Note the absence of your negation sign (!).
What is the best way to make a "First run" notification in a bash script?
I tried putting export FIRSTRUN="no" first,
and later on put
`if [ -z "$FIRSTRUN" ]; then
echo "It seems that this may be your first time running this script.";
echo "Please ${GREEN}test whether the needed components are installed${RESET}.";
echo "";
fi`
but it simply does it on every time the script is run. If i put the export FIRSTRUN="no" after the if part, it never runs the if part of the code. So that doesn't work.
I am new at this so please help :D
You can't modify the parent shell state from within your script, so attempting to export a variable will not work.
You could use a config file to store a variable which determines whether the script has been run already:
#!/bin/bash
# load vars from config at the start of the script
if [[ -f ~/.your_script.conf ]]; then
. ~/.your_script.conf
fi
# check whether var has been set to determine first run
if [[ -z $has_run ]]; then
echo 'first run'
# set variable in config file for next time
echo 'has_run=1' >> ~/.your_script.conf
fi
Alternatively, if this is the only "config" you have, you could just create a file after the first run and check for its existence to determine whether the script has been run.
The first run of the script should leave behind some sort of permanent evidence that it has run; it can then look for that file on startup.
do_init () {
# initialize whatever needs initializing
touch ~/.alreadyran
}
if ! [ -f ~/.alreadyran ]; then
echo "First time running."
do_init
fi
If the initialization isn't per-user, it will need to leave the file in a more public place, such as somewhere under /var.
Quick run down of what I'm doing, I'm making python development environments on my computer using virtualenv. I'm not one to remember all these commands, so I like to build simple(noob-ish) script that will help me.
Problem
As you see on step 2 in the Basic Usage section of the documentation. I need to run a command:
$ source venv/bin/activate
This activates my python environment. So I've tried a mixture of functions and eval. As mentioned, I am a bit of a noob when it comes to bash scripts.
My Code
File: fpyenv
#!/bin/bash
# $ cd ~/scripts/
# $ chmod u+x <filename>.sh
activateSrc(){
eval '$(source "${1}"/bin/activate)'
}
if [[ $1 == '' ]];
then
printf "ERROR: Missing Filename\n"
else
printf "Creating Virtual Environment $1\n"
# This creates the environment
virtualenv $1
printf "Do you want to activate $1 as your virtual environment?(y/n)\n"
# Get answer from user
read answer
if [[ $answer != 'y' ]];
# Said No
then
printf "Did not set $1 as your virtual environment\n"
else
# Said Yes
activateSrc $1
printf "Set $1 as your virtual environment\n"
fi
fi
This is what it should look like:
Step 1
myComputer $ fpyenv venv
returns
Creating Virtual Environment venv
Do you want to activate venv as your virtual environment?(y/n)
Step 2(user inputs y/n)
y
returns
Set venv as your virtual environment
(venv)myComputer $
But what I'm getting is:
Set venv as your virtual environment
myComputer $
So it doesn't run the source venv/bin/activate. Does anyone know what I'm doing wrong? I've looked through many answers, but given that source is commonly used in a different way in bash scripts, the answers I'm getting are no help. Thank you in advance!
FIX:
Change activateSrc to:
activateSrc(){
source $1/bin/activate
}
And execute script like this:
myComputer $ . fpyenv venv
It runs source just as written. The thing is that you need to source this new script as well, otherwise it just runs in a subshell and any changes made by the sourced script are lost when the subshell exits.
I have a bash script which calls another bash script, like so:
#!/bin/bash
echo "Hi"
./script-two.sh
echo "Hello!"
The problem that I have is that it never makes it to printing "Hello!"
I think this is because ./script-two.sh (Which I did not write) is somehow exiting or changing the shell. I have included this script at the end of this post.
Is there a way I can gurentee that my execution will continue after script-two.sh executes?
I have looked into using the trap command, but I don't fully understand its use properly.
Thanks,
Casey
Here is the contents of what would be script-two.sh
#!/bin/sh
# This file is part of the DITA Open Toolkit project hosted on
# Sourceforge.net. See the accompanying license.txt file for
# applicable licenses.
# (c) Copyright IBM Corp. 2006 All Rights Reserved.
export DITA_HOME=cwd
if [ "${DITA_HOME:+1}" != "1" ]; then
echo "DITA_HOME environment variable is empty or not set";
exit 127;
fi
echo $DITA_HOME
cd "$DITA_HOME"
# Get the absolute path of DITAOT's home directory
DITA_DIR="`pwd`"
echo $DITA_DIR
if [ -f "$DITA_DIR"/tools/ant/bin/ant ] && [ ! -x "$DITA_DIR"/tools/ant/bin/ant ]; then
chmod +x "$DITA_DIR"/tools/ant/bin/ant
fi
export ANT_OPTS="-Xmx512m $ANT_OPTS"
export ANT_OPTS="$ANT_OPTS -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl"
export ANT_HOME="$DITA_DIR"/tools/ant
export PATH="$DITA_DIR"/tools/ant/bin:"$PATH"
NEW_CLASSPATH="$DITA_DIR/lib:$DITA_DIR/lib/dost.jar:$DITA_DIR/lib/commons-codec-1.4.jar:$DITA_DIR/lib/resolver.jar:$DITA_DIR/lib/icu4j.jar"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9.jar:$DITA_DIR/lib/saxon/saxon9-dom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-dom4j.jar:$DITA_DIR/lib/saxon/saxon9-jdom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-s9api.jar:$DITA_DIR/lib/saxon/saxon9-sql.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-xom.jar:$DITA_DIR/lib/saxon/saxon9-xpath.jar:$DITA_DIR/lib/saxon/saxon9-xqj.jar:$NEW_CLASSPATH"
if test -n "$CLASSPATH"
then
export CLASSPATH="$NEW_CLASSPATH":"$CLASSPATH"
else
export CLASSPATH="$NEW_CLASSPATH"
fi
"$SHELL"
It looks like script-two.sh is setting up an ant build environment.
I think the author intended that it sets up the build environment, then you type your build commands in manually, then type exit to leave the build environment.
I say this because the bottom line of script-two.sh is:
"$SHELL"
which starts a new shell.
Try running your script, then type exit. I think you will see it print Hello! after you type exit.
I'm guessing you're trying to do something like:
#!/bin/bash
echo "Hi"
./script-two.sh
ant <some args>
To do that, what you really want to do is source it, by changing:
./script-two.sh
to
. script-two.sh
e.g.
#!/bin/bash
echo "Hi"
. script-two.sh
ant <some args>
But, you will need to edit script-two.sh and change:
"$SHELL"
to:
case $0 in *script-two.sh)
# executed, start a new shell with the new environment
"$SHELL"
;;
*)
# sourced, don't start a new shell
;;
esac
so that it only starts a shell if the script is being run like ./script-two.sh, but not if it is being sourced like . script-two.sh.
Or if you absolutely can't change script-two.sh, then you could do:
#!/bin/bash
echo "Hi"
. script-two.sh </dev/null
ant <some args>
which will trick "$SHELL" into exiting because it has no input.
Also
export DITA_HOME=cwd
doesn't seem right to me.
It should probably be
export DITA_HOME=$(pwd)
or
export DITA_HOME=`pwd`
(both are equivalent)
I had a similar problem today, up on digging I finally found the answer.
The script I was calling (from within my script) actually had an exit 0 in the end. Removing that fixed my issues.
Just leaving this here as someone may find it useful.
Well for starters, you can execute your bash script with the -x switch to see where it is failing:
bash -x script-one.sh
Secondly, if you call the second script like this:
#!/bin/bash
echo "Hi"
var=$(bash script-two.sh)
echo "Hello!"
It will continue, as long as script-two.sh exits cleanly. Again, you can run the -x script against that script find any problems.
And as Mikel mentioned, always make sure to have exit at the bottom of your scripts.