Assign ON to a variable - windows

If I assign Boost_USE_STATIC_LIBS variable and then print it in Windows Command Prompt as follows:
set Boost_USE_STATIC_LIBS=ON
echo %Boost_USE_STATIC_LIBS%
an empty line is displayed.
And I get an error in some script that requires the variable to be exactly "ON":
* libboost_system-vc143-mt-s-x64-1_80.lib (static runtime,
Boost_USE_STATIC_RUNTIME not ON)
How to fix this?

It is set correctly, but "echo ON" is a shell command to turn echo mode on or off and has no output. Try any of the following:
set Boost_USE_STATIC_LIBS=ON
if %Boost_USE_STATIC_LIBS%==ON echo yes
echo it is %Boost_USE_STATIC_LIBS%
set Boost_USE_STATIC_LIBS
Output:
yes
it is ON
Boost_USE_STATIC_LIBS=ON

Related

how to get fully qualified directory in bash

This is the structure of my directory:
/rabbitProps, /app and/app/xyz
There is a bash script that i need to edit. In the bash script, there is a variable called BASEDIR and that has the value of "/app/xyz". I need to essentially concatenate the directory of /rabbitProps to a variable called CLASSPATH in the script but not sure how to do that. This is my if statement in the script:
amqDirectory=${BASEDIR}/../rabbitProps/
echo $amqDirectory
echo $BASEDIR
if [ -d "$amqDirectory" ]; then
echo "--SETTING rabbitProps PROPERTIES"
echo "In the if statement for setting rabbitProps"
echo $amqDirectory
CLASSPATH="${CLASSPATH}:${amqDirectory}"
else
echo "--NOT SETTING AMQ PROPERTIES"
echo "In the else if statement for setting amq_properties"
echo $amqDirectory
fi
echo $BASEDIR
echo $CLASSPATH
but it keeps on going to the else block. when i print out the amqDirectory variable, it has the value of /app/xyz/../rabbitProps/ ... which is probably why it's failing the if condition. Can someone correct the expression? I'm not really familiar with bash.
Change your line amqDirectory=${BASEDIR}/../rabbitProps/ to the following
amqDirectory=$(realpath "${BASEDIR}/../rabbitProps/")
Utility realpath gives the resolved path
If the utility is not available, you could do it this way
amqDirectory=$(cd "$BASEDIR/../rabbitProps/"; pwd)
Also, directory is not changed to $BASEDIR/../rabbitProps/ after executing above command as $(..) does the command substitution and it invokes sub-shell for executing commands

Looking for the existence of an ENV Variable

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 (!).

How do I use property defined with Jenkins Pipeline properties step in batch script in the same pipeline?

I have defined the properties using snippet generator and below is the portion from the jenkinsfile.
properties([[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false], parameters([string(defaultValue: 'SNAPSHOT', description: '', name: 'BUILD_TYPE'), string(defaultValue: '5.7.0', description: '', name: 'BRANCH_VERSION')]), pipelineTriggers([])])
When I echo the above defined properties using either "echo param" or "echo env.param" these parameters are correctly resolved.
Below are resolved correctly:
echo BUILD_TYPE
echo BRANCH_VERSION
echo env.BUILD_TYPE
echo env.BRANCH_VERSION
However, I need to use these parameters in batch script which is part of the same jenkinsfile, but these parameters are not resolved there. Below is batch portion from the jenkinsfile
bat '''echo off
echo "flag: env.BUILD_TYPE"
echo "flag: env.BRANCH_VERSION"'''
As the batch script is a Windows CMD Script, it must follow syntax for windows CMD.
ie. This means using correct variable expansion for environmental variables as handled by the CMD Interpreter.
CMD scripts expand the environmental variables by wrapping them in percent signs (or Exclamation Marks ! when you have Enabled Delayed Expansion).
eg: Jenkins sets Environmental Variable BUILD_TYPE CMD Expands this value using %BUILD_TYPE% (or !BUILD_TYPE! if you use delayed expansion in the cmd script)
If it was in a stand-alone CMD file it would look like this:
REM Script: Jenkinsbat.cmd
#(SETLOCAL
ECHO OFF
)
echo "flag: %BUILD_TYPE%"
echo "flag: %BRANCH_VERSION%"
(ENDLOCAL
Exit /b
)
And Jenkins would call it like this by referencing the full path of the script:
bat '''REM Call Script: Jenkinsbat.cmd
ECHO OFF
CALL "\\URL\Share\Path_to_Bat\Jenkinsbat.cmd"'''
Since your batch script is code sitting inside the Jenkins file it should look like this:
bat '''REM Script: Jenkinsbat.cmd
ECHO OFF
echo "flag: %BUILD_TYPE%"
echo "flag: %BRANCH_VERSION%"'''

Variable from another file echoed but not recognized in the script

