All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Masahiro Yamada' <masahiroy@kernel.org>
Cc: "linux-kbuild@vger.kernel.org" <linux-kbuild@vger.kernel.org>,
	"Dominique Martinet" <asmadeus@codewreck.org>,
	Michal Marek <michal.lkml@markovi.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH 3/3] kbuild: rewrite ld-version.sh in shell script
Date: Mon, 21 Dec 2020 14:51:16 +0000	[thread overview]
Message-ID: <2a0911ccea3d47e0bfa5ab6ca2d26bb5@AcuMS.aculab.com> (raw)
In-Reply-To: <CAK7LNAT0-_C3N1AO9uPV-G9U99YMeusD5HULnSmX2CfswQsuYQ@mail.gmail.com>

From: Masahiro Yamada
> Sent: 21 December 2020 14:29
> 
> On Sun, Dec 13, 2020 at 6:47 AM David Laight <David.Laight@aculab.com> wrote:
> >
> > From: Masahiro Yamada
> > > Sent: 12 December 2020 16:55
> > >
> > > This script was written in awk in spite of the file extension '.sh'.
> > > Rewrite it as a shell script.
> > ...
> > > +#
> > > +# Usage: $ ./scripts/ld-version.sh ld
> > > +#
> > > +# Print the linker version of `ld' in a 5 or 6-digit form
> > > +# such as `23501' for GNU ld 2.35.1 etc.
> > > +
> > > +first_line="$($* --version | head -n 1)"
> > > +
> > > +if ! ( echo $first_line | grep -q "GNU ld"); then
> > > +     echo 0
> > > +     exit 1
> > > +fi
> > > +
> > > +# Distributions may append an extra string like 2.35-15.fc33
> > > +# Take the part that consists of numbers and dots.
> > > +VERSION=$(echo $first_line | sed 's/.* \([^ ]*\)$/\1/' | sed 's/^\(^[0-9.]*\).*/\1/')
> > > +MAJOR=$(echo $VERSION | cut -d . -f 1)
> > > +MINOR=$(echo $VERSION | cut -d . -f 2)
> > > +PATCHLEVEL=$(echo $VERSION | cut -d . -f 3)
> > > +printf "%d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
> >
> >
> > Hmmmm.....
> > You've managed to convert an awk script into something that requires
> > sh, head, grep, sed (twice), and cut (thrice).
> > Plus (probably) a few sub-shells.
> >
> > It is quite ease to do it all in all in the shell.
> >
> >         David
> >
> 
> OK, please rewrite the code.

I've posted a couple of versions before, how about this one.
I've added a few comments - which don't need to be in the final version.

# Get the first line of the 'ld --version' output
if input_from_from_stdin; then
	read line
else
	IFS='
'
	set -- $("$@")
	line="$1"
fi

# Split the line on 'space' and get the last word
IFS=' '
set -- $line
shift $(($# - 1))
version="$1"

# Split on '.' and '-'
IFS='.-'
set -- $version

# The three version components are now $1 $2 and $3
# so you can do either
printf "%d%02d%02d\\n" $1 $2 $3
# or
echo $(($1 * 10000 + $2 * 100 + $3))
# both are builtins on recent shells (printf is most likely not to be).

So this should be the same as the script above:

#! /bin/sh
IFS='
'
set -- $("$@")
# The last word contains the version
IFS=' '
set -- $1
shift $(($# - 1))
# Get the first 3 parts of the version
IFS='.-'
set -- $1
printf "%d%02d%02d\\n" $1 $2 $3

Seems to work for me.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  reply	other threads:[~2020-12-21 14:53 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 16:54 [Cocci] [PATCH 1/3] kbuild: do not use scripts/ld-version.sh for checking spatch version Masahiro Yamada
2020-12-12 16:54 ` Masahiro Yamada
2020-12-12 16:54 ` [PATCH 2/3] kbuild: LD_VERSION redenomination Masahiro Yamada
2020-12-12 16:54   ` Masahiro Yamada
2020-12-12 16:54   ` Masahiro Yamada
2020-12-14 23:05   ` Will Deacon
2020-12-14 23:05     ` Will Deacon
2020-12-14 23:05     ` Will Deacon
2020-12-15 13:48   ` Thomas Bogendoerfer
2020-12-15 13:48     ` Thomas Bogendoerfer
2020-12-15 13:48     ` Thomas Bogendoerfer
2021-01-28  6:38   ` Masahiro Yamada
2021-01-28  6:38     ` Masahiro Yamada
2021-01-28  6:38     ` Masahiro Yamada
2020-12-12 16:54 ` [PATCH 3/3] kbuild: rewrite ld-version.sh in shell script Masahiro Yamada
2020-12-12 17:48   ` Dominique Martinet
2020-12-21 14:23     ` Masahiro Yamada
2020-12-12 20:21   ` kernel test robot
2020-12-12 20:21     ` kernel test robot
2020-12-12 20:53   ` kernel test robot
2020-12-12 20:53     ` kernel test robot
2020-12-12 21:42   ` kernel test robot
2020-12-12 21:42     ` kernel test robot
2020-12-12 21:47   ` David Laight
2020-12-21 14:29     ` Masahiro Yamada
2020-12-21 14:51       ` David Laight [this message]
2020-12-12 17:30 ` [Cocci] [PATCH 1/3] kbuild: do not use scripts/ld-version.sh for checking spatch version Julia Lawall
2020-12-12 17:30   ` Julia Lawall
2020-12-13 13:25 ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2a0911ccea3d47e0bfa5ab6ca2d26bb5@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=asmadeus@codewreck.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=michal.lkml@markovi.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.