Link to home
Start Free TrialLog in
Avatar of Mario Zio
Mario ZioFlag for Italy

asked on

Bash script to parse the Windows version written inside the license.rtf

Hello,

Since I'm totally newbie in bash scripting,can someone help me to write a bash script to parse the version of Windows written inside the license.rtf file ? Thanks.

If it starts with :

MICROSOFT SOFTWARE LICENSE TERMS

WINDOWS 7 ULTIMATE

These license terms are an agreement between Microsoft Corporation (or based on where you live, one of its affiliates) and you. Please read them. They apply to the software named above, which includes the media on which you received it, if any. Printed-paper license terms, which may come with the software, may replace or modify any on-screen license terms. The terms also apply to any Microsoft

I need to grab the version of Windows used,in this case it is : WINDOWS 7 ULTIMATE

thanks in advance.
Avatar of Dushyant Sharma
Dushyant Sharma
Flag of India image

If it is in the third line always then you can use the script below
i=0
while read line
do
      if [ $i -eq 3 ]
      then
            echo "$line"
      fi
      i=`expr $i + 1`
done < winver.txt

here winver.txt is the file to read
An alternate can be

cat winver.txt | grep "WINDOWS"

but this will return all the lines containing WINDOWS (all caps)
Avatar of Mario Zio

ASKER

I like the option n.2. I've tried to parse the eula.txt of windows XP with this command :

mario@mario-desktop:/media/FREEDOS/sh$ cat eula.txt | grep "Microsoft Windows"

Microsoft Windows XP Home Edition

I need to store the output "Microsoft Windows XP Home Edition" inside a variable

and I've tried to parse the license.rtf of Windows Vista / Seven with this command :

cat license.rtf | grep "WINDOWS"

\pard\brdrb\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 WINDOWS 7 ULTIMATE\par
mario@mario-desktop:/media/FREEDOS/sh$

in this case I need to store inside a variable the output : WINDOWS 7 ULTIMATE
inside a script you can always store output into a variable using the command in `` (acutes)


temp = `cat licence.rtf | grep "WINDOWS"`
echo $temp
this should store the the value of the commands (in your case "WINDOWS 7 ULTIMATE")  to the variable temp
cat licence.rtf | grep "WINDOWS" returns nothing,because Rich text is a kind of markup language, similar to html. What you see in the display is not exactly the text the file itself contains. The parsing would have to take this into account.
errata corrige :

cat licence.rtf | grep "WINDOWS" returns * nothing that I can use as is *
ah sure... then may be you can use cut to get the output. Do not have a linux box right now to test it but you can do something like below
cat licence.rtf | grep "WINDOWS" | cut -d\ -f8
here -d\ is used for delimiter as "\"
-f8 is for field 8 after cut is performed, so this should return the output.

you can also use awk for this.
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d\ -f8

error : cut: delimiter must be a single character.
use it in single quotes '\'
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f8

sb120
this is the beginning of the license.rtf file :

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe2052\deftab360{\fonttbl{\f0\fswiss\fprq2\fcharset 0 Tahoma;}}
{\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\nowidctlpar\sb120\sa120\b\f0\fs20 MICROSOFT SOFTWARE LICENSE TERMS\par
\pard\brdrb\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 WINDOWS 7 ULTIMATE\par
\pard\nowidctlpar\sb120\sa120\b0 These license terms are an agreement between Microsoft Corporation (or based on where you live, one of its affiliates) and $
\pard\nowidctlpar\sb120\sa120\tx0\'b7\tab updates,\par
\'b7\tab supplements,\par
\'b7\tab Internet-based services, and\par
\'b7\tab support services\par
\pard\nowidctlpar\sb120\sa120 for this software, unless other terms accompany those items. If so, those terms apply.\par
\b By using the software, you accept these terms. If you do not accept them, do not use the software. Instead, return it to the retailer for a refund or cre$
\b As described below, using the software also operates as your consent to the transmission of certain computer information during activation, validation an$
\pard\brdrt\brdrs\brdrw10\brsp20 \nowidctlpar\sb120\sa120 If you comply with these license terms, you have the rights below for each license you acquire.\par
\pard\nowidctlpar\sb120\sa120\tx360 1.\tab OVERVIEW.\par
use -f9 or -f10 or may be -f7.. here number after f represents the column you want.
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9

sa120 WINDOWS 7 ULTIMATE

mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f10

par

the first should be good,but I have to cut "sa120"
i can also see a space "sa120 WINDOWS 7 ULTIMATE" so you need to do another cut after getting this string like

cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f2
here ' ' is space
mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f2
WINDOWS

has been cut exactly what I want : "7 ULTIMATE"
ASKER CERTIFIED SOLUTION
Avatar of Dushyant Sharma
Dushyant Sharma
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bingo,you "won" 500 points :D

mario@mario-desktop:/media/FREEDOS/sh$ cat license.rtf | grep "WINDOWS" | cut -d'\' -f9 | cut -d' ' -f "2 3 4"
WINDOWS 7 ULTIMATE

btw sh scripting is a kind of language I have because you have to pay your attention over aspects frankly not so important and you can't pay attention over the whole piece of code.
*I hate*
ver_windows=$(cat /mnt/sda1/Windows/system32/eula.txt | grep "Microsoft Windows")
echo $ver_windows

Microsoft Windows XP Home Edition

if [ "$ver_windows" = "Microsoft Windows XP Home Edition" ]; then ver_windows_min="xp"
echo home
fi

output : nothing

I think that $ver_windows is not = to "Microsoft Windows XP Home Edition" ,why ?
Try this
if [ $ver_windows -eq "Microsoft Windows XP Home Edition" ]
then
    ver_windows_min="xp"
    echo $home
fi
FIXED : there was a stupid hidden space at the end of the first line.