Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
5
votes
1
answers
10738
views
How to change regional settings in Linux, including decimal point
I was on Linux Mint, now I'm trying Ubuntu 18 main distro. At top of Gnome day of week/time is displayed. That's where I started, wanting to change it to English having set regional to Russian via GUI, as by the way I noticed time zone changes if I changed regional to US. I've read and tried this: [...
I was on Linux Mint, now I'm trying Ubuntu 18 main distro. At top of Gnome day of week/time is displayed. That's where I started, wanting to change it to English having set regional to Russian via GUI, as by the way I noticed time zone changes if I changed regional to US.
I've read and tried this: How to get current or set wanted regional format in the terminal?
Strangely, after
sudo update-locale LC_TIME=en_CA.UTF-8
and even restart of PC, /etc/default/locale
has CA, but locale
in bash gives RU. And day of week on top of screen is in Russian still. Why different in file and by command? How to change language on top of screen, is it some short date variable, where to find it?
As extension to above, is it possible and how to change decimal point and thousand separator each individually to what I like?
Alex Martian
(1287 rep)
Jul 5, 2018, 07:45 AM
• Last activity: Jul 8, 2025, 01:03 PM
0
votes
0
answers
22
views
How can I apply a function to cells in place in Calc?
I have a large table of numbers using the wrong decimal separator for my locale (dot instead of comma), so LibreOffice Calc does not parse them as numbers, but as strings. I can apply this function to a cell (A1 in this case) to parse it as a number: `NUMBERVALUE(A1; "."; "")` I know that I can crea...
I have a large table of numbers using the wrong decimal separator for my locale (dot instead of comma), so LibreOffice Calc does not parse them as numbers, but as strings. I can apply this function to a cell (A1 in this case) to parse it as a number:
NUMBERVALUE(A1; "."; "")
I know that I can create a side table where I would calculate the values using cell references and copy the results back to the original table. But that is impractical for a large table. How can I apply the function to all these cells at once without using any other cells?
matj1
(141 rep)
Apr 28, 2025, 11:57 AM
11
votes
6
answers
34664
views
Bash limiting precision of floating point variables
In Ubuntu `14.04.1 LTS 64-bit` bash I am declearing floating point variables by multiplying floating point bash variables in `bc` with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero to the left of the decimal point. How can I tra...
In Ubuntu
14.04.1 LTS 64-bit
bash I am declearing floating point variables by multiplying floating point bash variables in bc
with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero to the left of the decimal point. How can I transform, say 0.005000000
into .005
? This is necessary due to my file naming convention. Thanks for your recommendations.
UPDATE: Can I use it for already defined shell variables and redefining them? The following code gives me an error.
~/Desktop/MEEP$ printf "%.3f\n" $w
bash: printf: 0.005000: invalid number
0,000
The output of locale
@vesnog:~$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=tr_TR.UTF-8
LC_TIME=tr_TR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=tr_TR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=tr_TR.UTF-8
LC_NAME=tr_TR.UTF-8
LC_ADDRESS=tr_TR.UTF-8
LC_TELEPHONE=tr_TR.UTF-8
LC_MEASUREMENT=tr_TR.UTF-8
LC_IDENTIFICATION=tr_TR.UTF-8
LC_ALL=
The output of echo $w
@vesnog:~$ echo $w
0.005000
Vesnog
(689 rep)
Dec 24, 2014, 09:09 AM
• Last activity: Feb 26, 2025, 02:07 AM
7
votes
3
answers
367
views
Bash printf float formatting became nonsensical and random
Bash `printf` floating number formatting (with `%f` or `%g`) is suddenly completely wrong, and changing all the time. An example output: ```bash $ export LC_ALL=C $ printf '%g\n' 1 1.20739e+3531 $ printf '%g\n' 1 4.50784e-4778 $ printf '%g\n' 1 1.20739e+3531 $ printf '%g\n' 1 -2.71289e-809 $ printf...
Bash
printf
floating number formatting (with %f
or %g
) is suddenly completely wrong, and changing all the time.
An example output:
$ export LC_ALL=C
$ printf '%g\n' 1
1.20739e+3531
$ printf '%g\n' 1
4.50784e-4778
$ printf '%g\n' 1
1.20739e+3531
$ printf '%g\n' 1
-2.71289e-809
$ printf '%g\n' 1
1.3505e+3136
$ printf '%g\n' 1
7.19546e+2880
$ printf '%f\n' 1
0.000000
$ printf '%f\n' 1
-19222373783767455764509969957314767706032565305205751517976389011586356910287283735873496366975075385910809025031233228909443235485943598697862160225065543796566324326303010027986380740507313362109478389280032720809755605977575591712977484833714421549464179548312816619669691695969675768618124142786377660052446990574866227828104911221712770547544994592495621702453486060768120985842267603349960735550433657827156688542311891238932963870595675569407105652838611000368894193915118759451716250477855842495787217908770520219596001805968923516834815448319714521179606192054239712352794281273655344703644255690153172028191401140768961585354430400117766273628103129509500449952562778071128862373050266582261154261590345666202936676378315868263637718127652344391988248019089245237502067305800855582602326233046151999935749805709388443164385685796904118233897433899895164143403147924128628840995216653448904704.000000
$ printf '%f\n' 1
-1535939692251970798780814048195400843655654544941633379309930935432267368465724365567117453392273362651582982424721184431169329486749020702578930669204206705946793193732917405530938783202632819503328225917797530425669645856818642162664722510713747202433401571296943178862058175872487260754269792220244257605097737000140353828109440097622967061126831273879953063844519410176251996107980841451192710472925184.000000
$ printf '%f\n' 1
-559936185544451048996856397218869061556420299014564433770484452018186365447891968084773838558135844864.000000
It is bash builtin that is being used, as I can see when typing command -v printf
.
alias printf
says "not found". I have the binaries at /bin/printf
and /usr/bin/printf
that behave normally. Awk and Python equivalent commands behave normally. Interestingly, when using sh
, it's fine too.
I am on Debian testing, and I tried to apt upgrade
and reboot but the bug persists.
Bash version is GNU bash, version 5.2.32(1)-release (x86_64-pc-linux-gnu)
. I tried apt reinstall bash
without success. Yesterday for another reason I did a apt-get dist-upgrade
.
What have I done?! It's wild.
[EDIT: additional debugging below]
- Run a Memtest86+ for 12 passes, detected no error.
- recompiled [Bash 5.2.32 from GNU](https://www.gnu.org/software/bash/) , it has the problem.
- tried as another user.
- This bug has been reported here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1078556 . I guess I am going to continue this topic with the Debian bug report system.
Here is the content of /etc/apt/sources.list
:
deb https://deb.debian.org/debian/ testing main contrib non-free non-free-firmware
deb-src https://deb.debian.org/debian/ testing main contrib non-free non-free-firmware contrib
deb http://security.debian.org/debian-security testing-security main contrib non-free non-free-firmware contrib
deb-src http://security.debian.org/debian-security testing-security main contrib non-free non-free-firmware contrib
and I also have /etc/apt/preferences.d/security
as suggested in [Best practices for Testing users](https://wiki.debian.org/DebianTesting#Best_practices_for_Testing_users) :
Package: src:chromium src:firefox src:firefox-esr src:linux src:linux-signed-amd64
Explanation: these packages are always security updates updated in unstable first
Pin: release a=/^(unstable|unstable-debug|buildd-unstable|buildd-unstable-debug)$/
Pin-Priority: 980
PlasmaBinturong
(751 rep)
Sep 17, 2024, 01:20 PM
• Last activity: Feb 1, 2025, 02:20 PM
429
votes
17
answers
663549
views
How to do integer & float calculations, in bash or other languages/frameworks?
Using `echo "20+5"` literally produces the text "`20+5`". What command can I use to get the numeric sum, `25` in this case? Also, what's the easiest way to do it just using bash for floating point? For example, `echo $((3224/3807.0))` prints `0` :(. I am looking for answers using either the basic co...
Using
echo "20+5"
literally produces the text "20+5
".
What command can I use to get the numeric sum, 25
in this case?
Also, what's the easiest way to do it just using bash for floating
point? For example, echo $((3224/3807.0))
prints 0
:(.
I am looking for answers using either the basic command shell ('command
line') itself or through using languages that are available from the
command line.
Michael Durrant
(43563 rep)
Jun 14, 2012, 02:43 PM
• Last activity: Nov 26, 2024, 11:07 AM
1
votes
1
answers
70
views
Pi Zero Cross Compilation Floating Point Specification
I am trying to cross compile a go project (syzkaller) to target a Pi Zero so it can emulate usb devices. The problem is that I have been unable to find information regarding how to specify the proper floating point flag to ensure compatibility with the Pi Zero. Everything I have tried results in the...
I am trying to cross compile a go project (syzkaller) to target a Pi Zero so it can emulate usb devices. The problem is that I have been unable to find information regarding how to specify the proper floating point flag to ensure compatibility with the Pi Zero. Everything I have tried results in the
VMOV
assembly instruction being in the binary file and throwing an illegal instruction. My host machine is x86_64
running go1.21.4
and the pi zero is arm6l
running go1.21.4
. When cross compiling I am using GOOS=linux GOARCH=arm GOARM=5
. The makefile I am using can be found here: https://github.com/google/syzkaller/blob/master/Makefile (I am compiling the execprog binary). From my research the GOARM=5
should be forcing software floating point operations, but the information sources are very outdated. I have also tried compiling for armv5 with no success. I also could be using the flag incorrectly since I am not very familiar with GO. Below are the exact compilation arguments for the binary. Any advice is appreciated, thanks.
Must remake target 'execprog'.
GOOS=linux GOARCH=arm go build "-ldflags=-s -w -X github.com/google/syzkaller/prog.\
GitRevision=121701b62358a454bbfdccfadfcce9bb843602d6+ -X 'github.com/google/syzkaller\
/prog.gitRevisionDate=20240605-134717'" "-tags=syz_target syz_os_linux syz_arch_armv6\
GOARM=5" -o ./bin/linux_arm/syz-execprog github.com/google/syzkaller/tools/syz-execprog
Darrion Ramos
(23 rep)
Jun 28, 2024, 03:30 AM
• Last activity: Jun 28, 2024, 04:55 AM
0
votes
3
answers
1097
views
Only allow floating points regex
How can I check if a specific string is a floating points? This are possible floating points: 12.245 +.0009 3.11e33 43.1E11 2e-14 This is what I tried: grep "^[+\-\.0-9]" grep "^[+-]*[0-9]" grep "^[+\-\.0-9]" And other lots of related things, but none filtered anything at all. Almost every string go...
How can I check if a specific string is a floating points?
This are possible floating points:
12.245
+.0009
3.11e33
43.1E11
2e-14
This is what I tried:
grep "^[+\-\.0-9]"
grep "^[+-]*[0-9]"
grep "^[+\-\.0-9]"
And other lots of related things, but none filtered anything at all. Almost every string got through. How would I tackle this problem?
O'Niel
(169 rep)
Oct 12, 2017, 07:41 PM
• Last activity: Mar 7, 2024, 07:47 PM
0
votes
1
answers
2243
views
Setting Input field to 2 decimal points (Price)
I am currently trying to input prices into a .txt file. So i want it when the user input:`1.1` Instead of `1.1`, I want it to become `1.10` instead. As it would look better for the table I am trying to make in the future. I would also like to round up or down if it goes more than 3 decimal places. (...
I am currently trying to input prices into a .txt file.
So i want it when the user input:
1.1
Instead of 1.1
, I want it to become 1.10
instead. As it would look better for the table I am trying to make in the future. I would also like to round up or down if it goes more than 3 decimal places. (_Thanks john1024 for the correction_)
pandora
(3 rep)
Jan 19, 2018, 02:56 AM
• Last activity: Nov 15, 2023, 07:20 PM
77
votes
5
answers
229493
views
How to convert floating point number to integer?
Is this the right way to do float to integer conversion in bash? Is there any other method? flotToint() { printf "%.0f\n" "$@" }
Is this the right way to do float to integer conversion in bash? Is there any other method?
flotToint() {
printf "%.0f\n" "$@"
}
Rahul Patil
(25515 rep)
Sep 6, 2013, 08:36 PM
• Last activity: Oct 29, 2023, 07:34 PM
71
votes
4
answers
74664
views
Are operations on floats supported with bc?
It looks like bc doesn't support float operations. When I do `echo 1/8 | bc` it gets me a zero. I checked the manual page `bc (1)`, but it doesn't even mention `float`, so I wonder if it's supported?
It looks like bc doesn't support float operations. When I do
echo 1/8 | bc
it gets me a zero. I checked the manual page bc (1)
, but it doesn't even mention float
, so I wonder if it's supported?
daisy
(55777 rep)
Mar 4, 2013, 02:07 AM
• Last activity: Aug 29, 2023, 11:36 AM
23
votes
3
answers
93251
views
How to round floating point numbers in shell?
How do I correctly round IEEE 754 floating point numbers on the command line? I want to specify the precision of the output number - the count of fractional digits. Rounding `6.66` to precision `1` should give `6.7`, for example. More in the table below: Value Precision Rounded 6.66 0 7 6.66 1 6.7 6...
How do I correctly round IEEE 754 floating point numbers on the command line?
I want to specify the precision of the output number - the count of fractional digits.
Rounding
6.66
to precision 1
should give 6.7
, for example. More in the table below:
Value Precision Rounded
6.66 0 7
6.66 1 6.7
6.66 2 6.66
6.66 3 6.660
6.666 3 6.666
6.6666 3 6.667
It should be usable in an interactive shell, but ideally robust enough for using it in production shell scripts.
Volker Siegel
(17703 rep)
Nov 10, 2014, 05:49 AM
• Last activity: May 5, 2023, 10:24 AM
0
votes
0
answers
59
views
Rounding 2.555500000000000104805053 with printf -> 2.555 or 2.556?
I tried ``` printf "%.3f\n" 2.555500000000000104805053 printf "%.3f\n" 2.555500000000000104805054 ``` the result was ``` 2.555 2.556 ``` same with `printf "%.3f\n" 3.555500000000000104805053` But 0.555500..., 1., 4., 5., 6., 7., 8., 9. are correct -> x.556 What is the problem with rounding with prin...
I tried
printf "%.3f\n" 2.555500000000000104805053
printf "%.3f\n" 2.555500000000000104805054
the result was
2.555
2.556
same with printf "%.3f\n" 3.555500000000000104805053
But 0.555500..., 1., 4., 5., 6., 7., 8., 9. are correct -> x.556
What is the problem with rounding with printf?
- MacOS Ventura 13.2
- The shell is zsh 5.8.1 (x86_64-apple-darwin22.0), but it appears also
in bash-3.2
- In ksh (shell) the output is identical (2.556) with both commands
R 9000
(167 rep)
Feb 27, 2023, 10:35 PM
• Last activity: Feb 28, 2023, 08:54 AM
2
votes
2
answers
896
views
How do you derive a decimal from a math operation involving a variable?
I'm trying to find derive `SCALEFACTOR` which is basically `10000/(sum of 4th column in a file)`. How do I get a decimal from the output? Appreciate any help in advance. #!/bin/bash FILES=/path/to/files/*; for f in ${FILES} do echo $f COLTOTAL="$(awk '{sum += $4} END {print sum}' $f)" echo "total: $...
I'm trying to find derive
SCALEFACTOR
which is basically 10000/(sum of 4th column in a file)
. How do I get a decimal from the output? Appreciate any help in advance.
#!/bin/bash
FILES=/path/to/files/*;
for f in ${FILES}
do
echo $f
COLTOTAL="$(awk '{sum += $4} END {print sum}' $f)"
echo "total: ${COLTOTAL}"
# SCALEFACTOR=expr 10^5 / $COLTOTAL
B=10000
SCALEFACTOR=$((B / ${COLTOTAL}))
SCALINGFACTOR=$(echo "100000 / $COLTOTAL" | bc -l
# echo "scale=5; ${SCALEFACTOR}" | bc
echo ${SCALEFACTOR}
awk '{print($1"\t"$2"\t"$3"\t"$4 * ${SCALINGFACTOR})}' $f > $f"_normalized.txt"
done
Stephen
(141 rep)
Feb 24, 2015, 06:48 PM
• Last activity: Feb 17, 2023, 12:41 AM
1
votes
1
answers
321
views
Completed stumped by trying to use a decimal value in an array for Bash
Here is some simple test code. ``` #!bin/bash cpm=(0 1 0.094) lv=1 attack=5 defense=9 stamina=16 echo $((cpm[lv])) mycpm=$((cpm[lv])) #mycpm=`echo "0.094" | bc -l` cq=`echo "$attack*$defense*1/10*sqrt($stamina)*2^2*$mycpm" | bc -l` echo $cq ``` Terminal output: ``` bash testing.sh 1 72.0000000000000...
Here is some simple test code.
#!bin/bash
cpm=(0 1 0.094)
lv=1
attack=5
defense=9
stamina=16
echo $((cpm[lv]))
mycpm=$((cpm[lv]))
#mycpm=echo "0.094" | bc -l
cq=echo "$attack*$defense*1/10*sqrt($stamina)*2^2*$mycpm" | bc -l
echo $cq
Terminal output:
bash testing.sh
1
72.00000000000000000000
Okay, great.
Then if I change the third line to =2
bash testing.sh
testing.sh: line 7: 0.094: syntax error: invalid arithmetic operator (error token is ".094")
testing.sh: line 8: 0.094: syntax error: invalid arithmetic operator (error token is ".094")
(standard_in) 2: syntax error
So how am I supposed to use decimals from an array? I cannot find any thing with duckduckgoing. The closest are links to for loop arrays where they figure out how to iterate by +0.1 each step. Otherwise its tutorials that all use integers. I have a preconstructed array and I just need to get values out of it to use in expressions.
Line 10 will work if I actually take out the $mycpm and change that to 0.094. It'll evaluate that just fine. It's just the problem if being able to use an array to have this value.
Is what I want to achieve possible in bash?
Tim50001
(73 rep)
Feb 3, 2023, 12:57 AM
• Last activity: Feb 3, 2023, 08:31 PM
2
votes
3
answers
1836
views
Replicating floating point arithmetic WITHOUT bc utility
I'm needing to write a script on some embedded Linux systems that don't have `bc` installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that takes certain corrective actions at different load values. For example, at load average 1.5, do s...
I'm needing to write a script on some embedded Linux systems that don't have
bc
installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that takes certain corrective actions at different load values. For example, at load average 1.5, do something.
I am wondering if there's a simple way to take the load average variable and just multiply it by 100 _or_ simply move the decimal place to the right two spaces and pad with 0s if necessary, that would then make this integer arithmetic and normal bash (()) arithmetic expansion could take over.
Right now, I'm breaking the floating point number its truncated integer and the decimal as a whole number(e.g. 1.5, LOAD1_INT=1, LOAD1_DECIMAL=50) but would like to simplify it if possible.
Current (complicated) version:
CRIT_LOAD=3.5
if [[ $CRIT_LOAD =~ ^[0-9]{1,2}\.[0-9]{1,2}$ ]]; then
# Since bash can't handle floating point arithmetic, break $CRIT_LOAD float into 2 separate integers
CRIT_LOAD_INT=$(echo $CRIT_LOAD | cut -d'.' -f1)
CRIT_LOAD_DECIMAL=$(echo $CRIT_LOAD | cut -d'.' -f2)
elif [[ $CRIT_LOAD =~ ^[0-9]{1,2}$ ]]; then
# If $CRIT_LOAD is already an int, update variables so Monitor code works unchanged
CRIT_LOAD_INT=$CRIT_LOAD
CRIT_LOAD_DECIMAL=0
else
# Set a default value of 1.0 if we can't parse CRIT_LOAD value
CRIT_LOAD_INT=1
CRIT_LOAD_DECIMAL=0
fi
LOAD1=$(cat /proc/loadavg | cut -d' ' -f1)
LOAD1_INT=$(echo $LOAD1 | cut -d'.' -f1)
LOAD1_DECIMAL=$(echo $LOAD1 | cut -d'.' -f2)
# Current load int is already higher than critical threshold int
if (( LOAD1_INT > CRIT_LOAD_INT )); then
log "CRITICAL: Load values have exceeded threshold."
elif (( LOAD1_INT == CRIT_LOAD_INT )); then
# If current load int is same as crit threshold int, compare decimals
if (( LOAD1_DECIMAL > CRIT_LOAD_DECIMAL )); then
log "CRITICAL: Load values have exceeded threshold."
fi
fi
Is there a way to just pare down all this code by simply turning loadavg (e.g. 1.50) into an int (e.g. 150)? Again, _without_ using the bc
utility as it't not available on these systems.
**EDIT:** I wound up taking the printf
command @ilkkachu suggested and modified it into a function for use in my code. I chose this route over the awk
command as there are other places in this code where calling a function to emulate floating point arithmetic simplifies code readability and re-usability. Marking his answer as solution.
function dec_to_int() {
DECIMAL=$1
SCALE_FACTOR=$2
# printf removes decimal and allows $SCALE_FACTOR additional spaces to be included, 0-pads numbers that would be too small otherwise
# NOTE: printf will round number if the values it keeps are greater than the scale factor
# e.g. SCALE_FACTOR=2, 1.759 -> 176
SCALED_INT=$(printf "%.0f\n" "${DECIMAL}e${SCALE_FACTOR}")
echo $SCALED_INT
}
LOAD1=$(cat /proc/loadavg | cut -d' ' -f1)
LOAD1=$(dec_to_int $LOAD1 2)
William
(43 rep)
May 16, 2022, 01:26 PM
• Last activity: Sep 4, 2022, 05:57 AM
24
votes
3
answers
51200
views
How to format floating point number with exactly 2 significant digits in bash?
I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: - 76543 should be printed as 76000 - 0.0076543 should be printed as 0.0076 In both cases the significant digits are 7 and 6. I have read some answe...
I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.).
Examples:
- 76543 should be printed as 76000
- 0.0076543 should be printed as 0.0076
In both cases the significant digits are 7 and 6. I have read some answers for similar problems like:
https://unix.stackexchange.com/questions/167058/how-to-round-floating-point-numbers-in-shell
https://unix.stackexchange.com/questions/175744/bash-limiting-precision-of-floating-point-variables
but the answers focus on limiting the number of decimal places (eg.
bc
command with scale=2
or printf
command with %.2f
) instead of significant digits.
Is there an easy way to format the number with exactly 2 significant digits or do I have to write my own function?
tafit3
(343 rep)
Feb 22, 2016, 11:10 PM
• Last activity: May 17, 2022, 05:35 PM
2
votes
1
answers
255
views
Gnuplot Precission
Does anyone know whether there is a way to run gnuplot with quadruple arithmetic precision? E.g. When you want to plot a line and you want to subtract a value like "2.3403800935287029218841060098446498" from values that vary only right hand to the 16th digit from this value. Any input on this proble...
Does anyone know whether there is a way to run gnuplot with quadruple arithmetic precision? E.g. When you want to plot a line and you want to subtract a value like
"2.3403800935287029218841060098446498"
from values that vary only right hand to the 16th digit from this value.
Any input on this problem is much appreciated.
Jannis Erhard
(121 rep)
Sep 17, 2019, 05:22 PM
• Last activity: May 2, 2022, 12:40 AM
0
votes
3
answers
2490
views
Using numbers with decimal points in if statements and operators
I'm trying to write a script that detects the Linux OS and version and through the use of conditions and operators execute different commands depending on the result. **Detect OS** ``` if [ -f /etc/os-release ]; then . /etc/os-release OS="$NAME" OS_version="$VERSION_ID" elif [ -f /etc/debian_version...
I'm trying to write a script that detects the Linux OS and version and through the use of conditions and operators execute different commands depending on the result.
**Detect OS**
if [ -f /etc/os-release ]; then
. /etc/os-release
OS="$NAME"
OS_version="$VERSION_ID"
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS="Debian"
OS_version="$(cat /etc/debian_version)"
fi
If I echo these variables they return with
$ echo $OS
Debian GNU/Linux
$ echo $OS_version
10
In my example below I want to match **Debian** and OS version **10**. However, the **$OS** variable contains more that just the word **Debian** so I need some kind of wildcard so I can do a partial match.
The only way I've found to get this to work in through command substitution by echoing the **$OS** variable to stdout and piping it into the grep command which then does a wildcard search. This is the if statement I came up with in the end:
if [ "$(echo "$OS" | grep 'Debian*')" -a "$OS_version" -ge 10 ]; then
echo "OS is Debian"
sleep 4
else
echo "OS is other"
sleep 4
fi
As I will be using this script on other Unix based OSs so I would like to make this script as widely compatible as possible and stick to POSIX standards.
While testing my script on Ubuntu 21.04 I discovered that Linux has trouble working out which number is greater than another when there is decimal place involved. Below I've created two shell scripts which export a two-integer number with a two-decimal place and uses an if statement to check if it's greater and/or equal to another number.
**20.10.sh**
#!/bin/sh
export OS_version="20.10"
if [ "$OS_version" -ge 21.04 ]; then
echo "Your OS is new enough"
sleep 2
else
echo "Your OS is too old!"
sleep 2
fi
**20.10.sh output**
~$ ./20.10.sh
YOUR OS VERSION IS >> 20.10
./20.10.sh: 6: [: Illegal number: 20.10
Your OS is too old!
**23.45.sh**
#!/bin/sh
export OS_version="23.45"
if [ "$OS_version" -ge 21.04 ]; then
echo "Your OS is new enough"
sleep 2
else
echo "Your OS is too old!"
sleep 2
fi
**23.45.sh output**
~$ ./23.45.sh
YOUR OS VERSION IS >> 23.45
./23.45.sh: 6: [: Illegal number: 23.45
Your OS is too old!
The **23.45.sh** script should echo "Your OS is new enough" but doesn't and displays an error of an illegal number.
Is there a way to detect the size of different numbers with decimal places in?
willowen100
(67 rep)
Feb 24, 2022, 06:24 PM
• Last activity: Feb 25, 2022, 05:44 AM
1
votes
1
answers
1616
views
Invalid arithmetic operator (error token is " 7.16.3 when passing float from read command
I am trying to automate software update with bash script. When I am passing version number e.g 7.16.3 I get following error: ") Syntax error Invalid arithmetic operator (error token is " I could not find any answer which related when passing value from `read` command. My code look like below: ```#!/...
I am trying to automate software update with bash script. When I am passing version number e.g 7.16.3 I get following error: ") Syntax error Invalid arithmetic operator (error token is "
I could not find any answer which related when passing value from
read
command. My code look like below:
#!/bin/bash
DATE=date +'%Y%m%d'
BSI_SETUP=/opt/bsi/source/setup/elk_${DATE}
OLD_VERSION_FILEBEAT=/usr/share/filebeat/bin/filebeat version| awk '{print $3 }' 2>/dev/null
OLD_VERSION_METRICBEAT=/usr/share/metricbeat/bin/metricbeat version| awk '{print $3 }' 2>/dev/null
MY_HOME=~
read -p 'Enter filebeat & metricbeat version: ' NEW_VERSION_BEATS
read -p 'Enter CSC environmet: ' CSC_ENV
if [[ ${NEW_VERSION_BEATS} -ne ${OLD_VERSION_FILEBEAT} ]]; then # I get error here
sudo yum install -y $BSI_SETUP/filebeat-*.rpm 2>/dev/null
else
echo "Filebeat is up-to-date"
fi
if [[ ${NEW_VERSION_BEATS} -ne ${OLD_VERSION_METRICBEAT} ]]; then # and here
sudo yum install -y $BSI_SETUP/metricbeat-*.rpm 2>/dev/null
else
echo "Metricbeat is up-to-date"
fi
codehunter-py
(25 rep)
Jan 25, 2022, 04:01 PM
• Last activity: Jan 25, 2022, 05:11 PM
0
votes
1
answers
101
views
My calculation is getting converted to integer by default(KSH)
I am assigning a decimal value to a variable which is an output from an oracle command. when I echo the variable I see the decimal value. For example, echo $a is showing me .7214 but when I do calculations on this, it is defaulted to integer. For example, b=$((a+(a*20/100))) is rounding to 1. How do...
I am assigning a decimal value to a variable which is an output from an oracle command. when I echo the variable I see the decimal value.
For example, echo $a is showing me .7214 but when I do calculations on this, it is defaulted to integer.
For example, b=$((a+(a*20/100))) is rounding to 1.
How do I avoid rounding this to integer.
RAJ
(1 rep)
Aug 25, 2021, 12:32 AM
• Last activity: Aug 25, 2021, 06:02 AM
Showing page 1 of 20 total questions