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
Related
I am writing a script that run other scripts. Two argument are parsing to this script. The script create a directory and copy scripts from a archive to the created folder. One of the scripts is a .config file with constants variables but I want to write the two arguments because they will be variables I will need later.
I can do with this
printf "FASTAQ1 = ${FASTA1}" >> $DIRECTORY/$FASTA1/scripts/shortcut.config
But the results is
FASTAQ1 = fasta1.fasta
But I think that what I need is this
FASTAQ1 = "fasta1.fasta"
Am I right? If so, How can I add theses quotation marks?
To print a quotation mark with printf, you can escape it with a backslash.
So, if you want to write the string FASTAQ1 = "fasta1.fasta" into a file at $DIRECTORY/$FASTA1/scripts/shortcut.config, given an environment variable FASTA1="fast1.fasta", you could do something like (ref. my bash terminal log):
FASTA1=fasta1.fasta
DIRECTORY=`pwd`
mkdir -p $DIRECTORY/$FASTA1/scripts
printf "FASTAQ1 = \"${FASTA1}\"" >> $DIRECTORY/$FASTA1/scripts/shortcut.config
Producing a shortcut.config with this contents:
FASTAQ1 = "fasta1.fasta"
an executable expects command-line parameters in the format:
-varname "[64]"
Need to wrap it as an environment variable so the executable can be launched by another tool, so I tried:
> setenv PARAM '-varname "[64]"'
> echo $PARAM
echo: No match.
I tried all kind of escapes but couldn't find how to enclose the original string into an environment variable.
Must mention that both the inner executable and the wrapper are inflexible in their expectations, e.g. the executable expects the variable as shown and the wrapper expects a string that it associates with an environment variable through 'setenv'.
Any hint?
Thanks!
You are setting it correctly, but the problem is how the shell expands the value of $PARAM.
As you know, a star at the command line is expanded by the shell to all the files in the current directory.
> echo *
The result of shell expansion is subsequently used as the argument to echo.
There are additional wildcard/glob patterns allowed at the command line. Square brackets define character class.
To echo all files containing either a 4 or a 6:
> echo *[46]*
To echo all files named exactly 4 or 6.
> echo [46]
In the above example, if no files are named exactly 4 or 6, then you'll get echo: No match.
Solution: use printenv
> setenv PARAM '-varname "[64]"'
> printenv PARAM
--> -varname "[64]"
Note that PARAM, not $PARAM is the argument to printenv. This avoid shell expansion of $PARAM.
I have a shell script in which I am passing the same value many times as arguments. Instead of hardcoding this redundant value across all py scripts, I wanted to see if I can work with variables and pass those into the py script as arguments.
Here is the script.sh:
python A.py --month=10
python B.py --country=USA --month=10
I would like something like this:
#Setting variables to pass into args
country=USA
month=10
python A.py --month=month
python B.py --country=country --month=month
How would I do this?
To access the value of any variable in shell script, you can do it using the $ sign.
So below is how you can do it :
#Setting variables to pass into args
country=USA
month=10
python A.py --month="$month"
python B.py --country="$country" --month="$month"
If you have to use arguments passed to script, try something like below
script SomeMonth SomeCountry
Then in script
python A.py --month="$1"
python B.py --country="$2" --month="$1"
The positional parameters..
$0 = script (script Name)
$1 = SomeMonth (first argument)
$2 = SomeCountry (Second argument)
And so on..
Just to add something to the other correct answers. If you use those arguments many times, you can also embed the argument name together with its value, and save typing and errors:
# Setting variables to pass into args
p_country="--country=USA"
p_month="--month=10"
python A.py $p_month
python B.py $p_country $p_month
Note 1: beware of values containing spaces, or any other "strange" characters (meaningful to the shell). Ordinary words or numbers, like "USA" and "10" pose no problems.
Note 2: you can also store multiple "arg=value" in a single variable; it can save even more typing. For example:
p_m_and_c="--country=USA --month=10"
...
python B.py $p_m_and_c
I want to define variable in shell script as:
value1 = 40 (this can be number or character)
and want to use as in a text like:
$value1_position.xyz (I basically want 40_position.xyz)
How do I do this?
this should do:
${value1}_position.xyz
beware that the variable should be declared with this syntax
value1=40
notice the absence of spaces around the =
To define a variable, simply make sure there are no spaces between the variable name and value
value1=40
To use that variable in bash substitution, creating what you want, use the $ replacement symbol like so:
${value1}_position.xyz
To append that to your text file
echo "${value1}_position.xyz" >> file.txt
I am trying to write a bash script that takes a variable number of file names as arguments.
The script is processing those files and creating a temporary file for each of those files.
To access the arguments in a loop I am using
for filename in $*
do
...
generate t_$(filename)
done
After the loop is done, I want to do something like cat t_$* .
But it's not working. So, if the arguments are a b c, it is catting t_a, b and c.
I want to cat the files t_a, t_b and t_c.
Is there anyway to do this without having to save the list of names in another variable?
You can use the Parameter expansion:
cat "${#/#/t_}"
/ means substitute, # means at the beginning.