Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
36
votes
12
answers
56172
views
How to compare a program's version in a shell script?
Suppose I want to compare `gcc` version to see whether the system has the minimum version installed or not. To check the `gcc` version, I executed the following gcc --version | head -n1 | cut -d" " -f4 The output was 4.8.5 So, I wrote a simple `if` statement to check this version against some other...
Suppose I want to compare
gcc
version to see whether the system has the minimum version installed or not.
To check the gcc
version, I executed the following
gcc --version | head -n1 | cut -d" " -f4
The output was
4.8.5
So, I wrote a simple if
statement to check this version against some other value
if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi
But it throws an error:
[: integer expression expected: 4.8.5
I understood my mistake that I was using strings to compare and the -lt
requires integer. So, is there any other way to compare the versions?
Abhimanyu Saharan
(911 rep)
May 27, 2016, 02:01 PM
• Last activity: Aug 6, 2025, 04:04 AM
23
votes
2
answers
8654
views
Why is Gnome fractional scaling 1.7518248558044434 instead of 1.75?
If I set 175% scaling in Gnome Settings, the value is saved as `1.7518248558044434` in `~/.config/monitors.xml`: ```xml 0 0 1.7518248558044434 yes DP-3 ``` Why is it so? At first, I thought it could be due to floating point rounding error, but 1.75 is one of those happy numbers whose the exact value...
If I set 175% scaling in Gnome Settings, the value is saved as
1.7518248558044434
in ~/.config/monitors.xml
:
0
0
1.7518248558044434
yes
DP-3
Why is it so? At first, I thought it could be due to floating point rounding error, but 1.75 is one of those happy numbers whose the exact value can be expressed.
Gnome Wayland 43.3
Damn Vegetables
(1539 rep)
Mar 20, 2023, 03:31 AM
• Last activity: Jul 16, 2025, 05:41 AM
7
votes
3
answers
2582
views
Trim trailing zeroes off a number extracted by jq
The following command achieve my goal by grepping `BTC` price from specific exchange. curl -sS https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT | jq -r '.price' the output will be for the moment `7222.25000000` but i would like to get it `7222.25`
The following command achieve my goal by grepping
BTC
price from specific exchange.
curl -sS https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT | jq -r '.price'
the output will be for the moment 7222.25000000
but i would like to get it 7222.25
αԋɱҽԃ αмєяιcαη
(1267 rep)
May 12, 2019, 06:31 AM
• Last activity: Feb 19, 2025, 08:29 PM
2
votes
1
answers
242
views
Do `[[ ]]` and `(( ))` have "transitive evaluation" of variables?
I am using bash, version: ``` GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) ``` I made a following script: ``` #!/usr/bin/bash a=11 b=4 c=7+b if [[ a -eq c ]] then echo "ok" else echo "not ok" fi ``` It prints "ok". When I change `a` to 10, it prints "not ok". So I have a simple question...
I am using bash, version:
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
I made a following script:
#!/usr/bin/bash
a=11
b=4
c=7+b
if [[ a -eq c ]]
then
echo "ok"
else
echo "not ok"
fi
It prints "ok". When I change a
to 10, it prints "not ok". So I have a simple question, do [[ ]]
and (( ))
makes "transitive evaluation" (in my example, that would mean c
first evaluates to 7+b
and then b
evaluates to 4
and finally it overall becomes 11
)?
I ask because I have a serious "fight" with AIs (chatGPT, gemini, claude), where they say that this kind of evaluation does not happen, but my examples show otherwise (they say that c
is evaluated to 7+b
, and since it is not a number, the value 0
is taken)... Thanks in advance
Yakog
(517 rep)
Feb 17, 2025, 11:40 AM
• Last activity: Feb 17, 2025, 04:07 PM
-3
votes
2
answers
77
views
Error parsing memory value in bash script: expected integer expression
I have the following script to check free memory, ```bash #!/bin/bash THRESHOLD="500" FREE_MEM=$(free -mh | awk '/^Mem:/{print $4}') if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then echo "insufficient storage. Available memory is ${FREE_MEM} MB" fi ``` However, I am getting this error: ```none ./memory_mon...
I have the following script to check free memory,
#!/bin/bash
THRESHOLD="500"
FREE_MEM=$(free -mh | awk '/^Mem:/{print $4}')
if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then
echo "insufficient storage. Available memory is ${FREE_MEM} MB"
fi
However, I am getting this error:
./memory_monitor.sh: line 6: [: 135Mi: integer expression expected
Be_developer
(1 rep)
Dec 17, 2024, 09:28 AM
• Last activity: Dec 17, 2024, 11:35 AM
4
votes
3
answers
22390
views
Extracting positive/negative floating-point numbers from a string
I am trying to extract numbers out of some text. Currently I am using the following: echo "2.5 test. test -50.8" | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' This would give me 2.5, "." and 50.8. How should I modify the first `sed` so it would detect float numbers,...
I am trying to extract numbers out of some text. Currently I am using the following:
echo "2.5 test. test -50.8" | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' '
This would give me 2.5, "." and 50.8. How should I modify the first
sed
so it would detect float numbers, both positive and negative?
ahajib
(143 rep)
Jun 20, 2016, 07:28 PM
• Last activity: Nov 13, 2024, 01:16 PM
17
votes
2
answers
23473
views
comm: file is not in sorted order
I used `comm` to compare two sorted files. Each line in these files are positive integer numbers. But the results show comm: file 1 is not in sorted order comm: file 2 is not in sorted order How come the error even if these two files are sorted?
I used
comm
to compare two sorted files.
Each line in these files are positive integer numbers.
But the results show
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
How come the error even if these two files are sorted?
wenzi
(423 rep)
Nov 16, 2012, 11:25 AM
• Last activity: Jul 27, 2024, 01:02 PM
0
votes
2
answers
108
views
CSV output for Number data
Below is a tab delimited sample. When I open this file with notepad it looks good for numbers. But if I open it with excel, numbers do not look correct. They look +11 at the end but inside if I double click data is correct. I want to see the actual values instead of +11 at the end, if I use excel sp...
Below is a tab delimited sample. When I open this file with notepad it looks good for numbers. But if I open it with excel, numbers do not look correct. They look +11 at the end but inside if I double click data is correct.
I want to see the actual values instead of +11 at the end, if I use excel spreadsheet.
I don't want to manually change format of the cells to text and see the value (or) change format to numbers and see the data. That would be manual approach. I want this to be automated. When a file is generated as file.csv from Informatica powercenter and opened in Excel the numbers should look correct without having to double click the cells.
Can this be done with Unix script? I would be happy with xlsx format if it is not possible with csv.
Name Number address pin
John 23435435 usa 235
Jacob 2342342 india 234
Jack 35345345 uk 345
Jet 3234234 canada 323
jub 234234 australia 366
codecheck123
(1 rep)
Oct 18, 2023, 08:20 PM
• Last activity: May 11, 2024, 07:39 AM
10
votes
4
answers
80858
views
bash + how to calculate percentage from number
how to calculate percentage from number for example we set number=248 and we want to know what is the 80% from $number so how to calculate it in bash ? expected output 198 ( exactly is 198.4 but we want to round down with floor )
how to calculate percentage from number
for example we set
number=248
and we want to know what is the 80% from $number
so how to calculate it in bash ?
expected output 198 ( exactly is 198.4 but we want to round down with floor )
yael
(13936 rep)
Jan 31, 2018, 08:56 PM
• Last activity: May 9, 2024, 06:54 PM
4
votes
7
answers
8664
views
Convert a list of decimal values in a text file into hex format
I have a need to convert a list of decimal values in a text file into hex format, so for example test.txt might contain: 131072 196608 262144 327680 393216 ... the output should be list of hex values (hex 8 digit, with leading zeroes): 00020000 00030000 00040000 ... the output is printed into the te...
I have a need to convert a list of decimal values in a text file into hex format, so for example test.txt might contain:
131072
196608
262144
327680
393216
...
the output should be list of hex values (hex 8 digit, with leading zeroes):
00020000
00030000
00040000
...
the output is printed into the text file. How to make this with python or linux shell script?
### EDIT #1
I missed one extra operation: I need to add
80000000
hex to each of the created hex values. (arithmetic addition, to apply to already created list of hex values).
minto
(575 rep)
Jul 21, 2018, 01:29 PM
• Last activity: Apr 23, 2024, 03:24 PM
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
77
votes
5
answers
229488
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
8
votes
5
answers
1839
views
How to round to 2 decimals in bash like MS Excel does?
I spent hours in searching how to round "floating numbers" in BASH but couldn't find any correct! solution :( If I put these numbers to Excel, then I will receive the correct results after rounding to 2 decimals: 3.314 -> 3.31 3.315 -> 3.32 8.124 -> 8.12 8.125 -> 8.13 How to have the exact results i...
I spent hours in searching how to round "floating numbers" in BASH but couldn't find any correct! solution :( If I put these numbers to Excel, then I will receive the correct results after rounding to 2 decimals:
3.314 -> 3.31
3.315 -> 3.32
8.124 -> 8.12
8.125 -> 8.13
How to have the exact results in BASH? I tried with
printf
and awk
but don't have the same result
prompt> printf '%.*f\n' 2 8.125
8.12
prompt> echo '8.125' | awk '{printf("%.2f\n", $1)}'
8.12
user3719454
(202 rep)
Oct 24, 2023, 11:22 AM
• Last activity: Oct 28, 2023, 11:46 AM
5
votes
5
answers
1658
views
Make awk produce error on non-numeric
I have a program that sums a column in a file: awk -v col=2 '{sum+=$col}END{print sum}' input-file However, it has a problem: If you give it a file that doesn't have numeric data, (or if one number is missing) it will interpret it as zero. I want it to produce an error if one of the fields cannot be...
I have a program that sums a column in a file:
awk -v col=2 '{sum+=$col}END{print sum}' input-file
However, it has a problem: If you give it a file that doesn't have numeric data, (or if one number is missing) it will interpret it as zero.
I want it to produce an error if one of the fields cannot be parsed as a number.
Here's an example input:
bob 1
dave 2
alice 3.5
foo bar
I want it to produce an error because 'bar' is not a number, rather than ignoring the error.
Nick ODell
(2798 rep)
Jan 16, 2019, 05:18 PM
• Last activity: Jul 14, 2023, 11:11 AM
1
votes
2
answers
2219
views
Appending lines with incrementing numbers to a file
I have a text file, and I would like to add lines to the file arranged as follows; #define ICFGx 0x2y where x is a decimal number that begins at 0 and ends at 255, incrementing by 1 with each line and y is a hexadecimal number that begins at 000 and ends at 3FC, incrementing by 0x004 with each line....
I have a text file, and I would like to add lines to the file arranged as follows;
#define ICFGx 0x2y
where x is a decimal number that begins at 0 and ends at 255, incrementing by 1 with each line and y is a hexadecimal number that begins at 000 and ends at 3FC, incrementing by 0x004 with each line.
#define ICFG0 0x2000
#define ICFG1 0x2004
#define ICFG2 0x2008
#define ICFG3 0x200C
I would also like add them from a certain line onward, say line 500.
Is there any way to go about this task from the command line? I'm fairly new to using the linux terminal and I haven't done much bash scripting yet.
Ca01an
(11 rep)
Sep 20, 2018, 03:01 PM
• Last activity: Jun 23, 2023, 09:15 AM
4
votes
4
answers
46097
views
Grep only numbers, not the alphanumeric entries
I have a list of values like: 1 2 3 4 Ak123 Ak23 Ak147 1Apple 2Apricot 3Mango 4Orange I just want to execute a simple grep command to list me only the numbers. i.e. `1`, `2` , `3` and `4`. I tried this command - grep -Ein --color '^\s*[0-9]' test.txt but it returns the alphanumeric also. Instead I t...
I have a list of values like:
1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange
I just want to execute a simple grep command to list me only the numbers. i.e.
1
, 2
, 3
and 4
.
I tried this command -
grep -Ein --color '^\s*[0-9]' test.txt
but it returns the alphanumeric also. Instead I tried omitting the character by this command:
grep -Ein --color '^\s*[0-9][^A-Z]' test.txt
but it gives 0 results.
Srinivasan Senthil
(41 rep)
Nov 29, 2018, 06:01 PM
• Last activity: Jun 20, 2023, 09:22 PM
75
votes
12
answers
123863
views
Is there a unix command that gives the minimum/maximum of two numbers?
I was looking for a command to limit numbers read in from `stdin`. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, simple and (I think) common use case. My script which finds the **minimum** of two numbers: #!/bin/bash...
I was looking for a command to limit numbers read in from
stdin
.
I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, simple and (I think) common use case.
My script which finds the **minimum** of two numbers:
#!/bin/bash
# $1 limit
[ -z "$1" ] && { echo "Needs a limit as first argument." >&2; exit 1; }
read number
if [ "$number" -gt "$1" ]; then
echo "$1"
else
echo "$number"
fi
Minix
(6065 rep)
Feb 24, 2015, 07:09 PM
• Last activity: Feb 21, 2023, 03:21 AM
-1
votes
2
answers
574
views
Test for a number in bash
Is this the correct way to test for a number, with double `[[]]` enclosing `:digit:` and single quotes surrounding the regex ? if [[ "$var" =~ '^[[:digit:]]+$' ]]; then
Is this the correct way to test for a number, with double
[[]]
enclosing :digit:
and single quotes surrounding the regex ?
if [[ "$var" =~ '^[[:digit:]]+$' ]]; then
Vera
(1363 rep)
Jan 29, 2023, 03:30 AM
• Last activity: Jan 29, 2023, 09:36 AM
0
votes
2
answers
2071
views
shell script set the default numbers as double digit zero as prefix
In a shell script, I'm processing, some addition process will print an output. If it is a single-digit one, then it has to add zero as a prefix. Here is my current script: ``` c_year=2020 for i in {01..11} do n_year=2020 echo 'Next Year:'$n_year if [[ $i == 11 ]] then n_month=12 echo 'Next Month:'$n...
In a shell script, I'm processing, some addition process will print an output. If it is a single-digit one, then it has to add zero as a prefix.
Here is my current script:
c_year=2020
for i in {01..11}
do
n_year=2020
echo 'Next Year:'$n_year
if [[ $i == 11 ]]
then n_month=12
echo 'Next Month:'$n_month
else
n_month=$(($i + 1))
echo 'Next Month:'$n_month
fi
echo $date" : Processing data '$c_year-$i-01 00:00:00' and '$n_year-$n_month-01 00:00:00'"
done
The i
value is in doule digit, but the n_month
is still printing single digit. How do I set default shell output should return as double digit?
Or any alternate way to solve this?
TheDataGuy
(405 rep)
Nov 9, 2020, 01:04 PM
• Last activity: Nov 2, 2022, 12:42 PM
11
votes
1
answers
19932
views
How to sort lines by float number
I've such a file: name: xxx --- time: 5.4 seconds name: yyy --- time: 3.2 seconds name: zzz --- time: 6.4 seconds ... Now I want to sort this file by these float numbers to generate a new file as below: name: yyy --- time: 3.2 seconds name: xxx --- time: 5.4 seconds name: zzz --- time: 6.4 seconds ....
I've such a file:
name: xxx --- time: 5.4 seconds
name: yyy --- time: 3.2 seconds
name: zzz --- time: 6.4 seconds
...
Now I want to sort this file by these float numbers to generate a new file as below:
name: yyy --- time: 3.2 seconds
name: xxx --- time: 5.4 seconds
name: zzz --- time: 6.4 seconds
...
I've tried the command
awk '{print $5}' myfile | sort -g
but this will show me ONLY the float numbers.
Yves
(3401 rep)
Jul 30, 2018, 06:02 AM
• Last activity: Oct 5, 2022, 10:53 AM
Showing page 1 of 20 total questions