Use variable in list expect script - expect

In expect script, I have snippet code like
set log_path "/var/log/"
set opt_path "/opt/"
set path_list {$log_path $opt_path}
list $path_list
puts $path_list # ==> $log_path $opt_path
It always prints $log_path $opt_path
How can I make it prints /var/log /opt/?

In Tcl, variable substitution will not happen inside braces (though there are some exceptions). Please refer here. You should use double quotes so that the variable interpolation can happen.
set path_list "$log_path $opt_path"
puts $path_list

If you want path_list to be created/treated as a list, you may use list instead of {} to initialize:
Code:
set log_path "/var/log/"
set opt_path "/opt/"
set path_list [list $log_path $opt_path]
puts $path_list
Execution output:
/var/log/ /opt/

I used this one and it works
set path_list [list $log_path $opt_path]
Thank you so much.

Related

How to call a variable created in the script in Nextflow?

I have a nextflow script that creates a variable from a text file, and I need to pass the value of that variable to a command line order (which is a bioconda package). Those two processes happen inside the "script" part. I have tried to call the variable using the '$' symbol without any results, I think because using that symbol in the script part of a nextflow script is for calling variables defined in the input part.
To make myself clearer, here is a code sample of what I'm trying to achieve:
params.gz_file = '/path/to/file.gz'
params.fa_file = '/path/to/file.fa'
params.output_dir = '/path/to/outdir'
input_file = file(params.gz_file)
fasta_file = file(params.fa_file)
process foo {
//publishDir "${params.output_dir}", mode: 'copy',
input:
path file from input_file
path fasta from fasta_file
output:
file ("*.html")
script:
"""
echo 123 > number.txt
parameter=`cat number.txt`
create_report $file $fasta --flanking $parameter
"""
}
By doig this the error I recieve is:
Error executing process > 'foo'
Caused by:
Unknown variable 'parameter' -- Make sure it is not misspelt and defined somewhere in the script before using it
Is there any way to call the variable parameter inside the script without Nextflow interpreting it as an input file? Thanks in advance!
The documentation re the script block is useful here:
Since Nextflow uses the same Bash syntax for variable substitutions in
strings, you need to manage them carefully depending on if you want to
evaluate a variable in the Nextflow context - or - in the Bash
environment execution.
One solution is to escape your shell (Bash) variables by prefixing them with a back-slash (\) character, like in the following example:
process foo {
script:
"""
echo 123 > number.txt
parameter="\$(cat number.txt)"
echo "\${parameter}"
"""
}
Another solution is to instead use a shell block, where dollar ($) variables are managed by your shell (Bash interpreter), while exclamation mark (!) variables are handled by Nextflow. For example:
process bar {
echo true
input:
val greeting from 'Hello', 'Hola', 'Bonjour'
shell:
'''
echo 123 > number.txt
parameter="$(cat number.txt)"
echo "!{greeting} parameter ${parameter}"
'''
}
declare "parameter" in the top 'params' section.
params.parameter="1234"
(..)
script:
"""
(...)
create_report $file $fasta --flanking ${params.parameter}
(...)
"""
(...)
and call "nextflow run" with "--parameter 87678"

Bash create a path with variables divided by _ and check if exist

In Bash, I am trying to create a path with two variables within:
/path/to/my/file/${variable1_-}${variable2}/Still/some/path
My variable2 is always set, but the variable1 might be empty and in that case I don't want to print the "_"
I have tried the line above, but doesn't seem to be correct.
Can someone help in getting the right path printed?
Thanks in advance for your suggestions!
You have a simple typo (the underscore should be after the separator, not part of the variable name) and you want to include the underscore if variable1 is set, not it it's unset (so plus instead of minus in the parameter expansion; and add a colon to also cover the set but empty case). Presumably you also want to include the actual value of variable1 when it's set.
/path/to/my/file/${variable1}${variable1:+_}${variable2}/Still/some/path
or equivalently, nested
/path/to/my/file/${variable1:+${variable1}_}${variable2}/Still/some/path
where the braces before the underscore are necessary to separate the variable name from the literal text.
You can use this.
https://linux.die.net/man/1/bash
${parameter:+word}
set also variable1
variable1=VAR1
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
set only variable2
variable1=
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
With a few more lines of code this could work:
run () {
prefix="" # empty
if [ -n "$variable1" ]; then
prefix="${variable1}_"
fi
echo "/path/to/my/file/${prefix}${variable2}/Still/some/path"
}
# set only variable2
variable2=var2
run
# set also variable1
variable1=var1
run
output:
/path/to/my/file/var2/Still/some/path
/path/to/my/file/var1_var2/Still/some/path
description:
-n tests if the string is not empty, in that case I fill prefix with variable1 and the underscore

Bash - How to read content of file into variable, the file was read from key->value property file?

