Replacemet of a variable in shell script from another file - preferably python - bash

My shell script:
#!/bin/sh
.....
a='30'
b='2'
c='0.9'
.....
Is there a way to replace the values of my variables using python script/any other language? I should be able to replace the values with multiple values also. For example, I should be able to set b = '2 1 3' and a = '0.9 0.8' ..

It's simple: add a line to the top of your Bash script:
source thevars.sh
Then you can write all the variable assignments you require using a trivial Python script to create thevars.sh.
The Bash script can be modified slightly to have default arguments in case thevars.sh does not exist:
test -f thevars.sh && source thevars.sh
a=${a-30}
b=${b-2
c=${c-0.9}
Then if thevars.sh has any variables assigned, they will be used instead of the defaults--for example:
a="foo bar"
b=9001

Related

CMD to bash script conversion semicolumn

I am trying to convert a CMD script (.bat) into a .sh script on Linux side.
I did not find a proper documentation for instance for the following lines
set PATH="${PATH1}%;${PATH_NAME};"
another_script.bat -create "%LocalDestination%TEST;%LocalDestination%" -e %GenericEnvironementName% -d "%SettingsPath%/Env"
For the first one it is an export but I do not know it is like the if condition?
${PATH1}=${PATH_NAME}
export PATH=$PATH1
for the second one the expression
"%LocalDestination%TEST;%LocalDestination%" it's like an assignement? why we put the % at the end?
$LocalDestination$TEST = $LocalDestination
%GenericEnvironementName% will be $GenericEnvironementName
%SettingsPath%/Env >>> $SettingsPath/Env?
Variables in dos bat files are delimited with %, before AND after. So %VAR% is replaced by the value of VAR.
set PATH="${PATH1};${PATH_NAME};" assigns the values of PATH1 and PATH_NAME to variable PATH, separated by ;.
In Bash you would write: export PATH="$PATH1;$PATH_NAME"
Therefore, yes, any variable referencing is bash is done with $ before the variable name. So %TATA% becomes $TATA.
Example: %SettingsPath%/Env --> ${SettingsPath}/Env

How to output variables and values defined in a shell file

I want to print out all the variables defined in the file (not environment variables), so that I can quickly locate the error. I thought of printing through echo, but this is not friendly, is there any easy way to achieve this?
For example is as follow:
var1=${VAR1:-"test1"}
var2=${VAR2:-"test2"}
var3=${VAR1:-"test3"}
var4=${VAR1:-"test4"}
print like below:
var1=test1
var2=modify // modified by environment var
var3=test3
var4=test4
I really appreciate any help with this.
In Bash you can:
# List all variables to a file named before
declare -p > before
# source the file
. the_file
# list all variables to a file named after
declare -p > after
# difference between variables before and after sourcing the file
diff before after
You can manipulate with env -i bash -c to get a clean environment.
The other way is just to write a parser for your file. Simple sed 's/=.*//' the_file will give you a list of all variable definitions.

How to source a variable list pulled using sqlplus in bash without creating a file

Im trying to source a variable list which is populated into one single variable in bash.
I then want to source this single variable to the contents (which are other variables) of the variable are available to the script.
I want to achieve this without having to spool the sqlplus file then source this file (this already works as I tried it).
Please find below what Im trying:
#!/bin/bash
var_list=$(sqlplus -S /#mydatabase << EOF
set pagesize 0
set trimspool on
set headsep off
set echo off
set feedback off
set linesize 1000
set verify off
set termout off
select varlist from table;
EOF
)
#This already works when I echo any variable from the list
#echo "$var_list" > var_list.dat
#. var_list.dat
#echo "$var1, $var2, $var3"
#Im trying to achieve the following
. $(echo "var_list")
echo "$any_variable_from_var_list"
The contents of var_list from the database are as follows:
var1="Test1"
var2="Test2"
var3="Test3"
I also tried sourcing it other ways such as:
. <<< $(echo "$var_list")
. $(cat "$var_list")
Im not sure if I need to read in each line now using a while loop.
Any advice is appreciated.
You can:
. /dev/stdin <<<"$varlist"
<<< is a here string. It redirects the content of data behind <<< to standard input.
/dev/stdin represents standard input. So reading from the 0 file descriptor is like opening /dev/stdin and calling read() on resulting file descriptor.
Because source command needs a filename, we pass to is /dev/stdin and redirect the data to be read to standard input. That way source reads the commands from standard input thinking it's reading from file, while we pass our data to the input that we want to pass.
Using /dev/stdin for tools that expect a file is quite common. I have no idea what references to give, I'll link: bash manual here strings, Posix 7 base definitions 2.1.1p4 last bullet point, linux kernel documentation on /dev/ directory entires, bash manual shell builtins, maybe C99 7.19.3p7.
I needed a way to store dotenv values in files locally and vars for DevOps pipelines, so I could then source to the runtime environment on demand (from file when available and vars when not). More though, I needed to store different dotenv sets in different vars and use them based on the source branch (which I load to $ENV in .gitlab-ci.yml via export ENV=${CI_COMMIT_BRANCH:=develop}). With this I'll have developEnv, qaEnv, and productionEnv, each being a var containing it's appropriate dotenv contents (being redundant to be clear.)
unset FOO; # Clear so we can confirm loading after
ENV=develop; #
developEnv="VERSION=1.2.3
FOO=bar"; # Creating a simple dotenv in a var, with linebreak (as the files passed through will have)
envVarName=${ENV}Env # Our dynamic var-name
source <(cat <<< "${!envVarName}") # Using the dynamic name,
echo $FOO;
# bar

How to declare a new variable inside a shell block in groovy script?

For the life of me, I can't figure out how to declare and use new variables inside a shell block in a groovy script.
For example, this shell block -
sh """
export earlist='abc.ear,def.ear'
echo $earlist;
"""
throws an error saying
No such property: earlist for class: GroovyUserScript
If I add a def earlist before the sh, then it throws error saying -
No signature of method: GroovyUserScript.sh() is applicable for
argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [
export earlist='abc.ear,def.ear' echo ;
Can someone please help me with how to declare and then use variable inside a shell block, in a groovy script?
After consulting with senior experts at my workplace, I found the solution I was looking for.
The problem with this code -
sh """
export earlist='abc.ear,def.ear'
echo $earlist;
"""
is that when I say $earlist, the compiler looks for a groovy variable named earlist and doesn't find it. Since earlist there is a shell variable, I need to escape the $. So, the correct code is -
sh """
earlist='abc.ear,def.ear'
echo \$earlist;
"""
Bonus TIL - if I access a groovy variable inside a shell block, the access is Read-Only. I can't edit the value of the groovy variable, even temporarily within the shell block. If I do want to do that, I can assign the groovy variable to a shell variable, manipulate the shell variable value, save the modified value in a file and when the shell block ends, read the file into the original groovy variable.
Use a triple single quoted string instead, which doesn't interpolate variables:
sh '''
export earlist='abc.ear,def.ear'
echo $earlist;
'''
See here the documentation on triple single quoted strings: http://groovy-lang.org/syntax.html#_triple_single_quoted_string
Here a overview of the available string types in Groovy: http://groovy-lang.org/syntax.html#_string_summary_table

How Can I access bash variables in tcl(expect) script

How Can I access bash variables in tcl(expect) script.
I have bash file say f1.sh which set some variables like
export var1=a1
export var2=a2
These variable I need to use in my expect script .
I tried using this in my script which does not work
system "./f1.sh"
puts "var1 is $::env(var1)"
puts "var2 is $::env(var2)"
But this does not seems to work.
I see that non of the variable from f1.sh are getting set as environment variable.
system "./f1.sh" << # Is this command in my script right ?
How I need to access these bash variables from tcl file.
I would say that this problem is rather general. First I met this problem, when I wanted to initialize Microsoft Visual Studio environment (which is done using .cmd script) in PoserShell. Later I've faced this problem with other scripting languages in any combinations (Bash, Tcl, Python etc.).
Solution provided by Hai Vu is good. It works well, if you know from the beginning, which variables you need. However, if you are going to use script for initialization of some environment it my contains dozens of variables (which you don't even need to know about, but which are needed for normal operation of the environment).
In general, the solution for the problem is following:
Execute script and at the end print ALL environment variables and capture the output.
Match lines of output for the pattern like "variable=value", where is what you want to get.
Set environment variables using facilities of your language.
I do not have ready made solution, but I guess, that something similar to this should work (note, that snippets below was not tested - they are aimed only to give an idea of the solution):
Execute script; print vars and capture the output (argument expanding - {*} - requires Tcl 8.5, here we can go without it, but I prefer to use it):
set bashCommand {bash -c 'myScriptName arg1 arg2 2>&1 >/dev/null && export -p'}
if [catch {*}${bashCommand} output] {
set errMsg "ERROR: Failed to run script."
append errMsg "\n" $output
error $errMsg
}
;# If we get here, output contains the output of "export -p" command
Parse the output of the command:
set vars [dict create]
foreach line [split $output "\n"] {
regex -- {^declare -x ([[:alpha:]_]*)=\"(.*)\"$} $line dummy var val
;# 3. Store var-val pair of set env var.
}
Store var-val pair or set env var. Here several approaches can be used:
3.1. Set Tcl variables and use them like this (depending on context):
set $var $val
or
variable $var $val
3.2. Set environment variable (actually, sub-case of 3.1):
global ::env
set ::env($var) $val
3.3 Set dict or array and use it within your application (or script) without modification of global environment:
set myEnv($var) val ;# set array
dict set myEnvDict $var $val ;# set dict
I'd like to repeat, that this is only the idea of the receipt. And more important, that as most of the modern scripting languages support regexes, this receipt can provide bridge between almost arbitrary pair of languages, but not only Bash<->Tcl
You can use a here-document, like this:
#!/bin/bash
process=ssh
expect <<EOF
spawn $process
...
EOF
Exported variables are only passed from a parent process to it's children, not the other way around. The script f1.sh (actually the bash instance that's running the script) gets it's own copies of var1 and var2 and it doesn't matter if it changes them, the changes are lost when it exits. For variable exporting to work, you would need to start the expect script from the bash script.
In f1.sh, printf what you want to return...
printf '%s\n%s\n' "$var1" "$var2"
...and read it with exec in Tcl:
lassign [split [exec ./f1.sh] \n] var1 var2
Perhaps I did not look hard enough, but I don't see any way to do this. When you execute the bash script, you create a different process. What happens in that process does not propagate back to the current process.
We can work-around this issue by doing the following (thanks to potrzebie for the idea):
Duplicate the bash script to a temp script
Append to the temp script some commands at the end to echo a marker, and a list of variables and their values
Execute the temp script and parse the output
The result is a list of alternating names and values. We use this list to set the environment variables for our process.
#!/usr/bin/env tclsh
package require fileutil
# Execute a bash script and extract some environment variables
proc getBashVar {bashScript varsList} {
# Duplicate the bash script to a temp script
set tempScriptName [fileutil::tempfile getBashVar]
file copy -force $bashScript $tempScriptName
# Append a marker to the end of the script. We need this marker to
# identify where in the output to begin extracting the variables.
# After that append the list of specified varibles and their values.
set f [open $tempScriptName a]
set marker "#XXX-MARKER"
puts $f "\necho \\$marker"
foreach var $varsList {
puts $f "echo $var \\\"$$var\\\" "
}
close $f
# Execute the temp script and parse the output
set scriptOutput [exec bash $tempScriptName]
append pattern $marker {\s*(.*)}
regexp $pattern $scriptOutput all vars
# Set the environment
array set ::env $vars
# Finally, delete the temp script to clean up
file delete $tempScriptName
}
# Test
getBashVar f1.sh {var1 var2}
puts "var1 = $::env(var1)"
puts "var2 = $::env(var2)"

Resources