Execute command in variable bash script - shell

I have this .sh script:
#!/bin/bash
CLASSPATH=$1
PACKAGE=$2
INPUT_FILE=$3
javac -classpath $CLASSPATH classes/$PACKAGE/$INPUT_FILE.java
cp classes/$PACKAGE/$INPUT_FILE.class ../WEB-INF/classes/$PACKAGE/
eval "$CATALINA"
And $CATALINA is set on .bashrc:
CATALINA_PATH="/var/local/tomcat/bin/catalina.sh"
CATALINA="sh $CATALINA_PATH stop && sh $CATALINA_PATH run"
But when I execute my .sh script it doesn't execute the command inside $CATALINA.
Am I doing something wrong?
Thanks!

Since I'm running a shell script, it uses a sub-shell so it cannot access the parent shell's environment.
Problem solved running the shell script in this way:
. ./script.sh

Related

Sbt external process can't process `eval` command

Running in sbt command "eval $(minikube docker-env)" !! log give exception.
[error] java.io.IOException: Cannot run program "eval": error=2, No such file or directory
but same command in bash script
#!/usr/bin/env bash
eval $(minikube docker-env)
Runn as "eval.sh" !! log
Work fine.
I can't understand why. Please explain.
eval is a shell feature. There is no way to call it from java to set up the environment for future commands the way you can for a shell.
If you want to run a second command from Java that depends on doing eval "$(minikube docker-env)" first, you can instead run a single shell with both commands:
String shellCommand = "eval \"$(minikube docker-env)\"; your-second-command";
Runtime.exec(new String[] { "sh", "-c", shellCommand });

Shell script run in telnet well, but not via cron

I just started to learn shells scripting.
Trying to run script in cron, without success. In telnet when i run "sh.script.sh" output is as expected. What I miss?
script.sh is in /usr/bin folder.
#!/bin/sh
var1 = $(opkg update)
echo ${var1}
try simply this:
#!/usr/bin/env bash
var1=$(opkg update)
echo $var1

How to source a bash-script within tcl (expect)

I want to set an http proxy within the bash environment (export http_proxy=xyz). So I added the command to the end of the .bash_profile and called
exec /bin/sh -c "source /path/to/.bash_profile"
But it does not work like expected: $::env(http_proxy) does not exist (but there is no typo).
I also tried to run the script like that: exec /bin/sh -c [exec cat /path/to/.bash_profile] .. but with the same result.
Saying
exec /bin/sh -c "source /path/to/.bash_profile"
would source the /path/to/.bash_profile in a subshell. So any changes made to the environment are effectively ignored when the command is done executing.
In order to pass an environment variable to a program, try:
exec /usr/bin/env http_proxy=xyz program

How to source a csh script from inside a bash script

My default shell is bash. I have set some environment variables in my .bashrc file.
I installed a program which use .cshrc file. It contains the path to several cshell scripts.
When I run the following commands in the shell windows it works perfectly :
exec csh
source .cshrc
exec bash
I have tried to put these commands in bash script, unfortunately it didn't work.
is there another way to write a script in order to get the same result as running commands from a shell windows.
I hope my question is now clear
Many thanks for any help
WARNING : don't put the following script in your .bashrc, it will reload bash and so reload .bashrc again and again (stopable with C-c anyway)
Use preferable this script in your kit/CDS stuff startup script. (cadence presumably)
WARNING 2 : if anything in your file2source fails, the whole 'trick' stops.
Call this script : cshWrapper.csh
#! /bin/csh
# to launch using
# exec cshWrapper.csh file2source.sh
source $1
exec $SHELL -i
and launch it using
exec ./cshWrapper.csh file2source.sh
it will : launch csh, source your file and came back to the same parrent bash shell
Example :
$> ps
PID TTY TIME CMD
7065 pts/0 00:00:02 bash
$>exec ./cshWrapper.csh toggle.csh
file sourced
1
$> echo $$
7065
where in my case i use the file toggle.csh
#! /bin/csh
# source ./toggle.csh
if ! $?TOGGLE then
setenv TOGGLE 0
endif
if ($?TOGGLE) then
echo 'file sourced'
if ($TOGGLE == 0) then
setenv TOGGLE 1
else
setenv TOGGLE 0
endif
endif
echo $TOGGLE
Hope it helps
New proposal, since I faced another problem with exec.
exec kills whatever remains in the script, except if you force a fork by using a pipe after it `exec script |cat'. In such case if you have environment variable in the script, they are not spread back to the script itself, which is not what we want. The only solution I found is to use 3 files (let's call them for the example : main.bash that call first.cshrc and second.sh).
#! /bin/bash
#_main.bash_
exec /bin/csh -c "source /path_to_file/cshrc; exec /bin/bash -i -c /path_to_file/second.sh"
# after exec nothing remains (like Attila the Hun)
# the rest of the script is in 'second.sh'
With that manner, i can launch in a single script call, an old cshrc design kit, and still process some bash command after, and finally launch the main program in bash (let say virtuoso)

Problem with bash script

I'm using this bash script:
for a in `sort -u $HADOOP_HOME/conf/slaves`; do
rsync -e ssh -a "${HADOOP_HOME}/conf" ${a}:"${HADOOP_HOME}"
done
for a in `sort -u $HBASE_HOME/conf/regionservers`; do
rsync -e ssh -a "${HBASE_HOME}/conf" ${a}:"${HBASE_HOME}"
done
When I call this script directly from shell, there are no problems and it works fine. But when I call this script from another script, although the script does its job, I get this message at the end:
sort: open failed: /conf/slaves: No such file or directory
sort: open failed: /conf/regionservers: No such file or directory
I have set $HADOOP_HOME and $HBASE_HOME in /etc/profile and the script does the job right. But I don't understand why it gives this message in the end.
Are you sure it's doing it right? When you call this script from the shell it is acting as an interactive shell which reads and sources /etc/profile and ~/.bash_profile if it exists. When you call it from another script it is running as non-interactive and wont source those files. If you want a non-interactive shell to source a file you can do this by setting the BASH_ENV environment variable.
#!/bin/bash
export BASH_ENV=/etc/profile
./call/to/your/HADOOP/script.sh
Everything points to those variables not being defined when your script runs.
You should ensure that they are set for your script. Before the first loop, place the line:
echo "[${HADOOP_HOME}] [${HBASE_HOME}]"
and make sure that doesn't output "[] []" (or even one "[]").
Additionally, put a set +x line at the top of the script - this will output lines before executing them and you can see what's being done.
Keep in mind that some shells don't pass on environment variables to subshells unless you explicitly export them (setting them is not enough).

Resources