Assign the output to variable in shell script - shell

I am new to shell scripting and i am trying to assign the output of this line to a variable but all efforts are in vain.
var2=$(cat "filename")
var1=$(echo " $var2 +3 " | bc )
var2 is properly read from the file and also the output shows the value of the sumation, but the value is not assigned to the var1
ps : Filename contains a single entry which is a number

The code worked when i tried this
var3="$("echo " $var2 +3 " |bc)"

You are missing parenthesis
var2=$(cat "file")
var3=$(( var2 + 3 ))
echo $var3
Also you can try with expr
Something like this
var2=$(cat "filename")
var3=`expr $var2 + 3`
echo $var3
Here is a good answer
HIH

Related

ASH/BASH in Alpine: how to expand a variable from a list of variable names read from a list file

I've been trying to figure out a way to expand a variable from a string (the variable name) read from a list within a file.
The objective here is to evaluate if all variables in a gitlab pipeline are present, if not, fail the pipeline.
The best possible solution would be to have this as an one-liner and working from Alpine's ash shell, but a bash -c "something something" could also do.
Clearly there's a problem with the (!) expansion character as the varcontent is always empty, but I just can't figure out what I'm doing wrong.
alpine314:~# cat checkvars.sh
#!/bin/bash
false=false
while read -r line; do
varcontent=${!line}
if [ -z "$varcontent" ]; then
echo "Error: Missing a required pipeline variable."
false=true
exit 1
else
echo "$line: $varcontent"
fi
done < $1
alpine314:~# cat varsfile
VAR1
VAR2
VAR3
alpine314:~# echo $VAR1
1
alpine314:~# echo $VAR2
2
alpine314:~# echo $VAR3
alpine314:~# bash -x checkvars.sh varsfile
+ false=false
+ read -r line
+ varcontent=
+ '[' -z '' ']'
+ echo 'Error: Missing a required pipeline variable.'
Error: Missing a required pipeline variable.
+ false=true
+ exit 1
alpine314:~#
The desired behavior here is to exit the script with an error in case any of the variables are empty/not set.
Cheers!
what I'm doing wrong.
Your variables are not exported. Run the following before running your scirpt.
export VAR1 VAR2 VAR3
Check your scripts with shellcheck. false=true - would be better to use a different variable name...

Bash appending extra characters to variable?

I'm trying to store values in variables and trying to echo result to a file. But when it's adding two variables and echoing it to a file, extra characters are being added to the output file. This is happening in docker container, Can some one please help...
IFS=" "
#while read line
while read c s e
do
echo $c $s $e
first=$(echo "PER_${s}_${e}")
#echo -n $first
second=$(echo "/IPD_${c}")
#echo $second
echo $first$second >> /mnt/resource/step2/messages.txt
done < /mnt/resource/step2/job_control/Categories.txt
Categories.txt contains :
129490 201515 201540
I'm getting the output as :
PER__/IPD_PER_201515_201540/IPD_12949029490
But it should be like:
PER_201515_201540/IPD_129490
I can't reproduce the problem, but your code is more complicated than it needs to be.
while IFS=" " read c s e; do
first="PER_${s}_${e}"
second="/IPD_${c}"
echo "$first$second" >> /mnt/resource/step2/messages.txt
done < /mnt/resource/step2/job_control/Categories.txt

From bash script to bash script in infinite loop

A very simple example of the "just run once" version of my Script:
./myscript.sh var1 "var2 with spaces" var3
#!/bin/bash
echo $1 #output: var1
echo $2 #output: var2 with spaces
echo $3 #output: var3
Working as intended!
Now I try to just start the script and enter the vars in a loop, because later I want to copy multiple datasets at once to the shell.
./myscript.sh
#!/bin/bash
while true; do
read var1 var2 var3
#input: var1 "var2 with spaces" var3
echo $var1 #output: var1
echo $var2 #output: "var2
echo $var3 #output: with spaces" var3
done
It seems read splits the input at the spaces, putting all thats left in the last var, right? Is there any better possibility to add vars in a loop? Or how do I get read to behave like I added the vars just behind the script?
And what is the English word for that kind of loop to execute one script in a loop while copying different vars to the shell? Can't google for samples if I don't know what it is called...
This reads STDIN and parses those lines as arguments with shell quoting:
# Clean input of potentially dangerous characters. If your valid input
# is restrictive, this could instead strip everything that is invalid
# s/[^a-z0-9" ]//gi
sed -ue 's/[][(){}`;$]//g' | \
while read input; do
if [ "x$input" = "x" ]; then exit; fi
eval "set -- $input"
# check argument count
if [ $(( $# % 3 )) -ne 0 ]; then
echo "Please enter 3 values at a time"
continue;
fi
echo $1
echo $2
echo $3
done
set -- $input does all of the magic. See the Bash manual page for set.
--
If no arguments follow this option, then the positional parameters are
unset. Otherwise, the positional parameters are set to the arguments,
even if some of them begin with a ‘-’.

UNIX Script - setting dynamic variables (indirect variable reference)

How to set shell variables from an input file ?
hello,
I need to set dynamic variable from an .ini file in a shell script.
Assume the input file is input.ini :
var1=val1
var2=val2
var3=val3
In a script I want to set var1, var & var3 respectively to their val1, val2 & val3 to get
echo $var1
val1
echo $var2
val2
...
I've tryed :
file="input.ini"
while IFS== read -r f1 f2
do
eval dynvar=$f1
dynvar=$f2
done <"$file"
echo $var1
echo $var2
echo $var3
the echo $varx commands give no output. How can I work it out ?
thanks in advance.
source input.ini
Or
. input.ini
More info
<source | .> filename [arguments]
Execute commands from a file in the current shell.
Solved
using :
file="install.ini"
while IFS== read -r f v
do
eval "$f=$v"
done <"$file"
did the trick.

read one line with "read" in bash, but without "while"

I was trying to understand how "while read" works in bash, and i came accross a behaviour that i can't explain:
root#antec:/# var=old_value
root#antec:/# echo "new_value" | read var && echo $var
old_value
root#antec:/#
It works fine with "while read":
root#antec:/# var=old_value
root#antec:/# echo "new_value" | while read var; do echo $var; done
new_value
root#antec:/#
Can someone explain why it does not work when read is used without while ? Moreover, i don't understand how the value of "var" in the main shell can be seen from the allegedly subshell after the pipe ..
Thank you
I believe this is precedence problem, with | having higher precedence than &&. First is grouped as:
(echo "new_value" | read var) && echo $var
Second is grouped as:
echo "new_value" | (while read var; do echo $var; done)
Why do you think it works in the second case? It doesn't really update var. do echo $var after the line with while and see.
Explanation: whatever comes after | is executed in a subshell that has its own copy of var. The original var is not affected in either case. What is echoed depends on whether echo is called in the same subshell that does read or not.
I didn't want to print the variable immediately but use it later on and this works:
var=old_value
read var < <(echo "new_value")
echo $var
> new_value
alternatively:
var=old_value
tmp=$(echo "new_value")
read var <<<"$tmp"
echo $var
> new_value
You don't need a while loop:
echo "new_value" | (read var; echo $var)

Resources