Missing environment parameter in shell script - macos

I have problem to launch the following shell script. I
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Missing environment parameter"
exit 1
fi
environment=$1
npm=/opt/plesk/node/8/bin/npm
gulp=/opt/plesk/node/8/bin/gulp
grunt=/opt/plesk/node/8/bin/grunt
composer=/usr/bin/composer
home=$(pwd)
logs=${home}/logs
echo "home dir: ${home}"
echo "logs dir: ${logs}"
echo "environment: ${environment}"
...more code follow
From terminal I got the following error:
Missing environment parameter
I launch the following command from terminal.
./build.sh
Which parameter should I enter in my command?

Related

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

Sourcing a bash script and assigning a value to variable via backtick execution

I have a strange problem with bash that I cannot explain.
I created the script test.sh which contains the lines:
export TEST_HOME=`ls -d $HOME`
echo $TEST_HOME
if [ "$HOME" = "$TEST_HOME" ]
then
echo "Equal"
else
echo "Not equal"
fi
Now if I source test.sh:
$ . test.sh
the output is
/home/sven
Not equal
However, if I make it executable and run it as a normal script:
$ test.sh
the output is
/home/sven
Equal
Any explanation how this behavior is explained? I am using RedHat 5.
Best regards,
Sven

How to execute a script from within another script?

I'm having trouble executing some files under a BASH script.
What I want is, when running the SCRIPT.SH, to check if the directory from which it's running is the right one. Which in this case it's /ROOT/OPENSOURCE . If it's not, then it asks if the user wants to move the directory into the correct place. Doing this by another script /OPENSOURCE/MODULES/MOVE.SH.
I have this variable to get the script launching dir:
spath="$( cd "$( dirname $0 )" && pwd )"
Now since the script will not be installed on the right directory, I need to run MOVE.SH which is in the MODULES directory inside OPENSOURCE. I can't get this done.
Code:
# check if script is on correct directory
if [ "$rpath" = "$spath" ]; then
Colors;
echo ${RedF}[x] [WAIT]${YellowF} Checking directory: ${Reset};
sleep 1
echo ${BlueF}[*] [Directory]:${GreenF} OK ${Reset};
else
Colors;
echo ${RedF}[x] [WAIT]${YellowF} Checking directory: ${Reset};
sleep 1
echo ${BlueF}[*] [Directory]:${RedF} FAILED: This may cause a script malfunction ${Reset};
read -p "[!] Move script directory to right directory {y|n}:" pass
if test "$pass" = "y"
then
echo ${BlueF}[*] [Directory]: ${GreenF}Ok. Moving script to new directory${Reset};
sleep 1
---- COMMAND WILL BE HERE ----
else
echo ${BlueF}[*] [Directory]: ${YellowF}Ok not moving ${Reset};
fi
fi
How can I do it ?
I'm not sure I 100% understand the question, but I think you might just be looking for the mysterious "." command, which will run another script.
Example:
test.sh:
#!/bin/bash
echo running other script $1
. $1
echo done
test2.sh:
#!/bin/bash
echo I am the other script
Run it:
> ./test.sh test2.sh
running other script test2.sh
I am the other script
done

LOCAL_DIR variable prepends the scripts current directory (totally not what I expect)

Consider the following simple rsync script I am tryint to slap up:
#!/bin/bash
PROJECT="$1"
USER=stef
LOCAL_DIR="~/drupal-files/"
REMOTE_HOST="hostname.com"
REMOTE_PROJECTS_PATH=""
# Should not have anything to change below
PROJECT_LIST="proj1 proj2 proj3 quit"
echo "/nSelect project you wish to rsync\n\n"
select PROJECT in $PROJECT_LIST
do
if [ "$PROJECT" = "quit" ]; then
echo
echo "Quitting $0"
echo
exit
fi
echo "Rsynching $PROJECT from $REMOTE_HOST into" $LOCAL_DIR$PROJECT
rsync -avzrvP $USER#$REMOTE_HOST:/var/projects/$PROJECT/ $LOCAL_DIR$PROJECT
done
echo "Rsync complete."
exit;
The variable $LOCALDIR$PROJECT set in the rsync command always includes the scripts path, :
OUTPUT:
Rsynching casa from hostname.com.com into ~/drupal-files/casa
opening connection using: ssh -l stef hostname.com rsync --server --sender -vvlogDtprz e.iLsf . /var/groupe_tva/casa/
receiving incremental file list
rsync: mkdir "/home/stef/bin/~/drupal-files/proj1" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(605) [Receiver=3.0.9]
The line with mkdir should not have /home/stef/bin, why is bash adding the script's running dir on the variable?
Thanks
LOCAL_DIR="~/drupal-files/"
The string is in quotes so there's pathname expansion, and the variable will contain the literal string.
Remove the quotes.
$ x="~/test"; echo $x
~/test
$ x=~/test; echo $x
/home/user/test

Printing different messages by command exit code

So I have this in my .sh file:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
make OS=APPLE -k
if [$? -eq 0]
then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi
The problem is, bash prints this message I don't even understand:
./build-mac.sh: line 7: [0: command not found
Any help or advice is appreciated!
I'm running on a Mac with Bash.
You need to add additional spaces on the if line around the square brackets. Change your code to:
if [ $? -eq 0 ]
The reason this is required is that [ is a command itself (a synonym on *nix for test) and you need to execute the [ command not the (non-existing) [$? command.
Might as well not use $? at all:
#!/bin/bash
echo "Building Space Cubes X for Mac...."
if make OS=APPLE -k ; then
echo "Build completed."
echo "You can find the build under (THIS_FOLDER)/bin/build."
else
echo "Build failed! Check above for error messages!"
fi

Resources