I am trying to create a parameter file in shell script. And when i am trying to write variables into the file it is writing it's content in different line.
Input variables
var1="1,2,3,4"
var2="100,200,200,400"
var3="10,11,12,13"
How i am writing these variables into file
echo "VAR1="$var1 >> file.param
echo "VAR2="$var2 >> file.param
echo "VAR3="$var3 >> file.param
Expected file content
VAR1=1,2,3,4
VAR2=100,200,200,400
VAR3=10,11,12,13
But for VAR1 numbers are written in a line and for VAR2 and VAR3 numbers are not written as expected.
VAR1=1,2,3,4
VAR2=100
,200
,200
,400
VAR3=10
,11
,12
,13
Can anyone please help me why this is happening and how can i prevent it.
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"
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
i have a problem when I want to output several variables to txt file in shell. I want them in one line, but there appear line break after first and third variable. I have for example this
for s in `ls $TESTDIR/results_orig.txt`
do
t_a=(`grep " a" $s`);
t_b=(`grep "Bytes written to file: " $s`);
t_c=(`grep " Total Time:" $s`);
t_d=(`grep "QP " $s`);
a=${t_a[2]};
b=${t_b[4]};
c=${t_c[3]};
d=${t_d[2]};
echo "$a $b $c $d" >> $TESTDIR/results.txt;
done
I got the variables t_a to t_d are after I parse original txt file to find some values in it. Than I want to find some number ant write them in file without other text that I have in original file. And I want it to write those parsed numbers in one line in new file, like
13.2 1678 3231.5 2422.1
And I get
13.2
1678 3231.5
2422.1
When I echo any variable separately to the console I get right values. So variables are parsed fine. What could be wrong?
Thank you.
csv file with the first row has header file. I need to read this first row and then parse it to see if it has the elements i'm looking for.
First row has 4 elements. 1. HDR 2. today's date 3. From date 4. To date.
here is the code i used to get the first row.
read -r header < "1" -- this game me the first row into header variable.
I tried to read this 'header' variable to further split the row.
read f1 f2 f3 < “$header”
echo "OS is : $f1"
echo "Company is: $f2"
echo "Value is : $f3"
i'm getting no values displayed. I think the reason could be 'header' is not coming in as a string.
I'm new to unix shell scripting. Please help.
read -r header < "1"
No. You are reading from a file named 1 (which probably does not exist, so read is failing) because < is used for redirection. Try typing that very command in a terminal, you'll get an error ("no such file or directory").
read f1 f2 f3 < “$header”
This is wrong. If the header shell variable contains 1 2 3 you are reading from the file named 1 2 3 (a six character file path with two spaces inside) because < is used for redirection
Consider using awk to process files made of lines containing several columns.
Consider starting your (executable) shell script with #!/bin/bash -vx (to get traces) during the debugging phase. Spend a few hours reading some shell scripting tutorial and then the bash reference manual.
Be sure to understand how shells work, notably their globbing. Be aware that every program (e.g. started by some shell) is started by execve(2) (done in some process, e.g. your shell process).
BTW, before the read f1 f2 f3 < “$header” you might (temporarily) add debugging outputs, e.g.
echo "header=" "$header" > /dev/stderr
in your shell script, to understand what is going on.
(You really should spend days in reading more about shells)
this is my first stackoverflow question, regarding bash scripting. I am a beginner in this language, so be kind with me.
I am trying to write a comparison script. I tried to store all the outputs into variables, but only the last one is stored.
Example code:
me:1234567
you:2345678
us:3456789
My code:
#!bin/bash
while read -r forName forNumber
do
aName="$forName"
echo "$aName"
aNumber="$forNumber"
echo "$aNumber"
done < "exampleCodeFile.txt"
echo "$aNumber"
For the first time, everything will be printed out fine. However, the second echo will only print out "3456789", but not all the numbers again. Same with $aName. This is a problem because i have another file, which i stored a bunch of numbers to compare $aNumber with, using the same method listed above, called $aMatcher, consisting:
aMatcher:
1234567
2345678
3456789
So if i tried to run a comparison:
if [ "$aNumber" == "$aMatcher" ]; then
echo "match found!"
fi
Expected output (with bash -x "scriptname"):
'['1234567 == 1234567']'
echo "match found!"
Actual output (with bash -x "scriptname"):
'['3456789 == 3456789']'
echo "match found!"
Of course my end product would wish to list out all the matches, but i wish to solve my current issue before attempting anything else. Thanks!
When you run your following code
aNumber="$forNumber"
You are over-writing the variable $aNumber for every line of the file exampleCodeFile.txt rather than appending.
If you really want the values to be appended, change the above line to
aNumber="$aNumber $forNumber"
And while matching with $aMatcher, you again have to use a for/while loop to iterate through every value in $aNumber and $aMatcher.