I have one key->value property file (my.prop) with such a content:
ROOT_PATH = /opt/user1/
REL_PATH = data/folder1/
CONF_FILENAME = my.conf
In my bash script I simply read this file, like this:
#!/bin/bash
PROP_FILE='my.prop'
ROOT_PATH =''
REL_PATH=''
CONF_FILENAME=''
while read -r key eq value; do
case $key in
"ROOT_PATH")
ROOT_PATH=${value}
;;
case $key in
"REL_PATH")
REL_PATH=${value}
;;
case $key in
"CONF_FILENAME")
CONF_FILENAME=${value}
;;
esac
done < $PROP_FILE
After that I would like to form the path to my.conf file and read its content to some variable, like this:
CONF_FULL_PATH=$ROOT_PATH$REL_PATH$CONF_FILENAME
CONF_FILE_CONTENT=`cat ${CONF_FULL_PATH}`
If I print out CONF_FULL_PATH variable it will have some trash inside (parts of all three sub paths).
And at this lineCONF_FILE_CONTENT=`cat ${CONF_FULL_PATH}` I will have this error message - : No such file or directoryta/folder1/
So, my question is, how could I properly form the path to my.conf file and put its content to some specific variable? I already tried source command as a replacement for while loop. Also to build a proper path string I've used this statements:
$(dirname $ROOT_PATH)/$(dirname REL_PATH)/$(basename $CONF_FILENAME) but this looks odd for my point of view.
Any help would be great!
If you remove the spaces from your my.prop file, you can use source (or .) to read the variables inside it. This will make it much easier.
my.prop:
ROOT_PATH=/opt/user1/
REL_PATH=data/folder1/
CONF_FILENAME=my.conf
Then you can use these directly in your script:
#!/bin/bash
. my.prop
CONF_FULL_PATH="${ROOT_PATH}${REL_PATH}${CONF_FILENAME}"
CONF_FILE_CONTENT=$(cat "$CONF_FULL_PATH")

Unable to use environment variables in TCL (Linux/Unix)

I'm having difficulties trying to use environment variables in TCL.
So I have a shell script which exports a bunch of environment variables so that my TCL scripts can use them later on.
set_vars.sh has the following code in it:
export TEST_ENV_VAR=Test
and the following works:
% puts $env(TEST_ENV_VAR)
Test
BUT once I place the environment variable in a operator or in quotes, nothing is returned.
% puts "$env(TEST_ENV_VAR) is in TEST_ENV_VAR"
is in TEST_ENV_VAR
Anyone know what the issue could be? Any help would be greatly appreciated.
Thanks for your time.
There must be something you're not showing us:
$ export TEST_ENV_VAR=Test
$ tclsh
% puts $env(TEST_ENV_VAR)
Test
% puts "$env(TEST_ENV_VAR) is in TEST_ENV_VAR"
Test is in TEST_ENV_VAR
Note that env is a global variable, so if you're using it in a procedure, you must declare it as global
$ tclsh
% proc show_env1 {varname} {
puts "does not work: $env($varname)"
}
% show_env1 TEST_ENV_VAR
can't read "env(TEST_ENV_VAR)": no such variable
% proc show_env2 {varname} {
global env
puts "works: $env($varname)"
}
% show_env2 TEST_ENV_VAR
works: Test
% proc show_env3 {varname} {
puts "also works: $::env($varname)"
}
% show_env3 TEST_ENV_VAR
also works: Test

Calling external script in matlab and capturing output

Hey so I have a bash command that echos a string based on reading some file. Say for simplicity it is like this
for line in `cat file`
do
if [ "$line" == "IwantThisLine" ]
then
echo "True"
fi
done
And I have it saved as its own individual script. Its called readRef.sh. So now I want to call it in matlab and store whatever it outputs in a variable! I am not entirely sure on how to do that, I seem to get an error when using evalc() on a system(). But it could be just me messing up quotations.
I tried something like
evalc(system(['./readRef.sh ' bamfile']))
The "bamfile" is a variable that is just a string to the path of a bamfile.
I get this error.
>> tes = evalc(system(['./readRef.sh ' smplBamFile]))
hg18
??? Undefined function or method 'evalc' for input arguments of type 'double'.
Coincidentally it does spit out "hg18" which is what I want to set the matlab variable to be.
Oh, I see. I don't think you need evalc at all. Reading the system docs you can just do:
[status, result] = system('echo True; echo "I got a loverly bunch of coconuts"')
And result will be
True
I got a loverly bunch of coconuts
So just do:
[status, result] = system(['./readRef.sh ' smplBamFile])
The reason evalc isn't working is that it requires its input to be a Matlab expression in a string, but you are passing it the result of system.
You could try:
evalc("system(['./readRef.sh ' smplBamFile])")
See how I'm passing in the system(...) as a string?
The reason you get this error is because system(...) returns the return-code of the command it ran, not its output. To capture its output, use
[~, output] = system(...)
tes = evalc(output);

Resources