I have a variable in a file called access.txt located at /home/ubuntu/pub/access.txt. The access.txt contents looks like this:
SFTP_VAR="JHGSYDDUIGUIGUIGUIG"
SQL_VAR="GUIIGGJHGBJHGJHGJH"
I have a script file on the same machine that is supposed to read and use this SFTP_VAR. I use the source statement to mention the location of access.txt. Here is the code on my script file.
#!/bin/bash
source /home/ubuntu/pub/access.txt
echo $SFTP_VAR
export SSHPASS=$SFTP_VAR
for f in /home/ubuntu/pub/sfmc/Upstream/Encrypted/* ;
do
echo put "$f"
done | sshpass -e sftp -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss USERNAME#FTP_SERVER_IP:/Import
unset SSHPASS
When I run my script, I see the variable in the first echo. This means my script can see the file. But the export command immediately after the first echo does not seem to recognize my variable. This is when I replace the variable with its real value or:
echo 'JHGSYDDUIGUIGUIGUIG'
export SSHPASS='JHGSYDDUIGUIGUIGUIG'
The code works fine. What am I missing here and how can I read those variables in the code?
Edit: During further discussion in the comments it was found to be a problem with the quotes and not with sessions. Changing the double quotes to single quotes in the access.txt file solved it for OP.
I will leave my original answer below.
Your question is very poorly worded. I think I see your problem, but I am not sure.
It seems you might have some misunderstandings of how source and export work and how they affect your session. Lets test this:
First create a file with this content and give the path at line 6 of the script:
VAR1="value of VAR1"
VAR2="value of VAR2"
Then create this script called script.sh:
#!/bin/bash
echo "[The variables with the name VAR1 and VAR2 are undefined in out session]"
echo "VAR1: "$VAR1
echo "VAR2: "$VAR2
echo "[Source brings the variables of the file into our session, with them VAR1 and VAR2]"
source /home/X/access.txt
echo "[VAR1 and VAR2 of our session do now have the values specified in the file]"
echo "VAR1: "$VAR1
echo "VAR2: "$VAR2
echo "[A third undefined variable is used - empty]"
echo "VAR3: "$VAR3
echo "[The third variable gets the value of VAR1 and is exported]"
export VAR3=$VAR1
echo "[The third variale is now filled]"
echo "VAR3: "$VAR3
echo "[We unset the variable]"
unset VAR3
echo "[The variable is no longer set]"
echo "VAR3: "$VAR3
echo "[Set it again]"
export VAR3=$VAR1
echo "[Its back now]"
echo "VAR3: "$VAR3
I think it covers everything you want to do. Now on to testing:
First step:
Just use a normal call on itbash script.sh. Then look at the stdout. Everything should be there as is described by the echos.
Second step:
Now try echo $VAR3 from your shell where you previously called the script. It should not print anything. The variables did not hold their values after the script completed.
Third step:
Now call the script again with export script.sh. Output should look just the way it did in the first step.
Fourth step:
Now try echo $VAR3 again. It will now print the variable.
Why is that?
Bash is based on different sessions. Things you set after you started your session will not carry over to other open sessions and will be lost after you exit the session.
You can start new sessions by simply typing bash. But even this does not solve our problem.
When you call your script you use bash script.sh. This actually also starts a new session and then executes your script inside of this session. Not in the one you called it from.
After finishing it exits out of this session leaving behind the set variables. This is why you cant see them by simple using bash script.sh
In the third step we used source to call the script. This will get the variables into our session and thus we can print the values out.
But this still makes them only temporary as they will go away once we close the session where we sourced the script.
Put things in double quotes. Your example SFTP_VAR="JHGSYDDUIGUIGUIGUIG" should work fine, but try the password "*".
# Incorrect
SFTP_VAR="*"; export SSHPASS=$SFTP_VAR; echo $SSHPASS
# Correct
SFTP_VAR="*"; export SSHPASS="$SFTP_VAR"; echo "$SSHPASS"

Introduction to batch scripting

This is my shell script
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "correct name"
else
echo "wrong name"
fi
echo "Password"
read password
if [ "$password" == "pwd" ]; then
echo "Correct password"
else
echo "Wrong password"
fi
echo "City"
read city
if [ "$city" == "bangalore" ]; then
echo "correct city"
else
echo "wrong city"
fi
I'm totally new to batch scripting.How do i write an equivalent .bat file?
There are two things you need to read up on:
How to prompt for user input: see the set command and especially set /p. You can get information about this by typing help set at the prompt.
How to test string equality: The if command has some info on this, type help if at the prompt to get some details.
When you have these things in place, you should be able to replicate the behavior of your bash script.
Have a look here for how to get input:
Batch File Input
Edit: Link contents (no longer available) was:
The following method will work on Windows 2000 and XP:
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
If user hits ENTER without typing anything, the variable (in this case, %input%) keeps it value. So, the first line is to reset its value, so that if nothing is entered the variable will have no value at all, instead of some senseless value.
As you can see, that accepts blank inputs. You can make it to don't accept:
:input
set INPUT=
set /P INPUT=Type input: %=%
if "%INPUT%"=="" goto input
echo Your input was: %INPUT%
So, after getting user's input and saving it in a variable, you can use it as you will.
Consider running bash script on windows instead of porting script. See bash-shell-for-windows. It might be easier approach.

Resources