3

I have a string:

6.40.4

and a second one:

6.40

I am using var=$(echo $versionfull | cut -d'.' -f3) to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ] or [ $var == "" ] does not work. I want to give it a value of 0 in case of a second string.

  • 2
    Pipe it to od to see what it returns... – don_crissti Aug 29 '18 at 19:28
8

You could use var=$(echo "${versionfull}.0" | cut -d'.' -f3).

In the first case, versionfull will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0 will be padded and returned.

  • Thanks. This is simple and works exactly like I want it to. – Kliwer Aug 29 '18 at 19:39
  • 3
    It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns. – pipe Aug 30 '18 at 9:28
  • You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem. – ThoriumBR Aug 30 '18 at 11:43
10

To answer your direct question, cut returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):

echo "$versionfull" | cut -d. -f3 | od -c
0000000  \n
0000001

If you have a shell that supports here-strings, you could do the following:

IFS=. read a b c <<< "$versionfull"

Notice the quoting for $versionfull, in case it ever had whitespace (anything from $IFS).

If you think that c might be empty, then ask to set it to zero:

c=${c:-0}
  • 2
    Note that the need to quote in IFS=. read a b c <<< "$versionfull" was only for older versions of bash; other shells and newer versions of bash don't have the problem. – Stéphane Chazelas Aug 29 '18 at 19:43
  • Note that if the input has only one field, cut returns that field. – Stéphane Chazelas Aug 29 '18 at 20:05
6

cut has an odd API.

cut -f n will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:

$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3

$ echo a | cut -d. -f3
a

So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.

You can add the -s option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:

$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$

(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).

So @ThoriumBR's suggestion to add a .0 is a very good one. Without it:

versionfull=5
echo "$versionfull" | cut -d. -f3

would actually output 5. By adding .0, we make sure the input has at least one delimiter. I would go further and use:

echo "$versionfull.0.0" | cut -d. -f3

To make sure the input has at least 3 fields.

  • The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that. – Kliwer Aug 29 '18 at 20:11
  • How does this relate to his second case? The input has a delimiter. – Barmar Aug 29 '18 at 22:59
1

It sets it to an empty string. You should quote the variable when using it with the [ command.

if [ "$var" == "" ]
then echo "Var is empty"
fi

Without the quotes, the first line expands to:

if [ == "" ]

which is invalid syntax because == requires a parameter to the left of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.