I am new to shell scripting, can you please help with below requirement, thanks.
$AU_NAME=AU_MSM3-3.7-00.01.02.03
#separate the string after last "-", with "." as delimiter
#that is, separate "00.01.02.03" and print/save as below.
major=00
minor=01
micro=02
build=03
In
bash
, you can do something like this:The
grep
call uses-o
which outputs only the matching part of the line. The match itself is every non-hyphen character to the end of the line.The
cut
command uses the delimeter.
(-d.
), and uses-f
to select individual fields.It's a little clunky. I'm sure there are probably better ways to achieve this, but you can do quite a lot with
grep
andcut
alone so they're handy tools to have in your arsenal.First, note that you don't use
$
when assigning to a parameter in the shell. Your first line should be just this:Once you have that, then you can do something like this:
where the
##*-
strips off everything from the beginning of the string through the last '-', leaving just "00.01.02.03", and the IFS (Internal Field Separator) variable tells the shell where to break the string into fields.In bash, zsh, and ksh93+, you can get that onto one line by shortening the here-document to a here-string:
More generally, in those same shells (or any other shell that has arrays), you can split into an arbitrarily-sized array instead of distinct variables. This works in the given shells:
In older versions of ksh you can do this:
That gets you this equivalence (except in zsh, which by default numbers the elements 1-4 instead of 0-3):
You can use parameter expansion and the special IFS variable.
BTW, in an assignment, do not start the variable on the left hand side with a dollar sign.