Error in string comparison in if condition in bash shell [duplicate] - bash

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I need to read some properties from a config file and execute some commands if the properties match certain values.
But when I try to compare the value with a string in if condition, it doesn't work.
Below is a small version of what I am trying to do.
#!/bin/bash
source config.file
echo "mode: $mode"
if [ "$mode" = "slow" ];
then
echo "Mode is slow"
fi
The config file looks like this.
###config###
mode=slow
user=admin
password=pwd
###end###
The echo statement prints the values as "slow" but the if condition is never satisfied.
What am I doing wrong?

It works for me. Maybe you have MSWin line ends in the config file?
Run
dos2unix config.file
or
fromdos config.file
to convert them to the *nix standard.

Related

Composing a string in script shell [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx

BASH echo showing some unusual behaviour [duplicate]

This question already has answers here:
Strange "echo" behavior in shell script
(3 answers)
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 3 years ago.
This is the simple command that is giving me problems -
echo hafsda sfsdfdsfs $ymn $ymx $range
The output of this command is coming -
2.568 sfsdfdsfs 86.72
Where ymn = 86.72 ymx = 89.28 and range = 2.56. This only happens when I am using variables. The following command works fine -
echo hafsda sfsdfdsfs 1 2 $range
Also, the same command (the first one) works fine if I try running it directly in the terminal. This is only happening is a script. I also tried to use printf but encountered similar results.
I don't even understand what to google for to resolve this. I am unable to understand what is happening at all. So, what is happening here? Is this reproducible or is this just some error on my system, and if it is, what might be the problem?
Your script probably has DOS-style CRLF line endings. I suspect you actually have ymn="86.72\r" ymx="89.28\r" and range="2.56\r". You can test this in your script with
echo hafsda sfsdfdsfs $ymn $ymx $range | od -c
You can fix your script with dos2unix or sed -i 's/\r$// script.sh`.
Make sure you change the settings of your text editor do use unix line endings.

reading a line from file using shell script [duplicate]

This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it
IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT
But this script echo empty string. Where I am making mistake?
If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.
Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript
As an alternative that's both efficient and compatible with POSIX sh:
IFS= read -r SERVER_IP_PORT <"$IPAddressFile"

How to dynamically compute value of a variable and use it in a command? [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 6 years ago.
Reducing what I'm trying to do to a simple example:
env temp=`pwd` echo "$temp"
I get this message:
temp: Undefined variable.
How do I make it work (in a shell-agnostic way)? I'm expecting the result of pwd to be printed.
My actual requirement uses a complicated expression in place of pwd, a script in place of "echo", and the variable $temp as an argument to that script.
Also, I want to set this variable only for this single command, and not for the whole shell (or any subsequent subshells).
How about something like this for sh and bash
(export temp=`pwd`; echo $temp)
And this for csh
csh -c 'setenv temp `pwd`; echo "$temp"'

Linux bash scripts [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I test if a variable is a number in bash?
I am new to bash scripts. Needed a code to check whether a variable is a number, if YES i have to keep it as it, if NO i ll have to reassign it to some other value. How do I get this done?
Your basic if () then ... fi. Here's the format:
#!/bin/bash
var=2
if [[ $var != 1 ]]
then
var=5;
fi
echo $var
Note that the [[...]] format can actually be one of many things.
As with everything, if you want to get good at it, you'll have to work for it.
Type
man bash
man test
to get the manuals on the tools you're using. Read them top to bottom, again, again, and once more. And then tomorrow the same thing.

Resources